Module: UbiquitousUser::Helpers

Included in:
Usable
Defined in:
lib/ubiquitous_user/ubiquitous_user.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject

Helper method to get the current user. It will always return a user but the user may not be in the database. If options is true, then the user will be in the database (although it may be a ghost user).



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ubiquitous_user/ubiquitous_user.rb', line 9

def current_user
  # Find the user in the database if session[:user_id] is defined and @ubiquitous_user is not.
  @ubiquitous_user = UbiquitousUser::Config::user_model_class.find_by_id(session[:user_id]) if session[:user_id] != nil and @ubiquitous_user == nil

  # Create a new user object if @ubiquitous_user is not defined.
  @ubiquitous_user = UbiquitousUser::Config::user_model_class.send(UbiquitousUser::Config::user_model_new) if @ubiquitous_user == nil

  # If the object is new, let's get ready to mark the user as logged in when saving.
  if @ubiquitous_user.new_record? or @ubiquitous_user.id != session[:user_id]
    if !@ubiquitous_user.respond_to? :mark_user_as_logged_in_in_the_session
      UbiquitousUser::Config::user_model_class.class_eval do
        after_save :mark_user_as_logged_in_in_the_session

        def mark_user_as_logged_in_in_the_session
          if !@session_reference_by_ubiquitous_user.nil?
            @session_reference_by_ubiquitous_user[:user_id] = id
          end
        end
      end
    end
    @ubiquitous_user.instance_variable_set "@session_reference_by_ubiquitous_user", self.session
  end

  return @ubiquitous_user
end