Module: Caprese::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- Controller
- Defined in:
- lib/caprese/controller/concerns/persistence.rb
Instance Method Summary collapse
-
#create ⇒ Object
Creates a new record of whatever type a given controller manages.
-
#destroy ⇒ Object
Destroys a record of whatever type a given controller manages.
-
#update ⇒ Object
Updates a record of whatever type a given controller manages.
Instance Method Details
#create ⇒ Object
For this action to succeed, the given controller must define ‘create_params` @see #create_params
Creates a new record of whatever type a given controller manages
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/caprese/controller/concerns/persistence.rb', line 39 def create fail_on_type_mismatch(data_params[:type]) record = queried_record_scope.build assign_changes_from_document(record, data_params, permitted_params_for(:create)) execute_after_initialize_callbacks(record) execute_before_create_callbacks(record) execute_before_save_callbacks(record) fail RecordInvalidError.new(record, engaged_field_aliases) if record.errors.any? record.save! persist_collection_relationships(record) execute_after_create_callbacks(record) execute_after_save_callbacks(record) render( json: record, status: :created, fields: query_params[:fields], include: query_params[:include] ) end |
#destroy ⇒ Object
Destroys a record of whatever type a given controller manages
-
Execute any before_destroy callbacks, with the record to be destroyed passed in
-
Destroy the record, ensuring that it checks the model for dependencies before doing so
-
Execute any after_destroy callbacks, with the destroyed resource passed in
-
Return 204 No Content if the record was successfully deleted
99 100 101 102 103 104 105 |
# File 'lib/caprese/controller/concerns/persistence.rb', line 99 def destroy execute_before_destroy_callbacks(queried_record) queried_record.destroy! execute_after_destroy_callbacks(queried_record) head :no_content end |
#update ⇒ Object
For this action to succeed, the given controller must define ‘update_params` @see #update_params
Updates a record of whatever type a given controller manages
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/caprese/controller/concerns/persistence.rb', line 71 def update fail_on_type_mismatch(data_params[:type]) assign_changes_from_document(queried_record, data_params, permitted_params_for(:update)) execute_before_update_callbacks(queried_record) execute_before_save_callbacks(queried_record) fail RecordInvalidError.new(queried_record, engaged_field_aliases) if queried_record.errors.any? queried_record.save! execute_after_update_callbacks(queried_record) execute_after_save_callbacks(queried_record) render( json: queried_record, fields: query_params[:fields], include: query_params[:include] ) end |