Module: Challah::AuthableRole::InstanceMethods

Defined in:
lib/challah/authable/role.rb

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(sym, *args, &block)

Customized method_missing call that can be used to detect whether a role has a given permission. This passes a key to the #has method.

Examples:

role.admin?               # => role.has(:admin)
role.my_permission?       # => role.has(:my_permission)

See Also:



149
150
151
152
# File 'lib/challah/authable/role.rb', line 149

def method_missing(sym, *args, &block)
  return has(sym.to_s.gsub(/\?/, '')) if sym.to_s =~ /^[a-z0-9_]*\?$/
  super(sym, *args, &block)
end

Instance Method Details

- (Boolean) has(permision_or_key) Also known as: permission?

Does this role have the given Permission? Pass in a Permission instance, or a permission key to check for its existance.

Parameters:

  • permision_or_key (Permission, String, Symbol)

    The permission to check for.

Returns:

  • (Boolean)

    Does this role have the given permission?

See Also:



135
136
137
138
# File 'lib/challah/authable/role.rb', line 135

def has(permision_or_key)
  symbolized_key = ::Permission === permision_or_key ? permision_or_key[:key] : permision_or_key.to_s
  permission_keys.include?(symbolized_key)
end

- (Array) permission_keys

Grab all permission keys for this Role

Note that this returns permission keys, not Permission instances.

Returns:

  • (Array)

    List of permission keys registered to this role.

See Also:



111
112
113
# File 'lib/challah/authable/role.rb', line 111

def permission_keys
  @permission_keys ||= self.permissions.collect(&:key)
end

- (Array) permission_keys=(keys)

Set the permission keys that this role can access. This temporarily updates the permission keys for the Role instance, but changes are not saved until the model has been saved.

Parameters:

  • keys (Array)

    An array of permission keys to set for this role.

Returns:

  • (Array)

    List of permission keys updated.

See Also:



123
124
125
126
# File 'lib/challah/authable/role.rb', line 123

def permission_keys=(keys)
  @permission_keys = keys
  @permission_keys
end