Sha256: b953d70cb83ba22028455c3dc1a954f780086ca0030426aa9f710c798f2ec579

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Authority
  module UserAbilities

    # Should be included into whatever class represents users in an app.
    # Provides methods like `can_update?(resource)`
    # 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 corresponding methods on the resource.

    Authority.verbs.each do |verb|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def can_#{verb}?(resource, options = {})
          if options.empty?
            resource.#{Authority.abilities[verb]}_by?(self)
          else
            resource.#{Authority.abilities[verb]}_by?(self, options)
          end
        end
      RUBY
    end

    def can?(action, options = {})
      self_and_maybe_options = [self, (options == {} ? nil : options)].compact # throw out if nil
      begin
        ApplicationAuthorizer.send("authorizes_to_#{action}?", *self_and_maybe_options)
      rescue NoMethodError => original_exception
        begin
          # For backwards compatibility
          response = ApplicationAuthorizer.send("can_#{action}?", *self_and_maybe_options)
          Authority.logger.warn(
            "DEPRECATION WARNING: Please rename `ApplicationAuthorizer.can_#{action}?` to `authorizes_to_#{action}?`"
          )
          response
        rescue NoMethodError => new_exception
          raise original_exception
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authority-2.3.2 lib/authority/user_abilities.rb