Class: Wallaby::AbstractResourcesController
- Inherits:
-
BaseController
- Object
- ApplicationController
- SecureController
- BaseController
- Wallaby::AbstractResourcesController
- Includes:
- ResourcesHelperMethods
- Defined in:
- app/controllers/wallaby/abstract_resources_controller.rb
Overview
Generic CRUD controller
Direct Known Subclasses
Constant Summary
Constants inherited from ApplicationController
Wallaby::ApplicationController::ERROR_PATH
Class Method Summary collapse
-
.model_class ⇒ Class
Model class for controller.
-
.resources_name ⇒ String
Resources name for controller.
Instance Method Summary collapse
-
#create ⇒ Object
Resourceful create action to create a record.
-
#current_model_service ⇒ Wallaby::ModelServicer
Model servicer associated to current modal class.
-
#destroy ⇒ Object
Resourceful destroy action to delete a record.
-
#edit ⇒ Object
Resourceful edit action to show a form for editing a record.
-
#home ⇒ Object
Home page.
-
#index ⇒ Object
Resourceful action to list paginated records.
-
#new ⇒ Object
Resourceful new action to show a form for creating a record.
-
#resource_params ⇒ ActionController::Parameters
To whitelist the params for CRUD actions.
-
#show ⇒ Object
Resourceful show action to display values for a record.
-
#update ⇒ Object
Resourceful update action to update a record.
Methods inherited from BaseController
#current_model_class, #current_resources_name
Methods inherited from SecureController
#current_user, #forbidden, #security_config, #unauthorized
Methods inherited from ApplicationController
#bad_request, #configuration, configuration, #healthy, #not_found, #unprocessable_entity
Class Method Details
.model_class ⇒ Class
Returns model class for controller.
21 22 23 24 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 21 def model_class return unless self < configuration.mapping.resources_controller Map.model_class_map resources_name end |
.resources_name ⇒ String
Returns resources name for controller.
15 16 17 18 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 15 def resources_name return unless self < configuration.mapping.resources_controller Map.resources_name_map name.gsub('Controller', EMPTY_STRING) end |
Instance Method Details
#create ⇒ Object
Resourceful create action to create a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘new` is run
def create # do something beforehand super end
-
To perform action after ‘create`, but before rendering.
The reason is that we use responder at the end of the action
def create
super do
# do something afterwards
end
end
-
To perform completely different action
def create # do something completely different end
125 126 127 128 129 130 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 125 def create :create, resource current_model_service.create resource, params yield if block_given? # after_create respond_with resource, location: helpers.show_path(resource) end |
#current_model_service ⇒ Wallaby::ModelServicer
Model servicer associated to current modal class.
This model servicer will take care of all the CRUD operations
For how to override model service, see (Wallaby::ModelServicer)
264 265 266 267 268 269 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 264 def current_model_service @current_model_service ||= begin model_class = current_model_class Map.servicer_map(model_class).new model_class, end end |
#destroy ⇒ Object
Resourceful destroy action to delete a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘destroy` is run
def destroy # do something beforehand super end
-
To perform action after ‘destroy`, but before rendering.
The reason is that we use responder at the end of the action
def destroy
super do
# do something afterwards
end
end
-
To perform completely different action
def destroy # do something completely different end
251 252 253 254 255 256 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 251 def destroy :destroy, resource current_model_service.destroy resource, params yield if block_given? # after_destroy respond_with resource, location: helpers.index_path(current_model_class) end |
#edit ⇒ Object
Resourceful edit action to show a form for editing a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘edit` is run
def edit # do something beforehand super end
-
To perform action after ‘edit`, but before rendering.
The reason is that we use responder at the end of the action
def edit
super do
# do something afterwards
end
end
-
To perform completely different action
def edit # do something completely different end
188 189 190 191 192 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 188 def edit :edit, resource yield if block_given? # after_edit respond_with resource end |
#home ⇒ Object
Home page
You can completely override this action:
def home
# do something differently
end
34 35 36 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 34 def home # do nothing end |
#index ⇒ Object
Resourceful action to list paginated records.
You can override this method in subclasses in the following ways:
-
To perform action before ‘index` is run
def index # do something beforehand super end
-
To perform action after ‘index`, but before rendering.
The reason is that we use responder at the end of the action
def index
super do
# do something afterwards
end
end
-
To perform completely different action
def index # do something completely different end
63 64 65 66 67 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 63 def index :index, current_model_class yield if block_given? # after_index respond_with collection end |
#new ⇒ Object
Resourceful new action to show a form for creating a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘new` is run
def new # do something beforehand super end
-
To perform action after ‘new`, but before rendering.
The reason is that we use responder at the end of the action
def new
super do
# do something afterwards
end
end
-
To perform completely different action
def new # do something completely different end
94 95 96 97 98 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 94 def new :new, resource yield if block_given? # after_new respond_with resource end |
#resource_params ⇒ ActionController::Parameters
To whitelist the params for CRUD actions
274 275 276 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 274 def resource_params @resource_params ||= current_model_service.permit params end |
#show ⇒ Object
Resourceful show action to display values for a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘show` is run
def show # do something beforehand super end
-
To perform action after ‘show`, but before rendering.
The reason is that we use responder at the end of the action
def show
super do
# do something afterwards
end
end
-
To perform completely different action
def show # do something completely different end
157 158 159 160 161 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 157 def show :show, resource yield if block_given? # after_show respond_with resource end |
#update ⇒ Object
Resourceful update action to update a record.
You can override this method in subclasses in the following ways:
-
To perform action before ‘update` is run
def update # do something beforehand super end
-
To perform action after ‘update`, but before rendering.
The reason is that we use responder at the end of the action
def update
super do
# do something afterwards
end
end
-
To perform completely different action
def update # do something completely different end
219 220 221 222 223 224 |
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 219 def update :update, resource current_model_service.update resource, params yield if block_given? # after_update respond_with resource, location: helpers.show_path(resource) end |