• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 12 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

fake 'em until you make 'em

Devise Guests

Gem Version Tests Ruby Style Guide

A drop-in guest user implementation for devise

(I'm using "user" to mean my devise model, but you should be able to use any model you want, just like devise)

Installation

# install devise first
# gem install devise
# rails g devise:install
# rails g devise User

gem install devise-guests
rails g devise_guests User

Usage

# Where you might use current_user; now you can use

current_or_guest_user

# which returns

current_user # (for logged in users)

=> User<id: 1, email: ...>
# or 

guest_user # ( for anonymous users)

=> User<id: 2, email: guest_RANDOMENOUGHSTRING_@example.com, guest: true>

Transferring Guest to User on Login

During the login process you may want to transfer things from your guest user to the account the logged into. To do so, add the following method to your ApplicationController:

private
def transfer_guest_to_user
  # At this point you have access to:
  #   * current_user - the user they've just logged in as
  #   * guest_user - the guest user they were previously identified by
  # 
  # After this block runs, the guest_user will be destroyed!
  
  if current_user.cart
    guest_user.cart.line_items.update_all(cart_id: current_user.cart.id)
  else
    guest_user.cart.update!(user: current_user)
  end
end

Custom attribute

If you have added additional authentication_keys, or have other attributes on your Devise model that you need to set when creating a guest user, you can do so by overriding the set_guest_user_params method in your ApplicationController:

private
def guest_user_params
  { site_id: current_site.id }
end

Non-standard authentication keys

By default Devise will use :email as the authentication key for your model. If for some reason you have modified your Devise config to use an alternative attribute (such as :phone_number) you will need to provide a method to generate the value of this attribute for any guest users which are created.

Sticking with the :phone_number example, you should define the following method in your application_controller.rb:

private
def guest_phone_number_authentication_key(key)
  key &&= nil unless key.to_s.match(/^guest/)
  key ||= "guest_447" + 9.times.map { SecureRandom.rand(0..9) }.join
end

Validations are skipped when creating guest users, but if you need to rely on future modifications to the guest record passing validations, then you should ensure that this default value for guests is generated in such a way as to be valid.

Prevent deletion of guest records

By default, when signing in from a guest account to an authenticated account, the guest user is destroyed. You have an opportunity through the logging_in_user callback (or logging_in_MODEL if you're not using User) to transfer data from the guest to the main account before destruction occurs.

However, for some situations such as guest-checkout, you may desire that any guest account which makes a purchase does not get destroyed. In that case you can use the skip_destroy_guest_user method to identify when to skip deleting these records. By default this method returns false, indicating that every record is acceptable for destruction, but you could use something like the following to optionally prevent it:

def skip_destroy_guest_user
  guest_user.orders.any?
end

More Repositories

1

solr_wrapper

Wrap your tests with Solr 5+
Ruby
23
star
2

engine_cart

Rake tasks to generate test applications for Rails Engines
Ruby
17
star
3

deprecation

Stand-alone deprecation library borrowed from ActiveSupport::Deprecation
Ruby
9
star
4

fedora-workflow

Fedora-workflow is a Ruote-based workflow dispatching engine
Ruby
9
star
5

docker-aeneas

7
star
6

openseadragon-rails

OpenSeadragon assets and helpers for Rails. http://openseadragon.github.io/
Ruby
7
star
7

microservices

RoR µservices demonstrator
Ruby
5
star
8

metadatacop

Metadata quality analyzer
Ruby
5
star
9

npr-solr

AJAX-Solr interface to an NPR API solr index
5
star
10

rails-styleguide

Rails Engine version of the kneath/kss styleguide generator
Ruby
5
star
11

ave-sync

Timed XML synchronization/alignment utility
JavaScript
5
star
12

ldpath-angular-demo-app

Ruby
4
star
13

ruby-odrl

An Open Digital Rights Language-based Policy Decision Point
Ruby
4
star
14

blacklight_highlight

Blacklight plugin to provide Solr field highlighting
Ruby
4
star
15

docker-dspace

Shell
4
star
16

blacklight_facet_extras

Ruby
4
star
17

docker-heritrix

Shell
4
star
18

icemelt

Mock the Glacier API for faster testing
Ruby
4
star
19

fedora-content-models

Content models for the Fedora Commons repository
3
star
20

docker-omeka

3
star
21

nom-xml

Ruby
3
star
22

blacklight_user_generated_content

Ruby
3
star
23

tc

Timecode parser
Ruby
3
star
24

blacklight_unapi

unapi provider for blacklight
Ruby
3
star
25

fedora-ingest-workflow

Ruby
3
star
26

pairtree

Now maintained at http://github.com/microservices
Ruby
3
star
27

blacklight-oai-demo

Superseded by
JavaScript
3
star
28

lockit

Now maintained at http://github.com/microservices
Ruby
3
star
29

mirador-lunrsearch

JavaScript
3
star
30

kaminari_route_prefix

Ruby
2
star
31

docker-fcrepo4

2
star
32

blacklight_mlt

MoreLikeThis plugin for Blacklight
Ruby
2
star
33

fcrepo_wrapper

Ruby
2
star
34

edtf

Ruby
2
star
35

docker-tessera

Shell
2
star
36

resourcesync-validator

Validate Sitemap and ResourceSync manifests; Demo:
Ruby
2
star
37

blacklight_multiselect

A Blacklight plugin for multi select facet functionality
Ruby
2
star
38

docker-piwik

2
star
39

namaste

Now maintained at http://github.com/microservices
Ruby
2
star
40

checkm

Now maintained at http://github.com/microservices
Ruby
2
star
41

anvl

Now maintained at http://github.com/microservices
Ruby
2
star
42

fcrepo-ldpath-solr-camel

Java
2
star
43

rsolr-footnotes

RSolr request/response debug information for Rails-footnotes
Ruby
2
star
44

noid

Now maintained at http://github.com/microservices
Ruby
2
star
45

ldpath-spec

1
star
46

docker-solr-cloud

Shell
1
star
47

graphite-tools

Ruby
1
star
48

mfcc

Mel-frequency cepstral coefficients for Ruby
Ruby
1
star
49

ave-pbcore-editor

An Orbeon XForms based editor for the PBCore metadata standard
1
star
50

docker-phant

Dockerized version of sparkfun/phant
Shell
1
star
51

blacklight_oembed

Blacklight oEmbed plugin
Ruby
1
star
52

blacklight_oai_harvester_demo

Blacklight 3.x demo app for harvesting metadata from OAI-PMH providers (WIP)
Ruby
1
star
53

om_pbcore_test

testing only..
Ruby
1
star
54

is_it_working

Ruby
1
star
55

Project-Hydra-Google-CSE

1
star
56

tei-sync

TEI media/text alignment tool (prototype)
JavaScript
1
star
57

scherzo-blacklight

Ruby
1
star
58

fcrepo-visualization

Visualizations of Fedora Commons Digital Objects
Ruby
1
star
59

blank_blank

Ruby
1
star
60

solr-rpm

RPMs for Solr 5
Shell
1
star
61

dflat

Now maintained at http://github.com/microservices
Ruby
1
star
62

rdf-hurley

Ruby
1
star
63

om_xml_strawman

Ruby
1
star
64

trnsfrm

Transformation microservice
1
star
65

hubot-lunch

CoffeeScript
1
star
66

processwrapper

Ruby
1
star
67

fedora_akubra_mappers

Java
1
star
68

ave

Audio/Video Environment for HTML5
JavaScript
1
star
69

publicmediatech-stations

Public media station website gallery
Ruby
1
star
70

lodmonitor

Monitor the health and availability of linked open data resources
Ruby
1
star
71

docker-infinispan

Shell
1
star