Module: Challah::Rolls::Role::InstanceMethods

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

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:



168
169
170
171
# File 'lib/challah/rolls/role.rb', line 168

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

#has(permision_or_key) ⇒ Boolean 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:



154
155
156
157
# File 'lib/challah/rolls/role.rb', line 154

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

#permission_keysArray

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:



130
131
132
# File 'lib/challah/rolls/role.rb', line 130

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

#permission_keys=(keys) ⇒ Array

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:



142
143
144
145
# File 'lib/challah/rolls/role.rb', line 142

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