Class: Alchemy::ElementEditor
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Alchemy::ElementEditor
- Defined in:
- app/decorators/alchemy/element_editor.rb
Instance Method Summary collapse
-
#contents ⇒ Object
Returns content editor instances for defined contents.
-
#css_classes ⇒ Object
CSS classes for the element editor partial.
-
#deprecation_notice ⇒ Object
Returns a deprecation notice for elements marked deprecated.
-
#editable? ⇒ Boolean
Tells us, if we should show the element footer and form inputs.
-
#has_ingredients_defined? ⇒ Boolean
Are any ingredients defined?.
-
#ingredients ⇒ Object
Returns ingredient editor instances for defined ingredients.
-
#respond_to?(method_name) ⇒ Boolean
Fixes Rails partial renderer calling to_model on the object which reveals the delegated element instead of this decorator.
- #to_partial_path ⇒ Object
-
#translated_group(group) ⇒ Object
Returns the translated content/ingredient group for displaying in admin editor group headings.
Instance Method Details
#contents ⇒ Object
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_classes ⇒ Object
CSS classes for the element editor partial.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/decorators/alchemy/element_editor.rb', line 63 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_notice ⇒ Object
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.
122 123 124 125 126 127 128 129 130 131 |
# File 'app/decorators/alchemy/element_editor.rb', line 122 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.
78 79 80 81 82 |
# File 'app/decorators/alchemy/element_editor.rb', line 78 def editable? return false if folded? content_definitions.present? || ingredient_definitions.any? || taggable? end |
#has_ingredients_defined? ⇒ Boolean
Are any ingredients defined?
35 36 37 |
# File 'app/decorators/alchemy/element_editor.rb', line 35 def has_ingredients_defined? element.definition.fetch(:ingredients, []).any? end |
#ingredients ⇒ Object
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.
86 87 88 89 90 |
# File 'app/decorators/alchemy/element_editor.rb', line 86 def respond_to?(method_name) return false if method_name == :to_model super end |
#to_partial_path ⇒ Object
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 content/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
54 55 56 57 58 59 60 |
# File 'app/decorators/alchemy/element_editor.rb', line 54 def translated_group(group) Alchemy.t( group, scope: "element_groups.#{element.name}", default: Alchemy.t("element_groups.#{group}", default: group.humanize), ) end |