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

DSL 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 { ... } ⇒ 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.

Yields:

  • Runs in the component's request context; may mutate @data.



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.

Yields:

  • Runs in the component's request context; may refine @data.



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.

Yields:

  • Runs in the component's request context; assigns params onto @data.

See Also:

  • Default::Standalone::VerbDsl#assign_attributes


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.

Parameters:

  • new_data_class (Class, nil) (defaults to: nil)

    If given, the model class to use (e.g. a VirtualModel subclass).

Returns:

  • (Class)

    The resolved data class.



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.

Yields:

  • Runs in the component's request context; must assign @data.

See Also:

  • Default::Standalone::VerbDsl#load_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

Returns:

  • (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).

Yields:

  • Runs in the component's request context; persists @data.

See Also:

  • Default::Standalone::VerbDsl#store_data


94
95
96
# File 'lib/compony/component_mixins/resourceful.rb', line 94

def store_data(&block)
  @global_store_data_block = block
end