Class: DashApi::ApiController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- DashApi::ApiController
- Defined in:
- app/controllers/dash_api/api_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #delete_many ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
- #update_many ⇒ Object
Instance Method Details
#create ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/controllers/dash_api/api_controller.rb', line 44 def create resource = dash_scope.create!(dash_params) resource, :create? render json: { data: DashApi::Serializer.render(resource) } end |
#delete_many ⇒ Object
78 79 80 81 82 83 |
# File 'app/controllers/dash_api/api_controller.rb', line 78 def delete_many resources = dash_scope.where(id: params[:ids]) resources, :destroy? resources.destroy_all render json: { data: DashApi::Serializer.render(resources) } end |
#destroy ⇒ Object
64 65 66 67 68 69 |
# File 'app/controllers/dash_api/api_controller.rb', line 64 def destroy resource = dash_scope.find(params[:id]) resource, :destroy? resource.destroy render json: { data: DashApi::Serializer.render(resource) } end |
#index ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/controllers/dash_api/api_controller.rb', line 9 def index resources = dash_scope resources, :index? @filters.each{|filter| resources = resources.where(filter) } resources = resources.pg_search(@keywords) if @keywords.present? resources = resources.order(@order) if @order.present? resources = resources.select(@select) if @select.present? if @stats.present? statistic, value = @stats.keys[0], @stats.values[0]&.to_sym resources = resources.send(statistic, value) else resources = resources.page(@page).per(@per_page) end render json: { data: DashApi::Serializer.render(resources, includes: @includes), meta: { page: @page, per_page: @per_page, total_count: resources.respond_to?(:total_count) ? resources.total_count : 1 } } end |
#show ⇒ Object
36 37 38 39 40 41 42 |
# File 'app/controllers/dash_api/api_controller.rb', line 36 def show resource = dash_scope.find(params[:id]) resource, :show? render json: { data: DashApi::Serializer.render(resource, includes: @includes) } end |
#update ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/dash_api/api_controller.rb', line 52 def update resource = dash_scope.find(params[:id]) resource, :update? if resource.update(dash_params) render json: { data: DashApi::Serializer.render(resource) } else render json: { error: resource.errors. }, status: 422 end end |
#update_many ⇒ Object
71 72 73 74 75 76 |
# File 'app/controllers/dash_api/api_controller.rb', line 71 def update_many resources = dash_scope.where(id: params[:ids]) resources, :update? resources.update(dash_params) render json: { data: DashApi::Serializer.render(resources) } end |