Class: ActiveRecordApi::Rest::Controller
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ActiveRecordApi::Rest::Controller
- Includes:
- GracefulErrors
- Defined in:
- lib/active_record_api/rest/controller.rb
Direct Known Subclasses
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #destroy_by ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/active_record_api/rest/controller.rb', line 25 def create if model.save render json: model, serializer: serializer else render json: model.errors, status: :unprocessable_entity end end |
#destroy ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/active_record_api/rest/controller.rb', line 41 def destroy if model.destroy render json: model else render json: model.errors, status: :unprocessable_entity end end |
#destroy_by ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record_api/rest/controller.rb', line 49 def destroy_by found_model = .find_by(queryable_params) if !found_model head :not_found elsif found_model.destroy render json: found_model else render json: found_model.errors, status: :unprocessable_entity end end |
#index ⇒ Object
10 11 12 13 14 |
# File 'lib/active_record_api/rest/controller.rb', line 10 def index response.headers['x-total'] = models_count response.headers['x-link-next'] = next_url unless next_url.nil? render json: models_remaining.limit(limit), each_serializer: serializer end |
#new ⇒ Object
20 21 22 23 |
# File 'lib/active_record_api/rest/controller.rb', line 20 def new attributes = params - [:action] render json: .send(params[:action], attributes), serializer: serializer end |
#show ⇒ Object
16 17 18 |
# File 'lib/active_record_api/rest/controller.rb', line 16 def show render json: model, serializer: serializer end |
#update ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/active_record_api/rest/controller.rb', line 33 def update if model.update(modifiable_params) render json: model, serializer: serializer else render json: model.errors, status: :unprocessable_entity end end |