Module: Compony::ComponentMixins::Resourceful

Extended by:
ActiveSupport::Concern
Included in:
Compony::Components::Destroy, Compony::Components::Edit, Compony::Components::New
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

Instance Method Summary collapse

Instance Attribute Details

#dataObject (readonly)



9
10
11
# File 'lib/compony/component_mixins/resourceful.rb', line 9

def data
  @data
end

#global_after_assign_attributes_blockObject (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_blockObject (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_blockObject (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_blockObject (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_blockObject (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(&block) ⇒ Object (protected)

DSL method Runs after assign_attributes and before store_data for all standalone paths and verbs. Example use case: prefilling some fields for a form



77
78
79
# File 'lib/compony/component_mixins/resourceful.rb', line 77

def after_assign_attributes(&block)
  @global_after_assign_attributes_block = block
end

#after_load_data(&block) ⇒ Object (protected)

DSL method Runs after loading data and before authorization for all standalone paths and verbs. Example use case: if load_data produced an AR collection proxy, can still refine result here before to_sql is called.



60
61
62
# File 'lib/compony/component_mixins/resourceful.rb', line 60

def after_load_data(&block)
  @global_after_load_data_block = block
end

#assign_attributes(&block) ⇒ Object (protected)

DSL method Sets a default default assign_attributes block for all standalone paths and verbs. Can be overwritten for a specific path and verb in the Default::Standalone::VerbDsl. The block is expected to assign suitable params to attributes of @data.

See Also:

  • Default::Standalone::VerbDsl#assign_attributes


70
71
72
# File 'lib/compony/component_mixins/resourceful.rb', line 70

def assign_attributes(&block)
  @global_assign_attributes_block = block
end

#data_class(new_data_class = nil) ⇒ Object

DSL method Sets or calculates the model class based on the component's family name



30
31
32
# File 'lib/compony/component_mixins/resourceful.rb', line 30

def data_class(new_data_class = nil)
  @data_class ||= new_data_class || family_cst.to_s.singularize.constantize
end

#initialize(*args, data: nil, data_class: nil, **nargs, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/compony/component_mixins/resourceful.rb', line 18

def initialize(*args, data: nil, data_class: nil, **nargs, &block)
  @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(*args, **nargs, &block)
end

#load_data(&block) ⇒ Object (protected)

DSL method Sets a default load_data block for all standalone paths and verbs. Can be overwritten for a specific path and verb in the Default::Standalone::VerbDsl. The block is expected to assign @data.

See Also:

  • Default::Standalone::VerbDsl#load_data


53
54
55
# File 'lib/compony/component_mixins/resourceful.rb', line 53

def load_data(&block)
  @global_load_data_block = block
end

#resourceful?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/compony/component_mixins/resourceful.rb', line 41

def resourceful?
  return true
end

#resourceful_sub_comp(component_class, **comp_opts) ⇒ Object

Instanciate a component with self as a parent and render it, having it inherit the resource



35
36
37
38
39
# File 'lib/compony/component_mixins/resourceful.rb', line 35

def resourceful_sub_comp(component_class, **comp_opts)
  comp_opts[:data] ||= data # Inject additional param before forwarding all of them to super
  comp_opts[:data_class] ||= data_class # Inject additional param before forwarding all of them to super
  sub_comp(component_class, **comp_opts)
end

#store_data(&block) ⇒ Object (protected)

DSL method Sets a default store_data block for all standalone paths and verbs. Can be overwritten for a specific path and verb in the Default::Standalone::VerbDsl. The block is expected save @data to the database.

See Also:

  • Default::Standalone::VerbDsl#store_data


87
88
89
# File 'lib/compony/component_mixins/resourceful.rb', line 87

def store_data(&block)
  @global_store_data_block = block
end