Class: Motor::DataController

Inherits:
ApiBaseController show all
Includes:
LoadAndAuthorizeDynamicResource, WrapIoParams
Defined in:
app/controllers/motor/data_controller.rb

Constant Summary

Constants included from LoadAndAuthorizeDynamicResource

LoadAndAuthorizeDynamicResource::ASSOCIATION_INSTANCE_VARIABLE_NAME, LoadAndAuthorizeDynamicResource::INSTANCE_VARIABLE_NAME

Instance Method Summary collapse

Methods included from LoadAndAuthorizeDynamicResource

#load_and_authorize_association, #load_and_authorize_resource, #resource_class, #resource_name_prefix

Methods included from CurrentAbility

#current_ability

Methods included from CurrentUserMethod

#current_user

Instance Method Details

#createObject


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

#destroyObject


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

#executeObject


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

#indexObject


10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/motor/data_controller.rb', line 10

def index
  @resources = Motor::ApiQuery.call(@resources, params)

  if params[:format] == 'csv'
    render plain: Motor::ActiveRecordUtils.generate_csv_for_relation(@resources, reset_limit: true)
  else
    render json: {
      data: Motor::ApiQuery::BuildJson.call(@resources, params, current_ability),
      meta: Motor::ApiQuery::BuildMeta.call(@resources, params)
    }
  end
end

#showObject


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

#updateObject


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