Sha256: 351ca59dd34b0e35c1f87fab4c5858a769efa7dce3aec302e91a06ec3431b85f

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module CanTango::Api
  module Ability
    module Account
      def account_ability account, options = {}
        @account_ability ||= create_ability(account, ability_options.merge(options))
      end

      def current_account_ability user_type = :user
        account_ability(get_ability_account user_type)
      end

      protected

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

      def get_ability_account user_type = :user
        account_meth = :"current_#{user_type}_account"
        return AbilityAccount.guest if !respond_to?(account_meth)
        AbilityAccount.resolve_account(send account_meth)
     end

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

        def self.is_account? account
          ::CanTango.config.accounts.registered_class? account.class
        end

        def self.guest
          account = CanTango.config.guest.account

          raise "You must set the guest_account to a Proc or lambda in CanTango.config" if !account
          account.respond_to?(:call) ? account.call : account
        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/account.rb
cantango-api-0.1.2 lib/cantango/api/ability/account.rb
cantango-api-0.1.1 lib/cantango/api/ability/account.rb
cantango-api-0.1.0 lib/cantango/api/ability/account.rb