module Croods::Resource::Policy

Public Instance Methods

create_policy!() click to toggle source
# File lib/croods/resource/policy.rb, line 32
def create_policy!
  Object.const_set(policy_name, Class.new(Croods::Policy))
  policy_blocks.each { |block| policy.instance_eval(&block) }
  create_policy_actions!
end
create_policy_actions!() click to toggle source
# File lib/croods/resource/policy.rb, line 38
def create_policy_actions!
  (actions + additional_actions).each do |action|
    policy.define_method("#{action.name}?") { authorize_action(action) }

    Object.const_set(
      policy_scope_name(action.name), Class.new(Croods::Policy::Scope)
    )

    policy_scope(action.name).define_method(:action) { action }
  end
end
extend_policy(&block) click to toggle source
# File lib/croods/resource/policy.rb, line 6
def extend_policy(&block)
  return unless block

  policy_blocks << block
end
policy() click to toggle source
# File lib/croods/resource/policy.rb, line 16
def policy
  policy_name.constantize
end
policy_blocks() click to toggle source
# File lib/croods/resource/policy.rb, line 12
def policy_blocks
  @policy_blocks ||= []
end
policy_name() click to toggle source
# File lib/croods/resource/policy.rb, line 24
def policy_name
  "#{model_name}Policy"
end
policy_scope(action) click to toggle source
# File lib/croods/resource/policy.rb, line 20
def policy_scope(action)
  policy_scope_name(action).constantize
end
policy_scope_name(action) click to toggle source
# File lib/croods/resource/policy.rb, line 28
def policy_scope_name(action)
  "#{model_name}#{action.to_s.titleize.gsub(/\ /, '')}Scope"
end