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 NameError, with: :name_error! rescue_from NoMethodError, with: :no_method_error! rescue_from ::RubySpark::Device::ApiError, with: :fivehundred!.
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 NameError, with: :name_error! rescue_from NoMethodError, with: :no_method_error! rescue_from ::RubySpark::Device::ApiError, with: :fivehundred!
36 37 38 |
# File 'app/controllers/api/v1/base_controller.rb', line 36 def current_user @current_user end |
Instance Method Details
#check ⇒ Object
Disabling Strong Parameters def params
request.parameters
end
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/controllers/api/v1/base_controller.rb', line 45 def check # This method is only valid for ActiveRecords # For any other model-less controller, the actions must be # defined in the route, and must exist in the controller definition. # So, if it's not an activerecord, the find model makes no sense at all # Thus must return 404 path = params[:path].split("/") # puts "CHECK" return not_found! if (!path.first.classify.constantize.new.is_a? ActiveRecord::Base rescue false) 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 a singleton method) # GET :controller/:custom_action # puts "SECOND ZERO?" return not_found! unless @model.respond_to?(path.second) return render json: MultiJson.dump(@model.send(path.second, params)), status: 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? # GET :controller/:id/:custom_action # puts "SECOND AND THIRD" return not_found! unless @model.respond_to?(path.third) return render json: MultiJson.dump(@model.send(path.third, path.second.to_i, params)), status: 200 end elsif request.post? if path.second.blank? @params = params create elsif path.second.to_i.zero? # POST :controller/:custom_action # puts "NO SECOND" return not_found! unless @model.respond_to?(path.second) return render json: MultiJson.dump(@model.send(path.second, params)), status: 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? # PUT :controller/:id/:custom_action # puts "ANOTHER SECOND AND THIRD" return not_found! unless @model.respond_to?(path.third) return render json: MultiJson.dump(@model.send(path.third, path.second.to_i, params)), status: 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
170 171 172 173 174 175 176 177 |
# File 'app/controllers/api/v1/base_controller.rb', line 170 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
185 186 187 188 189 |
# File 'app/controllers/api/v1/base_controller.rb', line 185 def destroy return api_error(status: 500) unless @record.destroy # render json: {message: "Deleted"}, status: 200 head :ok end |
#index ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/controllers/api/v1/base_controller.rb', line 115 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? # puts "ALL RECORDS FOUND: #{@records_all.inspect}" status = @records_all.blank? ? 404 : 200 # puts "If it's asked for page number, then paginate" return render json: MultiJson.dump(@records, json_attrs), status: status if !page.blank? # (@json_attrs || {}) #puts "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? #puts "Default" json_out = MultiJson.dump(@records_all, json_attrs) #puts "JSON ATTRS: #{json_attrs}" #puts "JSON OUT: #{json_out}" render json: json_out, 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
160 161 162 163 |
# File 'app/controllers/api/v1/base_controller.rb', line 160 def search index render :index end |
#show ⇒ Object
165 166 167 168 |
# File 'app/controllers/api/v1/base_controller.rb', line 165 def show result = @record.to_json(json_attrs) render json: result, status: 200 end |
#update ⇒ Object
179 180 181 182 183 |
# File 'app/controllers/api/v1/base_controller.rb', line 179 def update @record.update_attributes!(request_params) render json: @record.to_json(json_attrs), status: 200 end |