Class: Alchemy::IngredientEditor

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

Instance Method Summary collapse

Instance Method Details

#css_classesObject



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) && level_options.any?) ? "with-level-select" : nil,
    (respond_to?(:size_options) && size_options.many?) ? "with-size-select" : nil,
    settings[:linkable] ? "linkable" : nil,
    settings[:anchor] ? "with-anchor" : nil
  ].compact
end

#data_attributesObject



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

#form_field_id(column = "value") ⇒ Object

Returns a unique string to be passed to a form field id.

Parameters:

  • column (String) (defaults to: "value")

    A Ingredient column_name. Default is ‘value’



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_validationObject



110
111
112
# File 'app/decorators/alchemy/ingredient_editor.rb', line 110

def format_validation
  validations.select { _1.is_a?(Hash) }.find { _1[:format] }&.fetch(:format)
end

#has_warnings?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/decorators/alchemy/ingredient_editor.rb', line 87

def has_warnings?
  definition.blank? || deprecated?
end

#length_validationObject



114
115
116
# File 'app/decorators/alchemy/ingredient_editor.rb', line 114

def length_validation
  validations.select { _1.is_a?(Hash) }.find { _1[:length] }&.fetch(:length)
end

#linked?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/decorators/alchemy/ingredient_editor.rb', line 91

def linked?
  link.try(:present?)
end

#presence_validation?Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'app/decorators/alchemy/ingredient_editor.rb', line 118

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.

Returns:

  • (Boolean)


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_pathObject



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

def to_partial_path
  "alchemy/ingredients/#{partial_name}_editor"
end

#translated_roleObject

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

#validationsObject



106
107
108
# File 'app/decorators/alchemy/ingredient_editor.rb', line 106

def validations
  definition.validate
end

#warningsObject



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
    definition.deprecation_notice(element_name: element&.name)
  end
end