Class: Api::V1::BaseController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::V1::BaseController
show all
- Includes:
- ActiveHashRelation, CanCan::ControllerAdditions
- Defined in:
- app/controllers/api/v1/base_controller.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#current_user ⇒ Object
rescue_from Pundit::NotAuthorizedError, with: :unauthorized!
27
28
29
|
# File 'app/controllers/api/v1/base_controller.rb', line 27
def current_user
@current_user
end
|
Instance Method Details
#create ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'app/controllers/api/v1/base_controller.rb', line 72
def create
@record = @model.new(request_params)
@record.user_id = current_user.id if @model.column_names.include? "user_id"
@record.save!
render json: @record.to_json(@json_attrs || {}), status: 201
end
|
#destroy ⇒ Object
87
88
89
90
91
92
93
|
# File 'app/controllers/api/v1/base_controller.rb', line 87
def destroy
unless @record.destroy
return api_error(status: 500)
end
head status: 204
end
|
#index ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'app/controllers/api/v1/base_controller.rb', line 29
def index
@q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
@records_all = @q.result(distinct: true)
@records = @records_all.page(params[:page]).per(params[:per])
return render json: MultiJson.dump({
count: @records_all.count,
current_page_count: @records.count,
next_page: @records.next_page,
prev_page: @records.prev_page,
is_first_page: @records.first_page?,
is_last_page: @records.last_page?,
is_out_of_range: @records.out_of_range?,
pages_count: @records.total_pages,
current_page_number: @records.current_page
}) if !params[:pages_info].nil?
return render json: MultiJson.dump(@records, @json_attrs || {}) if !params[:page].nil? return render json: MultiJson.dump({count: @records_all.count}) if !params[:count].nil?
render json: MultiJson.dump(@records_all, @json_attrs || {}) end
|
#search ⇒ Object
def count
@q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
@records_all = @q.result(distinct: true)
return render json: {count: @records_all.count}.to_json
end
63
64
65
66
|
# File 'app/controllers/api/v1/base_controller.rb', line 63
def search
index
render :index
end
|
#show ⇒ Object
68
69
70
|
# File 'app/controllers/api/v1/base_controller.rb', line 68
def show
render json: @record.to_json(@json_attrs || {}), status: 200
end
|
#update ⇒ Object
81
82
83
84
85
|
# File 'app/controllers/api/v1/base_controller.rb', line 81
def update
@record.update_attributes(request_params)
render json: @record.to_json(@json_attrs || {}), status: 200
end
|