Class: Alchemy::ElementEditor

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

Instance Method Summary collapse

Instance Method Details

#css_classesObject

CSS classes for the element editor partial.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/decorators/alchemy/element_editor.rb', line 52

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

#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.


110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/decorators/alchemy/element_editor.rb', line 110

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)


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

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

#has_ingredients_defined?Boolean

Are any ingredients defined?

Returns:

  • (Boolean)


24
25
26
# File 'app/decorators/alchemy/element_editor.rb', line 24

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



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

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

#respond_to?(*args, **kwargs) ⇒ Boolean

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

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'app/decorators/alchemy/element_editor.rb', line 73

def respond_to?(*args, **kwargs)
  method_name = args.first
  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

#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


43
44
45
46
47
48
49
# File 'app/decorators/alchemy/element_editor.rb', line 43

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