Module: Alchemy::Essence::InstanceMethods

Defined in:
lib/alchemy/essence.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_essence?Boolean

Returns:

  • (Boolean)


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

def acts_as_essence?
  acts_as_essence_class.present?
end

#definitionObject

Essence definition from config/elements.yml



210
211
212
213
214
# File 'lib/alchemy/essence.rb', line 210

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

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

#duplicatesObject



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

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)


243
244
245
# File 'lib/alchemy/essence.rb', line 243

def has_tinymce?
  false
end

#ingredientObject

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



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

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.



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

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



205
206
207
# File 'lib/alchemy/essence.rb', line 205

def ingredient_setter_method
  ingredient_column.to_s + "="
end

Returns:

  • (Boolean)


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

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

#partial_nameObject



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

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.



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

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

#to_partial_pathObject



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

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

#touch_elementObject

Touches element. Called after save.



217
218
219
# File 'lib/alchemy/essence.rb', line 217

def touch_element
  element&.touch
end

#validate_format(format) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/alchemy/essence.rb', line 174

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']


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/alchemy/essence.rb', line 138

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



158
159
160
161
162
163
# File 'lib/alchemy/essence.rb', line 158

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

#validate_uniqueness(validate = true) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/alchemy/essence.rb', line 165

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

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

#validation_errorsObject



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

def validation_errors
  @validation_errors ||= []
end

#validationsObject



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

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