Class: Api::V2::ApplicationController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::V2::ApplicationController
- Includes:
- ActiveHashRelation, ApiExceptionManagement, CanCan::ControllerAdditions
- Defined in:
- app/controllers/api/v2/application_controller.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_user ⇒ Object
Returns the value of attribute current_user.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
-
#index ⇒ Object
GET :controller/.
-
#params ⇒ Object
Nullifying strong params for API.
- #show ⇒ Object
- #update ⇒ Object
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
8 9 10 |
# File 'app/controllers/api/v2/application_controller.rb', line 8 def current_user @current_user end |
Instance Method Details
#create ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'app/controllers/api/v2/application_controller.rb', line 127 def create @record = @model.new(@body) :create, @record @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
144 145 146 147 148 |
# File 'app/controllers/api/v2/application_controller.rb', line 144 def destroy :destroy, @record return api_error(status: 500) unless @record.destroy head :ok end |
#index ⇒ Object
GET :controller/
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/controllers/api/v2/application_controller.rb', line 92 def index :index, @model # 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]) # Paging @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 |
#params ⇒ Object
Nullifying strong params for API
16 17 18 |
# File 'app/controllers/api/v2/application_controller.rb', line 16 def params request.parameters end |
#show ⇒ Object
121 122 123 124 125 |
# File 'app/controllers/api/v2/application_controller.rb', line 121 def show :show, @record result = @record.to_json(json_attrs) render json: result, status: 200 end |
#update ⇒ Object
137 138 139 140 141 142 |
# File 'app/controllers/api/v2/application_controller.rb', line 137 def update :update, @record @record.update_attributes!(@body) render json: @record.to_json(json_attrs), status: 200 end |