Sha256: 5a6a73f9658248364b9451d52ea3ed774cbc48f4ff73efd027c3c4c24575dc61

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

module Authority

  # Should be included into all models in a Rails app. Provides the model
  # with both class and instance methods like `updatable_by?(user)`
  # Exactly which methods get defined is determined from `config.abilities`;
  # the module is evaluated after any user-supplied config block is run
  # in order to make that possible.
  # All delegate to the methods of the same name on the model's authorizer.

  module Abilities
    extend ActiveSupport::Concern

    included do |base|
      class_attribute :authorizer_name

      # Set the default authorizer for this model.
      # - Look for an authorizer named like the model inside the model's namespace.
      # - If there is none, use 'ApplicationAuthorizer'
      self.authorizer_name = begin
        "#{base.name}Authorizer".constantize.name
      rescue NameError => e
        "ApplicationAuthorizer"
      end
    end


    def authorizer
      self.class.authorizer.new(self) # instantiate on every check, in case model has changed
    end

    module Definitions
      # Send all calls like `editable_by?` to an authorizer instance
      # Not using Forwardable because it makes it harder for users to track an ArgumentError
      # back to their authorizer
      Authority.adjectives.each do |adjective|
        define_method("#{adjective}_by?") { |*args| authorizer.send("#{adjective}_by?", *args) }
      end
    end
    include Definitions

    module ClassMethods
      include Definitions

      def authorizer=(authorizer_class)
        @authorizer          = authorizer_class
        self.authorizer_name = @authorizer.name
      end

      # @return [Class] of the designated authorizer
      def authorizer
        @authorizer ||= authorizer_name.constantize # Get an actual reference to the authorizer class
      rescue NameError
        raise Authority::NoAuthorizerError.new(
                  "#{authorizer_name} is set as the authorizer for #{self}, but the constant is missing"
              )
      end

    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
authority-3.1.0 lib/authority/abilities.rb
authority-3.0.0 lib/authority/abilities.rb
authority-2.10.0 lib/authority/abilities.rb
authority-2.9.0 lib/authority/abilities.rb
authority-2.8.1 lib/authority/abilities.rb
authority-2.8.0 lib/authority/abilities.rb
authority-2.7.0 lib/authority/abilities.rb