Module: CanTango::Api::Session::User

Defined in:
lib/cantango/api/session/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/cantango/api/session/user.rb', line 4

def self.included(base)
  ::CanTango.config.users.registered.each do |user|
    base.class_eval %{
      def session_#{user}
        current_#{user} if respond_to? :current_#{user}
        guest_user
      end
    }
  end
end

Instance Method Details

#active_userObject



31
32
33
# File 'lib/cantango/api/session/user.rb', line 31

def active_user
  session[:active_user]
end

#active_user=(user) ⇒ Object



35
36
37
# File 'lib/cantango/api/session/user.rb', line 35

def active_user= user
  session[:active_user] = user
end

#any_user(*types) ⇒ Object

give me any logged in user or the guest user



16
17
18
19
20
21
22
23
24
25
# File 'lib/cantango/api/session/user.rb', line 16

def any_user *types
  types = types.flatten.select_labels.map(&:to_sym)
  types = types & ::CanTango.config.users.registered
  users = types.map do |type|
    meth = :"current_#{type}"
    send(meth) if respond_to?(meth) && (types.empty? || types.include?(type))
  end
  chosen_user = users.compact.first
  chosen_user || guest_user
end

#guest_userObject



27
28
29
# File 'lib/cantango/api/session/user.rb', line 27

def guest_user
  CanTango.config.guest.user
end