Module: Compony::ComponentMixins::Resourceful
- Extended by:
- ActiveSupport::Concern
- Included in:
- Compony::Components::Destroy, Compony::Components::Edit, Compony::Components::Index, Compony::Components::List, Compony::Components::New, Compony::Components::Show
- Defined in:
- lib/compony/component_mixins/resourceful.rb
Overview
Include this when your component's family name corresponds to the pluralized Rails model name the component's family is responsible for.
When including this, the component gets an attribute @data which contains a record or a collection of records.
Resourceful components are always aware of a data_class, corresponding to the expected @data.class and used e.g. to render lists or for .new.
Instance Attribute Summary collapse
- #data ⇒ Object readonly
- #global_after_assign_attributes_block ⇒ Object readonly
- #global_after_load_data_block ⇒ Object readonly
- #global_assign_attributes_block ⇒ Object readonly
-
#global_load_data_block ⇒ Object
readonly
Must prefix the following instance variables with global_ in order to avoid overwriting VerbDsl inst vars due to Dslblend.
- #global_store_data_block ⇒ Object readonly
DSL collapse
-
#after_assign_attributes { ... } ⇒ void
protected
DSL method Runs after
assign_attributesand beforestore_datafor all standalone paths and verbs. -
#after_load_data { ... } ⇒ void
protected
DSL method Runs after
load_dataand before authorization for all standalone paths and verbs. -
#assign_attributes { ... } ⇒ void
protected
DSL method Sets the default
assign_attributesblock for all standalone paths and verbs (overridable per verb in the VerbDsl). -
#data_class(new_data_class = nil) ⇒ Class
DSL method Sets or calculates the model class.
-
#load_data { ... } ⇒ void
protected
DSL method Sets the default
load_datablock for all standalone paths and verbs (overridable per verb in the VerbDsl). - #resourceful? ⇒ Boolean
-
#store_data { ... } ⇒ void
protected
DSL method Sets the default
store_datablock for all standalone paths and verbs (overridable per verb in the VerbDsl).
Instance Method Summary collapse
Instance Attribute Details
#data ⇒ Object (readonly)
9 10 11 |
# File 'lib/compony/component_mixins/resourceful.rb', line 9 def data @data end |
#global_after_assign_attributes_block ⇒ Object (readonly)
15 16 17 |
# File 'lib/compony/component_mixins/resourceful.rb', line 15 def global_after_assign_attributes_block @global_after_assign_attributes_block end |
#global_after_load_data_block ⇒ Object (readonly)
13 14 15 |
# File 'lib/compony/component_mixins/resourceful.rb', line 13 def global_after_load_data_block @global_after_load_data_block end |
#global_assign_attributes_block ⇒ Object (readonly)
14 15 16 |
# File 'lib/compony/component_mixins/resourceful.rb', line 14 def global_assign_attributes_block @global_assign_attributes_block end |
#global_load_data_block ⇒ Object (readonly)
Must prefix the following instance variables with global_ in order to avoid overwriting VerbDsl inst vars due to Dslblend.
12 13 14 |
# File 'lib/compony/component_mixins/resourceful.rb', line 12 def global_load_data_block @global_load_data_block end |
#global_store_data_block ⇒ Object (readonly)
16 17 18 |
# File 'lib/compony/component_mixins/resourceful.rb', line 16 def global_store_data_block @global_store_data_block end |
Instance Method Details
#after_assign_attributes { ... } ⇒ void (protected)
This method returns an undefined value.
DSL method
Runs after assign_attributes and before store_data for all standalone paths and verbs.
Example: prefill or derive fields before validation.
83 84 85 |
# File 'lib/compony/component_mixins/resourceful.rb', line 83 def after_assign_attributes(&block) @global_after_assign_attributes_block = block end |
#after_load_data { ... } ⇒ void (protected)
This method returns an undefined value.
DSL method
Runs after load_data and before authorization for all standalone paths and verbs.
Example: refine an AR collection produced by load_data before it is read.
62 63 64 |
# File 'lib/compony/component_mixins/resourceful.rb', line 62 def after_load_data(&block) @global_after_load_data_block = block end |
#assign_attributes { ... } ⇒ void (protected)
This method returns an undefined value.
DSL method
Sets the default assign_attributes block for all standalone paths and verbs (overridable per verb in the VerbDsl).
The block is expected to assign validated params to attributes of @data.
73 74 75 |
# File 'lib/compony/component_mixins/resourceful.rb', line 73 def assign_attributes(&block) @global_assign_attributes_block = block end |
#data_class(new_data_class = nil) ⇒ Class
DSL method Sets or calculates the model class. Defaults to the component's family name, singularized and constantized.
35 36 37 |
# File 'lib/compony/component_mixins/resourceful.rb', line 35 def data_class(new_data_class = nil) @data_class ||= new_data_class || family_name.singularize.camelize.constantize end |
#initialize(data: nil, data_class: nil, **nargs) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/compony/component_mixins/resourceful.rb', line 18 def initialize(*, data: nil, data_class: nil, **nargs, &) @data = data @data_class = data_class # Provide defaults for hook blocks @global_load_data_block ||= proc { @data = self.data_class.find(controller.params[:id]) } super(*, **nargs, &) end |
#load_data { ... } ⇒ void (protected)
This method returns an undefined value.
DSL method
Sets the default load_data block for all standalone paths and verbs (overridable per verb in the VerbDsl).
Runs before authorization. The block is expected to assign @data.
52 53 54 |
# File 'lib/compony/component_mixins/resourceful.rb', line 52 def load_data(&block) @global_load_data_block = block end |
#resourceful? ⇒ Boolean
39 40 41 |
# File 'lib/compony/component_mixins/resourceful.rb', line 39 def resourceful? return true end |
#store_data { ... } ⇒ void (protected)
This method returns an undefined value.
DSL method
Sets the default store_data block for all standalone paths and verbs (overridable per verb in the VerbDsl).
The block is expected to persist @data (override e.g. for virtual models or custom persistence).
94 95 96 |
# File 'lib/compony/component_mixins/resourceful.rb', line 94 def store_data(&block) @global_store_data_block = block end |