Module: Alchemy::Element::ElementEssences

Included in:
Alchemy::Element
Defined in:
app/models/alchemy/element/element_essences.rb

Instance Method Summary collapse

Instance Method Details

#essence_error_messagesObject

Essence validation errors

Error messages are translated via I18n

Inside your translation file add translations like:

alchemy:
  content_validations:
    name_of_the_element:
      name_of_the_content:
        validation_error_type: Error Message

NOTE: validation_error_type has to be one of:

* blank
* taken
* invalid

Example:

de:
  alchemy:
    content_validations:
      contactform:
        email:
          invalid: 'Die Email hat nicht das richtige Format'

Error message translation fallbacks

In order to not translate every single content for every element you can provide default error messages per content name:

Example

en:
  alchemy:
    content_validations:
      fields:
        email:
          invalid: E-Mail has wrong format
          blank: E-Mail can't be blank

And even further you can provide general field agnostic error messages:

Example

en:
  alchemy:
    content_validations:
      errors:
        invalid: %{field} has wrong format
        blank: %{field} can't be blank


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/alchemy/element/element_essences.rb', line 106

def essence_error_messages
  messages = []
  essence_errors.each do |content_name, errors|
    errors.each do |error|
      messages << Alchemy.t(
        "#{name}.#{content_name}.#{error}",
        scope: "content_validations",
        default: [
          "fields.#{content_name}.#{error}".to_sym,
          "errors.#{error}".to_sym,
        ],
        field: Content.translated_label_for(content_name, name),
      )
    end
  end
  messages
end

#essence_errorsObject

Returns all essence errors in the format of:

{
  content.name => [
    error_message_for_validation_1,
    error_message_for_validation_2
  ]
}

Get translated error messages with Element#essence_error_messages



42
43
44
45
46
47
48
49
50
# File 'app/models/alchemy/element/element_essences.rb', line 42

def essence_errors
  essence_errors = {}
  contents.each do |content|
    if content.essence_validation_failed?
      essence_errors[content.name] = content.essence.validation_errors
    end
  end
  essence_errors
end

#has_ingredient?(name) ⇒ Boolean

True if the element has a content for given name, that has an essence value (aka. ingredient) that is not blank.

Returns:

  • (Boolean)


26
27
28
# File 'app/models/alchemy/element/element_essences.rb', line 26

def has_ingredient?(name)
  ingredient(name).present?
end

#ingredient(name) ⇒ Object

Returns the contents essence value (aka. ingredient) for passed content name.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/alchemy/element/element_essences.rb', line 7

def ingredient(name)
  ing = ingredient_by_role(name)
  if ing
    Alchemy::Deprecation.warn <<~WARN
      Using `element.ingredient` to get the value of an ingredient is deprecated and will change in Alchemy 6.1
      If you want to read the value of an elements ingredient please use `element.value_for(:ingredient_role)` instead.
      The next version of Alchemy will return a `Alchemy::Ingredient` record instead.
    WARN
    ing.value
  else
    content = content_by_name(name)
    return nil if content.blank?

    content.ingredient
  end
end