Module: BetterController::ResourcesController
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/better_controller/resources_controller.rb
Overview
Module providing standardized RESTful resource controller functionality
Instance Method Summary collapse
-
#create ⇒ Object
Create action to create a new resource.
-
#destroy ⇒ Object
Destroy action to delete a resource.
-
#index ⇒ Object
Index action to list all resources.
-
#show ⇒ Object
Show action to display a specific resource.
-
#update ⇒ Object
Update action to update an existing resource.
Instance Method Details
#create ⇒ Object
Create action to create a new resource
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/better_controller/resources_controller.rb', line 49 def create execute_action do @resource = resource_creator if @resource.errors.any? respond_with_error(@resource.errors, status: :unprocessable_entity) else data = serialize_resource(@resource, create_serializer) respond_with_success(data, status: :created, options: { message: , meta: , }) end end end |
#destroy ⇒ Object
Destroy action to delete a resource
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/better_controller/resources_controller.rb', line 83 def destroy execute_action do @resource = resource_destroyer if @resource.errors.any? respond_with_error(@resource.errors, status: :unprocessable_entity) else data = serialize_resource(@resource, destroy_serializer) respond_with_success(data, options: { message: , meta: , }) end end end |
#index ⇒ Object
Index action to list all resources
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/better_controller/resources_controller.rb', line 20 def index execute_action do collection = resource_collection_resolver # Apply pagination if enabled if self.class.[:enabled] && collection.respond_to?(:page) @resource_collection = paginate(collection, page: params[:page], per_page: params[:per_page] || self.class.[:per_page]) else @resource_collection = collection end data = serialize_resource(@resource_collection, index_serializer) respond_with_success(data, options: { meta: }) end end |
#show ⇒ Object
Show action to display a specific resource
39 40 41 42 43 44 45 46 |
# File 'lib/better_controller/resources_controller.rb', line 39 def show execute_action do @resource = resource_finder data = serialize_resource(@resource, show_serializer) respond_with_success(data, options: { meta: }) end end |
#update ⇒ Object
Update action to update an existing resource
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/better_controller/resources_controller.rb', line 66 def update execute_action do @resource = resource_updater if @resource.errors.any? respond_with_error(@resource.errors, status: :unprocessable_entity) else data = serialize_resource(@resource, update_serializer) respond_with_success(data, options: { message: , meta: , }) end end end |