Module: Alchemy::Essence::InstanceMethods

Defined in:
lib/alchemy/essence.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_essence?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/alchemy/essence.rb', line 231

def acts_as_essence?
  acts_as_essence_class.present?
end

#definitionObject

Essence definition from config/elements.yml



206
207
208
209
210
# File 'lib/alchemy/essence.rb', line 206

def definition
  return {} if element.nil? || element.content_definitions.nil?

  element.content_definitions.detect { |c| c["name"] == content.name } || {}
end

#duplicatesObject



178
179
180
181
182
183
184
# File 'lib/alchemy/essence.rb', line 178

def duplicates
  acts_as_essence_class
    .available
    .from_element(element.name)
    .where(ingredient_column.to_s => ingredient)
    .where.not(id: id)
end

#has_tinymce?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/alchemy/essence.rb', line 239

def has_tinymce?
  false
end

#ingredientObject

Returns the value stored from the database column that is configured as ingredient column.



187
188
189
190
191
# File 'lib/alchemy/essence.rb', line 187

def ingredient
  if respond_to?(ingredient_column)
    send(ingredient_column)
  end
end

#ingredient=(value) ⇒ Object

Sets the value stored in the database column that is configured as ingredient column.



194
195
196
197
198
# File 'lib/alchemy/essence.rb', line 194

def ingredient=(value)
  if respond_to?(ingredient_setter_method)
    send(ingredient_setter_method, value)
  end
end

#ingredient_setter_methodObject

Returns the setter method for ingredient column



201
202
203
# File 'lib/alchemy/essence.rb', line 201

def ingredient_setter_method
  ingredient_column.to_s + "="
end

Returns:

  • (Boolean)


223
224
225
# File 'lib/alchemy/essence.rb', line 223

def open_link_in_new_window?
  respond_to?(:link_target) && link_target == "blank"
end

#partial_nameObject



227
228
229
# File 'lib/alchemy/essence.rb', line 227

def partial_name
  self.class.name.split("::").last.underscore
end

#preview_text(maxlength = 30) ⇒ Object

Returns the first x (default 30) characters of ingredient for the Element#preview_text method.



219
220
221
# File 'lib/alchemy/essence.rb', line 219

def preview_text(maxlength = 30)
  send(preview_text_column).to_s[0..maxlength - 1]
end

#to_partial_pathObject



235
236
237
# File 'lib/alchemy/essence.rb', line 235

def to_partial_path
  "alchemy/essences/#{partial_name}_view"
end

#touch_elementObject

Touches element. Called after save.



213
214
215
# File 'lib/alchemy/essence.rb', line 213

def touch_element
  element&.touch
end

#validate_format(format) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/alchemy/essence.rb', line 170

def validate_format(format)
  matcher = Config.get("format_matchers")[format] || format
  if ingredient.to_s.match(Regexp.new(matcher)).nil?
    errors.add(ingredient_column, :invalid)
    validation_errors << :invalid
  end
end

#validate_ingredientObject

Essence Validations:

Essence validations can be set inside the config/elements.yml file.

Supported validations are:

  • presence

  • uniqueness

  • format

format needs to come with a regex or a predefined matcher string as its value. There are already predefined format matchers listed in the config/alchemy/config.yml file. It is also possible to add own format matchers there.

Example of format matchers in config/alchemy/config.yml:

format_matchers:

email: !ruby/regexp '/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/'
url:   !ruby/regexp '/\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix'
ssl:   !ruby/regexp '/https:\/\/[\S]+/'

Example of an element definition with essence validations:

- name: person
  contents:
  - name: name
    type: EssenceText
    validate: [presence]
  - name: email
    type: EssenceText
    validate: [format: 'email']
  - name: homepage
    type: EssenceText
    validate: [format: !ruby/regexp '^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$']

Example of an element definition with chained validations.

- name: person
  contents:
  - name: name
    type: EssenceText
    validate: [presence, uniqueness, format: 'name']


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/alchemy/essence.rb', line 134

def validate_ingredient
  validations.each do |validation|
    if validation.respond_to?(:keys)
      validation.map do |key, value|
        send("validate_#{key}", value)
      end
    else
      send("validate_#{validation}")
    end
  end
end

#validate_presence(validate = true) ⇒ Object



154
155
156
157
158
159
# File 'lib/alchemy/essence.rb', line 154

def validate_presence(validate = true)
  if validate && ingredient.blank?
    errors.add(ingredient_column, :blank)
    validation_errors << :blank
  end
end

#validate_uniqueness(validate = true) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/alchemy/essence.rb', line 161

def validate_uniqueness(validate = true)
  return if !validate || !public?

  if duplicates.any?
    errors.add(ingredient_column, :taken)
    validation_errors << :taken
  end
end

#validation_errorsObject



150
151
152
# File 'lib/alchemy/essence.rb', line 150

def validation_errors
  @validation_errors ||= []
end

#validationsObject



146
147
148
# File 'lib/alchemy/essence.rb', line 146

def validations
  @validations ||= definition.present? ? definition["validate"] || [] : []
end