Class: Maglev::BaseComponent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/components/maglev/base_component.rb

Direct Known Subclasses

BlockComponent, PageComponent, SectionComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#view_contextObject

Returns the value of attribute view_context.



8
9
10
# File 'app/components/maglev/base_component.rb', line 8

def view_context
  @view_context
end

Instance Method Details

#build(component_class, attributes) ⇒ Object



10
11
12
13
14
# File 'app/components/maglev/base_component.rb', line 10

def build(component_class, attributes)
  component_class.new(**attributes).tap do |component|
    component.view_context = view_context
  end
end

#build_content(scope, content, setting) ⇒ Object



38
39
40
# File 'app/components/maglev/base_component.rb', line 38

def build_content(scope, content, setting)
  Maglev::Content::Builder.build(scope, content, setting)
end

#build_settings_map(settings) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'app/components/maglev/base_component.rb', line 26

def build_settings_map(settings)
  definition.settings.inject({}) do |memo, setting_def|
    memo.merge(
      setting_def.id.to_sym => build_content(
        self,
        find_setting_value(settings, setting_def),
        setting_def
      )
    )
  end
end

#settings_proxy(map) ⇒ Object

Useful helper to avoid manipulating hashes eg.: <%= section.settings.body %> instead of <%= section.settings %>



18
19
20
21
22
23
24
# File 'app/components/maglev/base_component.rb', line 18

def settings_proxy(map)
  settings_keys = map.keys
  return nil if settings_keys.blank?

  settings_struct = Struct.new(*settings_keys)
  settings_struct.new(*map.values)
end