Sha256: 401c7d095c253d57ab58dc91e680e0546fd0728571ced1d848d4bf9c08f2e22b

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module CanTango::Api
  module Ability
    module User
      def user_ability user, options = {}
        create_ability(user, ability_options.merge(options))
      end

      def current_user_ability user_type = :user
        user_ability get_ability_user(user_type)
      end

      protected

      include CanTango::Api::Common
      include CanTango::Api::Attributes
      include CanTango::Api::Options

      def get_ability_user user_type
        user_meth = :"current_#{user_type}"
        return AbilityUser.guest if !respond_to?(user_meth)
        AbilityUser.resolve_user(send user_meth)
     end

      module AbilityUser
        # test if current_xxx actually returns an account and not a user instance!
        # if so call the #user method on the account
        def self.resolve_user obj
          return obj if is_user?(obj)
          return resolve_user(obj.send(:user)) if obj.respond_to? :user
          guest
        end

        def self.is_user? user
          ::CanTango.config.users.registered_class? user.class
        end

        def self.guest
          user = CanTango.config.guest.user

          raise "You must set the guest_user to a Proc or lambda in CanTango.config" if !user
          user.respond_to?(:call) ? user.call : user
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cantango-api-0.1.3 lib/cantango/api/ability/user.rb
cantango-api-0.1.2 lib/cantango/api/ability/user.rb
cantango-api-0.1.1 lib/cantango/api/ability/user.rb
cantango-api-0.1.0 lib/cantango/api/ability/user.rb