Class: Maglev::Editor::Settings::BaseComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/maglev/editor/settings/base_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:, value:, paths:, scope:) ⇒ BaseComponent

Returns a new instance of BaseComponent.



9
10
11
12
13
14
15
16
17
# File 'app/components/maglev/editor/settings/base_component.rb', line 9

def initialize(definition:, value:, paths:, scope:)
  @definition = definition
  @value = value
  @paths = paths
  @input_scope = scope[:input]
  @i18n_scope = scope[:i18n]

  after_initialize
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



7
8
9
# File 'app/components/maglev/editor/settings/base_component.rb', line 7

def definition
  @definition
end

#i18n_scopeObject (readonly)

Returns the value of attribute i18n_scope.



7
8
9
# File 'app/components/maglev/editor/settings/base_component.rb', line 7

def i18n_scope
  @i18n_scope
end

#input_scopeObject (readonly)

Returns the value of attribute input_scope.



7
8
9
# File 'app/components/maglev/editor/settings/base_component.rb', line 7

def input_scope
  @input_scope
end

#pathsObject (readonly)

Returns the value of attribute paths.



7
8
9
# File 'app/components/maglev/editor/settings/base_component.rb', line 7

def paths
  @paths
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'app/components/maglev/editor/settings/base_component.rb', line 7

def value
  @value
end

Instance Method Details

#after_initializeObject



49
50
51
# File 'app/components/maglev/editor/settings/base_component.rb', line 49

def after_initialize
  # to be overridden by subclasses
end

#callObject



39
40
41
42
# File 'app/components/maglev/editor/settings/base_component.rb', line 39

def call
  (:div, "Unknown setting type: #{definition.type} / #{definition.options}",
              class: 'bg-red-500 text-white p-4 rounded-md')
end

#default_placeholderObject



35
36
37
# File 'app/components/maglev/editor/settings/base_component.rb', line 35

def default_placeholder
  ::I18n.t("maglev.editor.settings.#{definition.type}.placeholder")
end

#fetch_path(name, context = {}) ⇒ Object



44
45
46
47
# File 'app/components/maglev/editor/settings/base_component.rb', line 44

def fetch_path(name, context = {})
  path_or_proc = paths.fetch(name, nil)
  path_or_proc.is_a?(Proc) ? path_or_proc.call(definition, context) : path_or_proc
end

#input_nameObject



19
20
21
# File 'app/components/maglev/editor/settings/base_component.rb', line 19

def input_name
  "#{input_scope}[#{definition.id}]"
end

#input_sourceObject



23
24
25
# File 'app/components/maglev/editor/settings/base_component.rb', line 23

def input_source
  input_name.parameterize.underscore
end

#labelObject



27
28
29
# File 'app/components/maglev/editor/settings/base_component.rb', line 27

def label
  ::I18n.t("#{i18n_scope}.settings.#{definition.id}", default: definition.label)
end

#placeholderObject



31
32
33
# File 'app/components/maglev/editor/settings/base_component.rb', line 31

def placeholder
  ::I18n.t("#{i18n_scope}.settings.#{definition.id}_placeholder", default: default_placeholder)
end