Class: Motor::DashboardsController

Inherits:
ApiBaseController show all
Defined in:
app/controllers/motor/dashboards_controller.rb

Instance Method Summary collapse

Methods included from CurrentAbility

#current_ability

Methods included from CurrentUserMethod

#current_user

Instance Method Details

#createObject


20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/motor/dashboards_controller.rb', line 20

def create
  if Motor::Dashboards::Persistance.title_already_exists?(@dashboard)
    render json: { errors: [{ source: 'title', detail: 'Title already exists' }] }, status: :unprocessable_entity
  else
    ApplicationRecord.transaction { @dashboard.save! }
    Motor::Configs::WriteToFile.call

    render json: { data: Motor::ApiQuery::BuildJson.call(@dashboard, params, current_ability) }
  end
rescue ActiveRecord::RecordNotUnique
  retry
end

#destroyObject


42
43
44
45
46
47
48
# File 'app/controllers/motor/dashboards_controller.rb', line 42

def destroy
  @dashboard.update!(deleted_at: Time.current)

  Motor::Configs::WriteToFile.call

  head :ok
end

#indexObject


12
13
14
# File 'app/controllers/motor/dashboards_controller.rb', line 12

def index
  render json: { data: Motor::ApiQuery::BuildJson.call(@dashboards.active, params, current_ability) }
end

#showObject


16
17
18
# File 'app/controllers/motor/dashboards_controller.rb', line 16

def show
  render json: { data: Motor::ApiQuery::BuildJson.call(@dashboard, params, current_ability) }
end

#updateObject


33
34
35
36
37
38
39
40
# File 'app/controllers/motor/dashboards_controller.rb', line 33

def update
  Motor::Dashboards::Persistance.update_from_params!(@dashboard, dashboard_params)
  Motor::Configs::WriteToFile.call

  render json: { data: Motor::ApiQuery::BuildJson.call(@dashboard, params, current_ability) }
rescue Motor::Dashboards::Persistance::TitleAlreadyExists
  render json: { errors: [{ source: 'title', detail: 'Title already exists' }] }, status: :unprocessable_entity
end