Module: ResourcePolicy::Policy::AttributesPolicy

Defined in:
lib/resource_policy/policy/attributes_policy.rb,
lib/resource_policy/policy/attributes_policy/attribute_policy.rb,
lib/resource_policy/policy/attributes_policy/attribute_configuration.rb,
lib/resource_policy/policy/attributes_policy/attributes_policy_model.rb

Overview

Allows to define fields policy using configuration block.

Usage example:

class SomeModelPolicy

include ResourcePolicy::AttributesPolicy

policy do |c|
  c.attribute(:some_name)
    .allowed(:read, if: :readable?)
    .allowed(:write, if: :writable?)

  c.group(:current_user_is_admin?) do |g|
    g.attribute(:password).allowed(:write)
  end
end

def current_user_is_admin?
   current_user.admin?
end
...

end

Defined Under Namespace

Classes: AttributeConfiguration, AttributePolicy, AttributesPolicyModel

Instance Method Summary collapse

Instance Method Details

#action(name) ⇒ Object



35
36
37
# File 'lib/resource_policy/policy/attributes_policy.rb', line 35

def action(name)
  actions_policy.public_send(name) if actions_policy.respond_to?(name)
end

#attribute(name) ⇒ Object



39
40
41
# File 'lib/resource_policy/policy/attributes_policy.rb', line 39

def attribute(name)
  attributes_policy.public_send(name) if attributes_policy.respond_to?(name)
end

#attributes_policyObject



47
48
49
# File 'lib/resource_policy/policy/attributes_policy.rb', line 47

def attributes_policy
  @attributes_policy ||= AttributesPolicyModel.new(self)
end

#policy_targetObject



43
44
45
# File 'lib/resource_policy/policy/attributes_policy.rb', line 43

def policy_target
  send(self.class.policy.policy_target)
end

#protected_resourceObject



31
32
33
# File 'lib/resource_policy/policy/attributes_policy.rb', line 31

def protected_resource
  @protected_resource ||= ProtectedResource.new(self)
end