lib/authority/abilities.rb in authority-2.3.1 vs lib/authority/abilities.rb in authority-2.3.2

- old
+ new

@@ -7,56 +7,43 @@ # 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 + extend Forwardable # Assume authorizer is `ApplicationAuthorizer` (but let the user change that) included do class_attribute :authorizer_name self.authorizer_name = "ApplicationAuthorizer" end + def authorizer + self.class.authorizer.new(self) # instantiate on every check, in case model has changed + end + + # Send all calls like `editable_by?` to an authorizer instance + Authority.adjectives.each do |adjective| + def_delegators :authorizer, :"#{adjective}_by?" + end + module ClassMethods + extend Forwardable + # Send all calls like `editable_by?` to the authorizer class Authority.adjectives.each do |adjective| - - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{adjective}_by?(user, options = {}) - if options.empty? - authorizer.#{adjective}_by?(user) - else - authorizer.#{adjective}_by?(user, options) - end - end - RUBY + def_delegators :authorizer, :"#{adjective}_by?" 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 - Authority.adjectives.each do |adjective| - - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{adjective}_by?(user, options = {}) - if options.empty? - authorizer.#{adjective}_by?(user) - else - authorizer.#{adjective}_by?(user, options) - end - end - - def authorizer - self.class.authorizer.new(self) # instantiate on every check, in case model has changed - end - RUBY end end end