Class: DashApi::ApiController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dash_api/api_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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)
  authorize resource, :create? 
  render json: { 
    data: DashApi::Serializer.render(resource) 
  }    
end

#delete_manyObject



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])
  authorize resources, :destroy? 
  resources.destroy_all
  render json: { data: DashApi::Serializer.render(resources) }
end

#destroyObject



64
65
66
67
68
69
# File 'app/controllers/dash_api/api_controller.rb', line 64

def destroy  
  resource = dash_scope.find(params[:id])
  authorize resource, :destroy? 
  resource.destroy 
  render json: { data: DashApi::Serializer.render(resource) }    
end

#indexObject



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

  authorize 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

#showObject



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])
  authorize resource, :show? 
  render json: { 
    data: DashApi::Serializer.render(resource, includes: @includes) 
  }
end

#updateObject



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])
  authorize resource, :update? 
  if resource.update(dash_params)
    render json: {           
      data: DashApi::Serializer.render(resource)
    }
  else 
    render json: { error: resource.errors.full_messages }, status: 422
  end 
end

#update_manyObject



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])
  authorize resources, :update? 
  resources.update(dash_params)
  render json: { data: DashApi::Serializer.render(resources) }
end