Module: Credentials::Extensions::Object::ClassMethods

Defined in:
lib/credentials/extensions/object.rb

Instance Method Summary collapse

Instance Method Details

#credentials(options = nil) ⇒ Object

The main method for specifying and retrieving the permissions of a member of this class.

When called with a block, this method yields a Credentials::Rulebook object, allowing you to specify the class’s credentials in a declarative fashion. For example:

class User
  credentials do |user|
    user.can :edit, User, :if => :administrator?
    user.can :edit, :self
  end
end

You can also specify options in this way:

class User
  credentials(:default => :allow) do |user|
    user.cannot :eat, "ice cream", :if => :lactose_intolerant?
  end
end

The following options are supported:

:default

Whether to :allow or :deny permissions that aren’t specified explicitly. The default default (!) is :deny.

When called without a block, credentials just returns the class’s Credentials::Rulebook, creating it if necessary.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/credentials/extensions/object.rb', line 34

def credentials(options = nil)
  @credentials ||= Credentials::Rulebook.new(self)
  if block_given?
    @credentials.options.merge!(options) unless options.nil?
    
    # Only include the magic methods for classes where 'credentials' is
    # explicitly called with a block: otherwise, you get all kinds of 
    # RailsFails.
    unless included_modules.include?(Credentials::Extensions::MagicMethods)
      include Credentials::Extensions::MagicMethods 
    end
    yield @credentials
  else
    raise ArgumentError, "you can only set options with a block" unless options.nil?
  end
  @credentials
end