Class: Alchemy::IngredientEditor
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Alchemy::IngredientEditor
- Defined in:
- app/decorators/alchemy/ingredient_editor.rb
Instance Method Summary collapse
- #css_classes ⇒ Object
- #data_attributes ⇒ Object
-
#deprecation_notice ⇒ Object
Returns a deprecation notice for ingredients marked deprecated.
-
#form_field_id(column = "value") ⇒ Object
Returns a unique string to be passed to a form field id.
-
#form_field_name(column = "value") ⇒ Object
Returns a string to be passed to Rails form field tags to ensure it can be used with Rails’ nested attributes.
- #format_validation ⇒ Object
- #has_warnings? ⇒ Boolean
- #length_validation ⇒ Object
- #linked? ⇒ Boolean
- #presence_validation? ⇒ Boolean
-
#respond_to?(method_name) ⇒ Boolean
Fixes Rails partial renderer calling to_model on the object which reveals the delegated ingredient instead of this decorator.
- #to_partial_path ⇒ Object
-
#translated_role ⇒ Object
Returns the translated role for displaying in labels.
- #validations ⇒ Object
- #warnings ⇒ Object
Instance Method Details
#css_classes ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 34 def css_classes [ "ingredient-editor", partial_name, deprecated? ? "deprecated" : nil, (respond_to?(:level_options) && .any?) ? "with-level-select" : nil, (respond_to?(:size_options) && .many?) ? "with-size-select" : nil, settings[:linkable] ? "linkable" : nil, settings[:anchor] ? "with-anchor" : nil ].compact end |
#data_attributes ⇒ Object
46 47 48 49 50 51 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 46 def data_attributes { ingredient_id: id, ingredient_role: role } end |
#deprecation_notice ⇒ Object
Returns a deprecation notice for ingredients marked deprecated
You can either use localizations or pass a String as notice in the ingredient definition.
Custom deprecation notices
Use general ingredient deprecation notice
- name: element_name
ingredients:
- role: old_ingredient
type: Text
deprecated: true
Add a translation to your locale file for a per ingredient notice.
en:
alchemy:
ingredient_deprecation_notices:
element_name:
old_ingredient: Foo baz widget is deprecated
or use the global translation that apply to all deprecated ingredients.
en:
alchemy:
ingredient_deprecation_notice: Foo baz widget is deprecated
or pass string as deprecation notice.
- name: element_name
ingredients:
- role: old_ingredient
type: Text
deprecated: This ingredient will be removed soon.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 143 def deprecation_notice case definition[:deprecated] when String definition[:deprecated] when TrueClass Alchemy.t( role, scope: [:ingredient_deprecation_notices, element.name], default: Alchemy.t(:ingredient_deprecated) ) end end |
#form_field_id(column = "value") ⇒ Object
Returns a unique string to be passed to a form field id.
75 76 77 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 75 def form_field_id(column = "value") "element_#{element.id}_ingredient_#{id}_#{column}" end |
#form_field_name(column = "value") ⇒ Object
Returns a string to be passed to Rails form field tags to ensure it can be used with Rails’ nested attributes.
Example:
<%= text_field_tag text_editor.form_field_name, text_editor.value %>
Options:
You can pass an Ingredient column_name. Default is ‘value’
Example:
<%= text_field_tag text_editor.form_field_name(:link), text_editor.value %>
67 68 69 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 67 def form_field_name(column = "value") "element[ingredients_attributes][#{form_field_counter}][#{column}]" end |
#format_validation ⇒ Object
160 161 162 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 160 def format_validation validations.select { _1.is_a?(Hash) }.find { _1[:format] }&.fetch(:format) end |
#has_warnings? ⇒ Boolean
87 88 89 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 87 def has_warnings? definition.blank? || deprecated? end |
#length_validation ⇒ Object
164 165 166 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 164 def length_validation validations.select { _1.is_a?(Hash) }.find { _1[:length] }&.fetch(:length) end |
#linked? ⇒ Boolean
91 92 93 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 91 def linked? link.try(:present?) end |
#presence_validation? ⇒ Boolean
168 169 170 171 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 168 def presence_validation? validations.include?("presence") || validations.any? { _1.is_a?(Hash) && _1[:presence] == true } end |
#respond_to?(method_name) ⇒ Boolean
Fixes Rails partial renderer calling to_model on the object which reveals the delegated ingredient instead of this decorator.
81 82 83 84 85 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 81 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/ingredient_editor.rb', line 7 def to_partial_path "alchemy/ingredients/#{partial_name}_editor" end |
#translated_role ⇒ Object
Returns the translated role for displaying in labels
Translate it in your locale yml file:
alchemy:
ingredient_roles:
foo: Bar
Optionally you can scope your ingredient role to an element:
alchemy:
ingredient_roles:
article:
foo: Baz
26 27 28 29 30 31 32 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 26 def translated_role Alchemy.t( role, scope: "ingredient_roles.#{element.name}", default: Alchemy.t("ingredient_roles.#{role}", default: role.humanize) ) end |
#validations ⇒ Object
156 157 158 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 156 def validations definition.fetch(:validate, []) end |
#warnings ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'app/decorators/alchemy/ingredient_editor.rb', line 95 def warnings return unless has_warnings? if definition.blank? Logger.warn("ingredient #{role} is missing its definition", caller(1..1)) Alchemy.t(:ingredient_definition_missing) else deprecation_notice end end |