Class: Dry::Ability::Controller::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/ability/controller/resource.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(resource) ⇒ Object



91
92
93
94
95
96
97
# 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_actionObject



105
106
107
# File 'lib/dry/ability/controller/resource.rb', line 105

def authorization_action
  parent? ? @mediator.parent_action : @action_name
end

#authorize_resourceObject



51
52
53
54
# 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

#callObject



28
29
30
31
32
33
34
35
# 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_instanceObject



143
144
145
# File 'lib/dry/ability/controller/resource.rb', line 143

def collection_instance
  @controller.instance_variable_get(:"@#{collection_name}")
end

#collection_instance=(instance) ⇒ Object



139
140
141
# File 'lib/dry/ability/controller/resource.rb', line 139

def collection_instance=(instance)
  @controller.instance_variable_set(:"@#{collection_name}", instance)
end

#current_abilityObject



164
165
166
# File 'lib/dry/ability/controller/resource.rb', line 164

def current_ability
  @controller.send(:current_ability)
end

#id_paramObject



109
110
111
# 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_attributesObject



99
100
101
102
103
# 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_resourceObject



37
38
39
40
# File 'lib/dry/ability/controller/resource.rb', line 37

def load_and_authorize_resource
  load_resource
  authorize_resource
end

#load_collectionObject

def load_collection?

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

end



85
86
87
88
89
# 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?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/dry/ability/controller/resource.rb', line 76

def load_instance?
  parent? || member_action?
end

#load_resourceObject



42
43
44
45
46
47
48
49
# 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_instanceObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/dry/ability/controller/resource.rb', line 68

def load_resource_instance
  raise NotImplementedError
end

#parameters_require_sanitizing?Boolean

Returns:

  • (Boolean)


183
184
185
# 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_methodObject



199
200
201
202
203
204
# 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?Boolean

Returns:

  • (Boolean)


56
57
58
# 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_nameObject



147
148
149
150
# 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_resourceObject

The object to load this resource through.



153
154
155
156
157
158
159
160
161
162
# 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_baseObject

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/dry/ability/controller/resource.rb', line 72

def resource_base
  raise NotImplementedError
end

#resource_classObject

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.



116
117
118
119
120
121
122
123
124
125
# 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_parentObject



127
128
129
# File 'lib/dry/ability/controller/resource.rb', line 127

def resource_class_with_parent
  parent_resource ? { parent_resource => resource_class } : resource_class
end

#resource_instanceObject



135
136
137
# 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) ⇒ Object



131
132
133
# File 'lib/dry/ability/controller/resource.rb', line 131

def resource_instance=(instance)
  @controller.instance_variable_set(:"@#{instance_name}", instance)
end

#resource_paramsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 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_nameObject



187
188
189
190
191
192
193
194
195
196
197
# 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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# 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