-
-
Save papricek/4374507 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnonymousUser < User | |
attr_accessible *ACCESSIBLE_ATTRS, :type, :token, as: :registrant | |
def register(params) | |
params = params.merge(type: 'User', token: nil) | |
self.update_attributes(params, as: :registrant) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
def authenticate_user!(*args) | |
current_user.present? || super(*args) | |
end | |
def current_user | |
super || AnonymousUser.find_or_initialize_by_token(anonymous_user_token).tap do |user| | |
user.save(validate: false) if user.new_record? | |
end | |
end | |
private | |
def anonymous_user_token | |
session[:user_token] ||= SecureRandom.hex(8) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegistrationsController < Devise::RegistrationsController | |
def create | |
@user = AnonymousUser.find(current_user.id) | |
if @user.register(params[:user]) | |
sign_in_and_redirect @user.becomes(User) | |
else | |
render :new | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RetrospectionApp::Application.routes.draw do | |
devise_for :users, controllers: {registrations: 'registrations'} | |
resources :posts, path: '/' | |
root to: 'posts#index' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment