class Dry::Ability::Controller::Resource

@private

Public Instance Methods

assign_attributes(resource) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 91
def assign_attributes(resource)
  resource.send(:"#{parent_name}=", parent_resource) if singleton? && parent_resource
  initial_attributes.each do |attr_name, value|
    resource.send(:"#{attr_name}=", value)
  end
  resource
end
authorization_action() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 105
def authorization_action
  parent? ? @mediator.parent_action : @action_name
end
authorize_resource() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 51
def authorize_resource
  return if skip?(:authorize)
  @controller.authorize!(authorization_action, resource_instance || resource_class_with_parent)
end
call() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 28
def call
  @controller.instance_variable_set(:@_ability_resource, self)
  retval = nil
  @mediator.sequence.each do |sym|
    retval = public_send(sym)
  end
  retval
end
collection_instance() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 143
def collection_instance
  @controller.instance_variable_get(:"@#{collection_name}")
end
collection_instance=(instance) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 139
def collection_instance=(instance)
  @controller.instance_variable_set(:"@#{collection_name}", instance)
end
current_ability() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 164
def current_ability
  @controller.send(:current_ability)
end
id_param() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 109
def id_param
  params[@mediator.id_param_key] if params.key?(@mediator.id_param_key)
end
initial_attributes() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 99
def initial_attributes
  current_ability.attributes_for(@action_name, resource_class).delete_if do |key, value|
    (resource_params && resource_params.include?(key)) || value.is_a?(Hash)
  end
end
load_and_authorize_resource() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 37
def load_and_authorize_resource
  load_resource
  authorize_resource
end
load_collection() click to toggle source

def load_collection?

collection_action?
# current_ability.has_scope?(authorization_action, resource_class) || resource_base.respond_to?(:accessible_by)

end

# File lib/dry/ability/controller/resource.rb, line 85
def load_collection
  current_ability.scope_for(authorization_action, resource_class) do
    resource_base.accessible_by(current_ability, authorization_action)
  end
end
load_instance?() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 76
def load_instance?
  parent? || member_action?
end
load_resource() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 42
def load_resource
  return if skip?(:load)
  if load_instance?
    self.resource_instance ||= load_resource_instance
  elsif collection_action?
    self.collection_instance ||= load_collection
  end
end
load_resource_instance() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 68
def load_resource_instance
  raise NotImplementedError
end
parameters_require_sanitizing?() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 183
def parameters_require_sanitizing?
  @mediator.save_actions.include?(@action_name) || resource_params_by_namespaced_name.present?
end
params_method() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 199
def params_method
  @params_method ||= @mediator.params_method || begin
    [:"#{@action_name}_params", :"#{name}_params", :resource_params].
      detect { |method| @controller.respond_to?(method, true) }
  end
end
parent?() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 56
def parent?
  @mediator.parent.nil? ? @mediator.collection_name != controller_name.to_sym : @mediator.parent?
end
parent_name() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 147
def parent_name
  return @parent_name if defined?(@parent_name)
  @parent_name = @mediator.through unless parent_resource.nil?
end
parent_resource() click to toggle source

The object to load this resource through.

# File lib/dry/ability/controller/resource.rb, line 153
def parent_resource
  return @parent_resource if defined?(@parent_resource)
  @parent_resource = if @mediator.through
    if @controller.instance_variable_defined? :"@#{@mediator.through}"
      @controller.instance_variable_get(:"@#{@mediator.through}")
    elsif @controller.respond_to?(@mediator.through, true)
      @controller.send(@mediator.through)
    end
  end
end
resource_base() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 72
def resource_base
  raise NotImplementedError
end
resource_class() click to toggle source

Returns the class used for this resource. This can be overriden by the :class option. If false is passed in it will use the resource name as a symbol in which case it should only be used for authorization, not loading since there's no class to load through.

# File lib/dry/ability/controller/resource.rb, line 116
def resource_class
  case class_name
  when false then
    name.to_sym
  when String then
    class_name.constantize
  else
    raise ArgumentError, "unexpected class_name: #{class_name}"
  end
end
resource_class_with_parent() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 127
def resource_class_with_parent
  parent_resource ? { parent_resource => resource_class } : resource_class
end
resource_instance() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 135
def resource_instance
  @controller.instance_variable_get(:"@#{instance_name}") if load_instance?
end
resource_instance=(instance) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 131
def resource_instance=(instance)
  @controller.instance_variable_set(:"@#{instance_name}", instance)
end
resource_params() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 168
def resource_params
  if parameters_require_sanitizing? && params_method.present?
    case params_method
    when Symbol then
      @controller.send(params_method)
    when String then
      @controller.instance_eval(params_method)
    when Proc then
      params_method.call(@controller)
    end
  else
    resource_params_by_namespaced_name
  end
end
resource_params_by_namespaced_name() click to toggle source
# File lib/dry/ability/controller/resource.rb, line 187
def resource_params_by_namespaced_name
  return @resource_params_by_namespaced_name if defined?(@resource_params_by_namespaced_name)
  @resource_params_by_namespaced_name =
    if params.key?(@mediator.instance_name)
      params[@mediator.instance_name]
    elsif params.key?(key = extract_key(@mediator.class_name))
      params[key]
    else
      params[name]
    end
end
skip?(behavior) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 60
def skip?(behavior)
  options = @controller.class.cancan_skipper.dig(behavior, name)
  return false if options.nil?
  options.blank? &&
    options[:except] && !action_exists_in?(options[:except]) ||
    action_exists_in?(options[:only])
end

Private Instance Methods

action_exists_in?(options) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 208
def action_exists_in?(options)
  Array.wrap(options).include?(@controller.action_name.to_sym)
end
extract_key(value) click to toggle source
# File lib/dry/ability/controller/resource.rb, line 212
def extract_key(value)
  value.to_s.underscore.tr(?/, ?_)
end