Class: Motor::DataController
Constant Summary
LoadAndAuthorizeDynamicResource::ASSOCIATION_INSTANCE_VARIABLE_NAME, LoadAndAuthorizeDynamicResource::INSTANCE_VARIABLE_NAME
Instance Method Summary
collapse
#load_and_authorize_association, #load_and_authorize_resource, #resource_class, #resource_name_prefix
#current_ability
#current_user
Instance Method Details
#create ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'app/controllers/motor/data_controller.rb', line 27
def create
if @associated_resource
if @resource_class.reflections[params[:association]].through_reflection?
@associated_resource.save!
@resource = @associated_resource
else
@resource.public_send(params[:association].to_sym).create!(@associated_resource.attributes) do |resource|
@resource = resource
end
end
else
@resource.save!
end
render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params, current_ability) }
rescue ActiveRecord::RecordInvalid
render json: { errors: @resource.errors }, status: :unprocessable_entity
end
|
#destroy ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'app/controllers/motor/data_controller.rb', line 55
def destroy
if @resource.respond_to?(:deleted_at)
@resource.update(deleted_at: Time.current)
else
@resource.destroy!
end
head :ok
end
|
#execute ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'app/controllers/motor/data_controller.rb', line 65
def execute
resource_preferences = Motor::Resource.find_by(name: @resource.class.name.underscore).preferences
resource_action = resource_preferences[:actions].find { |a| a[:preferences][:method_name] == params[:method] }
authorize!(resource_action[:name].to_sym, @resource)
@resource.public_send(params[:method].to_sym)
head :ok
end
|
#show ⇒ Object
23
24
25
|
# File 'app/controllers/motor/data_controller.rb', line 23
def show
render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params, current_ability) }
end
|
#update ⇒ Object
47
48
49
50
51
52
53
|
# File 'app/controllers/motor/data_controller.rb', line 47
def update
@resource.update!(resource_params)
render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params, current_ability) }
rescue ActiveRecord::RecordInvalid
render json: { errors: @resource.errors }, status: :unprocessable_entity
end
|