Module: Motor::LoadAndAuthorizeDynamicResource

Extended by:
ActiveSupport::Concern
Included in:
DataController
Defined in:
app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb

Constant Summary collapse

INSTANCE_VARIABLE_NAME =
'resource'
ASSOCIATION_INSTANCE_VARIABLE_NAME =
'associated_resource'

Instance Method Summary collapse

Instance Method Details

#load_and_authorize_associationObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb', line 49

def load_and_authorize_association
  return if params[:association].blank?

  association = resource_class.reflections[params[:association]]

  if association
    CanCan::ControllerResource.new(
      self,
      class: association.klass,
      parent: false,
      through: :resource,
      through_association: params[:association].to_sym,
      instance_name: params[:action] == 'create' ? ASSOCIATION_INSTANCE_VARIABLE_NAME : INSTANCE_VARIABLE_NAME
    ).load_and_authorize_resource
  else
    render json: { message: 'Unknown association' }, status: :not_found
  end
rescue ActiveRecord::RecordNotFound
  head :not_found
end

#load_and_authorize_resourceObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb', line 27

def load_and_authorize_resource
  options = {
    class: resource_class,
    parent: params[:association].present?,
    instance_name: INSTANCE_VARIABLE_NAME
  }

  if params[:resource_id].present?
    options = options.merge(
      parent: true,
      id_param: :resource_id
    )
  end

  CanCan::ControllerResource.new(
    self,
    options
  ).load_and_authorize_resource
rescue ActiveRecord::RecordNotFound
  head :not_found
end

#resource_classObject



15
16
17
18
19
20
21
# File 'app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb', line 15

def resource_class
  @resource_class ||=
    Motor::Resources::FetchConfiguredModel.call(
      Motor::BuildSchema::Utils.classify_slug(resource_name_prefix + params[:resource]),
      cache_key: Motor::Resource.maximum(:updated_at)
    )
end

#resource_name_prefixObject



23
24
25
# File 'app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb', line 23

def resource_name_prefix
  ''
end