Class: Alchemy::ElementEditor

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/decorators/alchemy/element_editor.rb

Instance Method Summary collapse

Instance Method Details

#contentsObject

Returns content editor instances for defined contents

Creates contents on demand if the content is not yet present on the element


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

def contents
  element.definition.fetch(:contents, []).map do |content|
    Alchemy::ContentEditor.new(find_or_create_content(content[:name]))
  end
end

#css_classesObject

CSS classes for the element editor partial.


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/decorators/alchemy/element_editor.rb', line 40

def css_classes
  [
    "element-editor",
    content_definitions.present? ? "with-contents" : "without-contents",
    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? ? "visible" : "hidden",
  ].join(" ")
end

#deprecation_noticeObject

Returns a deprecation notice for elements marked deprecated

You can either use localizations or pass a String as notice in the element definition.

Custom deprecation notices

Use general element deprecation notice

- name: old_element
  deprecated: true

Add a translation to your locale file for a per element notice.

en:
  alchemy:
    element_deprecation_notices:
      old_element: Foo baz widget is deprecated

or use the global translation that apply to all deprecated elements.

en:
  alchemy:
    element_deprecation_notice: Foo baz widget is deprecated

or pass string as deprecation notice.

- name: old_element
  deprecated: This element will be removed soon.

99
100
101
102
103
104
105
106
107
108
# File 'app/decorators/alchemy/element_editor.rb', line 99

def deprecation_notice
  case definition["deprecated"]
  when String
    definition["deprecated"]
  when TrueClass
    Alchemy.t(name,
              scope: :element_deprecation_notices,
              default: Alchemy.t(:element_deprecated))
  end
end

#editable?Boolean

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

Returns:

  • (Boolean)

55
56
57
58
59
# File 'app/decorators/alchemy/element_editor.rb', line 55

def editable?
  return false if folded?

  content_definitions.present? || ingredient_definitions.any? || taggable?
end

#has_ingredients_defined?Boolean

Are any ingredients defined?

Returns:

  • (Boolean)

35
36
37
# File 'app/decorators/alchemy/element_editor.rb', line 35

def has_ingredients_defined?
  element.definition.fetch(:ingredients, []).any?
end

#ingredientsObject

Returns ingredient editor instances for defined ingredients

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


27
28
29
30
31
# File 'app/decorators/alchemy/element_editor.rb', line 27

def ingredients
  element.definition.fetch(:ingredients, []).map do |ingredient|
    Alchemy::IngredientEditor.new(find_or_create_ingredient(ingredient))
  end
end

#respond_to?(method_name) ⇒ Boolean

Fixes Rails partial renderer calling to_model on the object which reveals the delegated element instead of this decorator.

Returns:

  • (Boolean)

63
64
65
66
67
# File 'app/decorators/alchemy/element_editor.rb', line 63

def respond_to?(method_name)
  return false if method_name == :to_model

  super
end

#to_partial_pathObject


7
8
9
# File 'app/decorators/alchemy/element_editor.rb', line 7

def to_partial_path
  "alchemy/admin/elements/element"
end