Module: Copper::Permission

Included in:
Permission
Defined in:
lib/copper/permission.rb

Defined Under Namespace

Classes: Naming

Instance Method Summary collapse

Instance Method Details

#apply_to(ability) ⇒ Object

Apply this permission to the ability, directly via cancan or via the Policy object if one is available



39
40
41
42
43
44
45
46
47
# File 'lib/copper/permission.rb', line 39

def apply_to(ability)
  if has_action_policy?
    policy_action_class.new(ability).apply!
  elsif has_type_policy?
    policy_type_class.new(ability).apply!
  else
    ability.can(action_name.to_sym, object_type.constantize)
  end
end

#has_action_policy?Boolean

Returns true if there is a Policy class specific to the action available to complement the permission.

Returns:

  • (Boolean)

    true if there is a Policy class specific to the action available to complement the permission



14
15
16
# File 'lib/copper/permission.rb', line 14

def has_action_policy?
  self.class.const_defined?(Naming.action_class_name(object_type, action_name))
end

#has_type_policy?Boolean

Returns true if there is a Policy class for the type available to complement the permission.

Returns:

  • (Boolean)

    true if there is a Policy class for the type available to complement the permission



19
20
21
# File 'lib/copper/permission.rb', line 19

def has_type_policy?
  self.class.const_defined?(Naming.class_name(object_type))
end

#policy_action_classObject

The Policy class for the specific action if one is present

Raises:

  • NameError if none present



25
26
27
# File 'lib/copper/permission.rb', line 25

def policy_action_class
  Naming.action_class_name(object_type, action_name).constantize
end

#policy_type_classObject

The Policy class for the type if one is present (Only used if there is no specific action class)

Raises:

  • NameError if none present



32
33
34
# File 'lib/copper/permission.rb', line 32

def policy_type_class
  Naming.class_name(object_type).constantize
end