Module: Wallaby::Resourcable
- Included in:
- ResourcesConcern, ResourcesHelper
- Defined in:
- lib/concerns/wallaby/resourcable.rb
Overview
Resources related attributes & helper methods
Instance Method Summary collapse
-
#collection(params: self.params, paginate: true, **paginate_options) {|collection| ... } ⇒ #each
(also: #collection!)
This is a method to return collection for index action and page.
-
#create_params ⇒ ActionController::Parameters
To allowlist the request params for create action.
-
#current_model_class ⇒ Class
Model class for current request.
-
#current_resources_name ⇒ String
Resources name for current request.
- #new_resource {|resource| ... } ⇒ Object (also: #new_resource!)
- #resource {|resource| ... } ⇒ Object (also: #resource!)
-
#resource_id ⇒ String?
Shorthand of params.
-
#resource_params ⇒ ActionController::Parameters
It’s used by #create_params and #update_params.
-
#update_params ⇒ ActionController::Parameters
To allowlist the request params for update action.
Instance Method Details
#collection(params: self.params, paginate: true, **paginate_options) {|collection| ... } ⇒ #each Also known as: collection!
This is a template method that can be overridden by subclasses.
This is a method to return collection for index action and page.
It can be customized as below in subclasses:
def collection
# do something before the original action
= {} # @see arguments for more details
# NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
collection!() do |query|
# NOTE: make sure to return a collection in the block
query.where(active: true)
end
end
Otherwise, it can be replaced completely in subclasses:
def collection
# NOTE: pagination should happen here if needed
# NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
@collection ||= paginate Product.active
end
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/concerns/wallaby/resourcable.rb', line 99 def collection(params: self.params, paginate: true, **, &block) @collection ||= ModuleUtils.yield_for( paginate( current_servicer.collection(params), .merge(paginate: paginate) ), &block ) end |
#create_params ⇒ ActionController::Parameters
This is a template method that can be overridden by subclasses.
To allowlist the request params for create action.
It can be replaced completely in subclasses:
def create_params
params.fetch(:product, {}).permit(:name, :sku)
end
54 55 56 |
# File 'lib/concerns/wallaby/resourcable.rb', line 54 def create_params resource_params end |
#current_model_class ⇒ Class
Model class for current request.
It comes from two places:
-
configured .model_class
-
fall back to the model class converted from either the #current_resources_name or .controller_path
20 21 22 23 |
# File 'lib/concerns/wallaby/resourcable.rb', line 20 def current_model_class @current_model_class ||= wallaby_controller.model_class || Map.model_class_map(current_resources_name || controller_path) end |
#current_resources_name ⇒ String
Returns resources name for current request.
7 8 9 |
# File 'lib/concerns/wallaby/resourcable.rb', line 7 def current_resources_name @current_resources_name ||= params[:resources] end |
#new_resource {|resource| ... } ⇒ Object Also known as: new_resource!
This is a template method that can be overridden by subclasses.
This is a method to return resource for new and create actions.
It can be customized as below in subclasses:
def new_resource
# do something before the original action
# NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
new_resource! do |object|
object.preload_status_from_api
# NOTE: make sure to return an object in the block
object
end
end
Otherwise, it can be replaced completely in subclasses:
def new_resource
# NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
@resource ||= Product.new(arrival: true)
end
170 171 172 173 |
# File 'lib/concerns/wallaby/resourcable.rb', line 170 def new_resource(&block) @resource ||= ModuleUtils.yield_for(current_servicer.new({}), &block) end |
#resource {|resource| ... } ⇒ Object Also known as: resource!
This is a template method that can be overridden by subclasses.
This is a method to return resource for show, edit, update and destroy actions.
It can be customized as below in subclasses:
def resource
# do something before the original action
# NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
resource! do |object|
object.preload_status_from_api
# NOTE: make sure to return an object in the block
object
end
end
Otherwise, it can be replaced completely in subclasses:
def resource
# NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
@resource ||= resource_id.present? ? Product.find_by_slug(resource_id) : Product.new(arrival: true)
end
138 139 140 141 |
# File 'lib/concerns/wallaby/resourcable.rb', line 138 def resource(&block) @resource ||= ModuleUtils.yield_for(current_servicer.find(resource_id, {}), &block) end |
#resource_id ⇒ String?
Shorthand of params
27 28 29 |
# File 'lib/concerns/wallaby/resourcable.rb', line 27 def resource_id params[:id] end |
#resource_params ⇒ ActionController::Parameters
This is a template method that can be overridden by subclasses.
It’s used by #create_params and #update_params.
It can be replaced completely in subclasses and will impact both #create_params and #update_params:
def resource_params
params.fetch(:product, {}).permit(:name, :sku)
end
41 42 43 |
# File 'lib/concerns/wallaby/resourcable.rb', line 41 def resource_params @resource_params ||= current_servicer.permit params, action_name end |
#update_params ⇒ ActionController::Parameters
This is a template method that can be overridden by subclasses.
To allowlist the request params for update action.
It can be replaced completely in subclasses:
def update_params
params.fetch(:product, {}).permit(:name, :sku)
end
67 68 69 |
# File 'lib/concerns/wallaby/resourcable.rb', line 67 def update_params resource_params end |