Class: Alchemy::Admin::ElementEditor

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/alchemy/admin/element_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element:, created: false, parent_element: nil) ⇒ ElementEditor

Returns a new instance of ElementEditor.



16
17
18
19
20
# File 'app/components/alchemy/admin/element_editor.rb', line 16

def initialize(element:, created: false, parent_element: nil)
  @element = element
  @created = created
  @parent_element = parent_element
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



8
9
10
# File 'app/components/alchemy/admin/element_editor.rb', line 8

def created
  @created
end

#elementObject (readonly)

Returns the value of attribute element.



8
9
10
# File 'app/components/alchemy/admin/element_editor.rb', line 8

def element
  @element
end

#parent_elementObject (readonly)

Returns the value of attribute parent_element.



8
9
10
# File 'app/components/alchemy/admin/element_editor.rb', line 8

def parent_element
  @parent_element
end

Instance Method Details

#css_classesObject

CSS classes for the element editor.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/components/alchemy/admin/element_editor.rb', line 23

def css_classes
  [
    "element-editor",
    ingredient_definitions.present? ? "with-ingredients" : "without-ingredients",
    nestable_elements.any? ? "nestable" : "not-nestable",
    taggable? ? "taggable" : "not-taggable",
    folded? ? "folded" : "expanded",
    compact? ? "compact" : nil,
    deprecated? ? "deprecated" : nil,
    fixed? ? "is-fixed" : "not-fixed",
    public? ? nil : "element-hidden"
  ]
end

#display_nameObject



94
95
96
97
98
# File 'app/components/alchemy/admin/element_editor.rb', line 94

def display_name
  parent_element ?
    "#{parent_element.display_name} > #{element.display_name}"
    : element.display_name
end

#editable?Boolean

Tells us, if we should show the element footer and form inputs.

Returns:

  • (Boolean)


38
39
40
# File 'app/components/alchemy/admin/element_editor.rb', line 38

def editable?
  ingredient_definitions.any? || taggable?
end

#filter_textObject



100
101
102
# File 'app/components/alchemy/admin/element_editor.rb', line 100

def filter_text
  [display_name, element.preview_text].compact_blank.join(" ")
end

#grouped_ingredientsHash<String, Array<Alchemy::Ingredient>>

Returns ingredients grouped by their group name

Returns:



67
68
69
# File 'app/components/alchemy/admin/element_editor.rb', line 67

def grouped_ingredients
  ingredients.select { _1.definition.group }.group_by { _1.definition.group }
end

#has_ingredients_defined?Boolean

Are any ingredients defined?

Returns:

  • (Boolean)


44
45
46
# File 'app/components/alchemy/admin/element_editor.rb', line 44

def has_ingredients_defined?
  ingredient_definitions.any?
end

#ingredientsObject

Returns ingredient instances for defined ingredients

Creates ingredient on demand if the ingredient is not yet present on the element



53
54
55
56
57
# File 'app/components/alchemy/admin/element_editor.rb', line 53

def ingredients
  ingredient_definitions.map do |ingredient|
    find_or_create_ingredient(ingredient)
  end
end

#translated_group(group) ⇒ Object

Returns the translated ingredient group for displaying in admin editor group headings

Translate it in your locale yml file:

alchemy:
  element_groups:
    foo: Bar

Optionally you can scope your ingredient role to an element:

alchemy:
  element_groups:
    article:
      foo: Baz


86
87
88
89
90
91
92
# File 'app/components/alchemy/admin/element_editor.rb', line 86

def translated_group(group)
  Alchemy.t(
    group,
    scope: "element_groups.#{element.name}",
    default: Alchemy.t("element_groups.#{group}", default: group.humanize)
  )
end

#ungrouped_ingredientsObject

Returns ingredients that are not part of any group



60
61
62
# File 'app/components/alchemy/admin/element_editor.rb', line 60

def ungrouped_ingredients
  ingredients.reject { _1.definition.group }
end