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
# File 'app/decorators/alchemy/ingredient_editor.rb', line 34

def css_classes
  [
    "ingredient-editor",
    partial_name,
    deprecated? ? "deprecated" : nil,
  ].compact
end

#data_attributesObject


42
43
44
45
46
47
# File 'app/decorators/alchemy/ingredient_editor.rb', line 42

def data_attributes
  {
    ingredient_id: id,
    ingredient_role: role,
  }
end

#deprecation_noticeObject

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.

139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/decorators/alchemy/ingredient_editor.rb', line 139

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.

Parameters:

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

    A Ingredient column_name. Default is 'value'


71
72
73
# File 'app/decorators/alchemy/ingredient_editor.rb', line 71

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 %>

63
64
65
# File 'app/decorators/alchemy/ingredient_editor.rb', line 63

def form_field_name(column = "value")
  "element[ingredients_attributes][#{form_field_counter}][#{column}]"
end

#has_warnings?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'app/decorators/alchemy/ingredient_editor.rb', line 83

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

#linked?Boolean

Returns:

  • (Boolean)

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

def linked?
  link.try(:present?)
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)

77
78
79
80
81
# File 'app/decorators/alchemy/ingredient_editor.rb', line 77

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

#warningsObject


91
92
93
94
95
96
97
98
99
100
# File 'app/decorators/alchemy/ingredient_editor.rb', line 91

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