module Permitter::Permission

Public Instance Methods

allow_action(controllers, actions, &block) click to toggle source
# File lib/permitter/permission.rb, line 31
def allow_action(controllers, actions, &block)
  @allowed_actions ||= {}
  Array(controllers).flatten.each do |controller|
    Array(actions).flatten.each do |action|
      @allowed_actions[[controller.to_s, action.to_s]] = block || true
    end
  end
end
allow_all() click to toggle source
# File lib/permitter/permission.rb, line 51
def allow_all
  @allow_all = true
end
allow_all?() click to toggle source
# File lib/permitter/permission.rb, line 55
def allow_all?
  @allow_all ||= false
end
allow_param(resources, attributes) click to toggle source
# File lib/permitter/permission.rb, line 40
def allow_param(resources, attributes)
  @allowed_params ||= {}
  resource_array = Array(resources).flatten
  attribute_array = Array(attributes).flatten

  resource_array.each do |resource|
    @allowed_params[resource] ||= []
    @allowed_params[resource] += attribute_array
  end
end
allowed_action(controller, action) click to toggle source
# File lib/permitter/permission.rb, line 25
def allowed_action(controller, action)
  if @allowed_actions
    @allowed_actions[[controller.to_s, action.to_s]]
  end
end
allowed_action?(controller, action, resource = nil) click to toggle source
# File lib/permitter/permission.rb, line 4
def allowed_action?(controller, action, resource = nil)
  if allow_all?
    true
  elsif @allowed_actions
    allowed = @allowed_actions[[controller.to_s, action.to_s]]
    (allowed && (allowed == true || resource && allowed.call(resource))) == true
  else
    false
  end
end
allowed_param?(resource, attribute) click to toggle source
# File lib/permitter/permission.rb, line 15
def allowed_param?(resource, attribute)
  if allow_all?
    true
  elsif @allowed_params && @allowed_params[resource]
    @allowed_params[resource].include? attribute
  else
    false
  end
end
permit_params!(params) click to toggle source
# File lib/permitter/permission.rb, line 59
def permit_params!(params)
  if @allow_all
    params.permit!
  elsif @allowed_params
    @allowed_params.each do |resource, attributes|
      if params[resource].respond_to? :permit
        params[resource] = params[resource].permit(*attributes)
      end
    end
  end
end