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 |
# 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 find_record path.second.to_i 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? # Non sono certo che i request params gli arrivino... Domani da testare # Il body come glielo passo? @params = params create elsif request.put? # Non sono certo che i request params gli arrivino... Domani da testare # Il body come glielo passo? @params = params find_record path.second.to_i update elsif request.delete? find_record path.second.to_i destroy end end |
#create ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'app/controllers/api/v1/base_controller.rb', line 135 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
150 151 152 153 154 155 156 |
# File 'app/controllers/api/v1/base_controller.rb', line 150 def destroy unless @record.destroy return api_error(status: 500) end render json: {message: "Deleted"}, status: 200 end |
#index ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/controllers/api/v1/base_controller.rb', line 84 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) @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
125 126 127 128 |
# File 'app/controllers/api/v1/base_controller.rb', line 125 def search index render :index end |
#show ⇒ Object
130 131 132 133 |
# File 'app/controllers/api/v1/base_controller.rb', line 130 def show result = @record.to_json(json_attrs) render json: result, status: result.blank? ? 404 : 200 end |
#update ⇒ Object
144 145 146 147 148 |
# File 'app/controllers/api/v1/base_controller.rb', line 144 def update @record.update_attributes(request_params) render json: @record.to_json(json_attrs), status: 200 end |