Module: Alchemy::Essence::InstanceMethods

Defined in:
lib/alchemy/essence.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_essence?Boolean

Returns:

  • (Boolean)

258
259
260
# File 'lib/alchemy/essence.rb', line 258

def acts_as_essence?
  acts_as_essence_class.present?
end

#definitionObject

Essence definition from config/elements.yml


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

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

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

#duplicatesObject


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

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)

266
267
268
# File 'lib/alchemy/essence.rb', line 266

def has_tinymce?
  false
end

#ingredientObject

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


214
215
216
217
218
# File 'lib/alchemy/essence.rb', line 214

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.


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

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


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

def ingredient_setter_method
  ingredient_column.to_s + "="
end

Returns:

  • (Boolean)

250
251
252
# File 'lib/alchemy/essence.rb', line 250

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

#partial_nameObject


254
255
256
# File 'lib/alchemy/essence.rb', line 254

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.


246
247
248
# File 'lib/alchemy/essence.rb', line 246

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

#to_partial_pathObject


262
263
264
# File 'lib/alchemy/essence.rb', line 262

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

#touch_elementObject

Touches element. Called after save.


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

def touch_element
  element&.touch
end

#validate_format(format) ⇒ Object


197
198
199
200
201
202
203
# File 'lib/alchemy/essence.rb', line 197

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][email protected]([^@\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']

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

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


181
182
183
184
185
186
# File 'lib/alchemy/essence.rb', line 181

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

#validate_uniqueness(validate = true) ⇒ Object


188
189
190
191
192
193
194
195
# File 'lib/alchemy/essence.rb', line 188

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

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

#validation_errorsObject


177
178
179
# File 'lib/alchemy/essence.rb', line 177

def validation_errors
  @validation_errors ||= []
end

#validationsObject


173
174
175
# File 'lib/alchemy/essence.rb', line 173

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