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


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/alchemy/element/element_essences.rb', line 91

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



27
28
29
30
31
32
33
34
35
# File 'app/models/alchemy/element/element_essences.rb', line 27

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)


12
13
14
# File 'app/models/alchemy/element/element_essences.rb', line 12

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

#ingredient(name) ⇒ Object

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



4
5
6
7
8
# File 'app/models/alchemy/element/element_essences.rb', line 4

def ingredient(name)
  content = content_by_name(name)
  return nil if content.blank?
  content.ingredient
end