Module: Alchemy::Essence::InstanceMethods

Defined in:
lib/alchemy/essence.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_essence?Boolean

Returns:

  • (Boolean)


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

def acts_as_essence?
  acts_as_essence_class.present?
end

#descriptionObject

Essence description from config/elements.yml



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

def description
  return {} if element.nil? or element.content_descriptions.nil?
  element.content_descriptions.detect { |c| c['name'] == self.content.name } || {}
end

#duplicatesObject



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

def duplicates
  acts_as_essence_class
    .available
    .from_element(element.name)
    .where("#{ingredient_column}" => ingredient)
    .where.not(id: self.id)
end

#ingredientObject

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



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

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

#ingredient=(value) ⇒ Object

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



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

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

#ingredient_setter_methodObject

Returns the setter method for ingredient column



190
191
192
# File 'lib/alchemy/essence.rb', line 190

def ingredient_setter_method
  ingredient_column.to_s + '='
end

Returns:

  • (Boolean)


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

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

#partial_nameObject



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

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.



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

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

#to_partial_pathObject



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

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

#touch_contentObject

Touch content. Called after update.



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

def touch_content
  return nil if content.nil?
  content.touch
end

#validate_format(format) ⇒ Object



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

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


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/alchemy/essence.rb', line 124

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

#validate_presence(validate = true) ⇒ Object



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

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

#validate_uniqueness(validate = true) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/alchemy/essence.rb', line 151

def validate_uniqueness(validate = true)
  return if !validate || !public?
  if duplicates.any?
    errors.add(ingredient_column, :taken)
    validation_errors << :taken
  end
end

#validation_errorsObject



140
141
142
# File 'lib/alchemy/essence.rb', line 140

def validation_errors
  @validation_errors ||= []
end

#validationsObject



136
137
138
# File 'lib/alchemy/essence.rb', line 136

def validations
  @validations ||= description.present? ? description['validate'] || [] : []
end