Class: Api::V1::BaseController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::V1::BaseController
- Includes:
- ActiveHashRelation, CanCan::ControllerAdditions
- Defined in:
- app/controllers/api/v1/base_controller.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_user ⇒ Object
rescue_from Pundit::NotAuthorizedError, with: :unauthorized!.
Instance Method Summary collapse
-
#check ⇒ Object
Disabling Strong Parameters def params request.parameters end.
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
-
#search ⇒ Object
def count # find the records @q = (@model.column_names.include?(“user_id”) ? @model.where(user_id: current_user.id) : @model).ransack(params) @records_all = @q.result(distinct: true) # if you ask for count, then return a json object with just the number of objects return render json: @records_all.count.to_json end.
- #show ⇒ Object
- #update ⇒ Object
Instance Attribute Details
#current_user ⇒ Object
rescue_from Pundit::NotAuthorizedError, with: :unauthorized!
35 36 37 |
# File 'app/controllers/api/v1/base_controller.rb', line 35 def current_user @current_user end |
Instance Method Details
#check ⇒ Object
Disabling Strong Parameters def params
request.parameters
end
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/controllers/api/v1/base_controller.rb', line 42 def check path = params[:path].split("/") find_model path.first if request.get? if path.second.blank? @page = params[:page] @per = params[:per] @pages_info = params[:pages_info] @count = params[:count] @query = params[:q] index elsif path.second.to_i.zero? # String, so it's a custom action I must find in the @model (as an singleton method) # Like: :controller/:custom_action result = MultiJson.dump(@model.send(path.second, params)) return render json: result, status: result.blank? ? 404 : 200 elsif !path.second.to_i.zero? && path.third.blank? # Integer, so it's an ID, I must show it # Rails.logger.debug "IL SECONDO è ID? #{path.second.inspect}" # find_record path.second.to_i @record_id = path.second.to_i find_record show elsif !path.second.to_i.zero? && !path.third.blank? # Like :controller/:id/:custom_action result = MultiJson.dump(@model.send(path.third, path.second.to_i, params)) return render json: result, status: result.blank? ? 404 : 200 end elsif request.post? if path.second.blank? @params = params create elsif path.second.to_i.zero? result = MultiJson.dump(@model.send(path.second, params)) return render json: result, status: result.blank? ? 404 : 200 end elsif request.put? if !path.second.to_i.zero? && path.third.blank? @params = params # Rails.logger.debug "IL SECONDO è ID in PUT? #{path.second.inspect}" # find_record path.second.to_i @record_id = path.second.to_i find_record update elsif !path.second.to_i.zero? && !path.third.blank? result = MultiJson.dump(@model.send(path.third, path.second.to_i, params)) return render json: result, status: result.blank? ? 404 : 200 end elsif request.delete? # Rails.logger.debug "IL SECONDO è ID in delete? #{path.second.inspect}" # find_record path.second.to_i @record_id = path.second.to_i find_record destroy end end |
#create ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'app/controllers/api/v1/base_controller.rb', line 150 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
165 166 167 168 169 170 171 |
# File 'app/controllers/api/v1/base_controller.rb', line 165 def destroy unless @record.destroy return api_error(status: 500) end render json: {message: "Deleted"}, status: 200 end |
#index ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/controllers/api/v1/base_controller.rb', line 99 def index # Rails.logger.debug params.inspect # find the records @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q]) @records_all = @q.result(distinct: true) page = (@page.presence || params[:page]) per = (@per.presence || params[:per]) pages_info = (@pages_info.presence || params[:pages_info]) count = (@count.presence || params[:count]) @records = @records_all.page(page).per(per) # If there's the keyword pagination_info, then return a pagination info object 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 !pages_info.blank? status = @records_all.blank? ? 404 : 200 # If it's asked for page number, then paginate return render json: MultiJson.dump(@records, json_attrs), status: status if !page.blank? # (@json_attrs || {}) # if you ask for count, then return a json object with just the number of objects return render json: MultiJson.dump({count: @records_all.count}) if !count.blank? # Default render json: MultiJson.dump(@records_all, json_attrs), status: status #(@json_attrs || {}) end |
#search ⇒ Object
def count
# find the records
@q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(params[:q])
@records_all = @q.result(distinct: true)
# if you ask for count, then return a json object with just the number of objects
return render json: {count: @records_all.count}.to_json
end
140 141 142 143 |
# File 'app/controllers/api/v1/base_controller.rb', line 140 def search index render :index end |
#show ⇒ Object
145 146 147 148 |
# File 'app/controllers/api/v1/base_controller.rb', line 145 def show result = @record.to_json(json_attrs) render json: result, status: result.blank? ? 404 : 200 end |
#update ⇒ Object
159 160 161 162 163 |
# File 'app/controllers/api/v1/base_controller.rb', line 159 def update @record.update_attributes(request_params) render json: @record.to_json(json_attrs), status: 200 end |