Sha256: 833ac4e43d3220941f708e7fbdd245bfaa69b931b05cdd4f3eadc5183dfb63d5

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

module CanTango
  class PermitEngine < Engine
    class Finder
      # This class is used to find the right permit, possible scoped for a specific user account
      attr_reader :user_account, :name

      def initialize user_account, name
        @user_account = user_account
        @name = name
      end

      def get_permit
        raise find_error if !retrieve_permit
        retrieve_permit
      end

      protected

      def find_error
        "Permit for #{type} #{name} could not be loaded. Define either class: #{account_permit_class} or #{permit_class}"
      end

      def retrieve_permit
        @found_permit ||= [account_permit(name), permit(name)].compact.first
      end

      def account_permit name
        
        # TODO: User/Account cases should be handled somehow following is just interim measure
        return nil if !user_account.class.name =~ /Account/
        account_permits_for_account.send(type).registered[name]
      rescue
        nil
      end

      def permit name
        permits.send(type).registered[name]
      end

      def permits
        CanTango.config.permits
      end

      # TODO: make proper account touching

      def account_permits_for_account
        account_permits.send(account)
      end

      def account_permits
        CanTango.config.permits
      end

      def account
        user_account.class.name.underscore
      end
      # this is used to namespace role permits for a specific type of user account
      # this allows role permits to be defined differently for each user account (and hence sub application) if need be
      # otherwise it will fall back to the generic role permit (the one which is not wrapped in a user account namespace)
      def account_permit_class
        [account_permit_ns , permit_class].join('::')
      end

      def account_permit_ns
        "#{user_account.class}Permits"
      end

      def permit_class
        "#{name.to_s.camelize}Permit"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cantango-0.8.7 lib/cantango/permit_engine/finder.rb
cantango-0.8.6.2 lib/cantango/permit_engine/finder.rb
cantango-0.8.6.1 lib/cantango/permit_engine/finder.rb
cantango-0.8.6 lib/cantango/permit_engine/finder.rb
cantango-0.8.5.1 lib/cantango/permit_engine/finder.rb