Module: Alchemy::Essence::InstanceMethods
- Defined in:
- lib/alchemy/essence.rb
Instance Method Summary collapse
- #acts_as_essence? ⇒ Boolean
-
#definition ⇒ Object
Essence definition from config/elements.yml.
- #duplicates ⇒ Object
- #has_tinymce? ⇒ Boolean
-
#ingredient ⇒ Object
Returns the value stored from the database column that is configured as ingredient column.
-
#ingredient=(value) ⇒ Object
Returns the value stored from the database column that is configured as ingredient column.
-
#ingredient_setter_method ⇒ Object
Returns the setter method for ingredient column.
- #open_link_in_new_window? ⇒ Boolean
- #partial_name ⇒ Object
-
#preview_text(maxlength = 30) ⇒ Object
Returns the first x (default 30) characters of ingredient for the Element#preview_text method.
- #to_partial_path ⇒ Object
-
#touch_content ⇒ Object
Touch content.
- #validate_format(format) ⇒ Object
-
#validate_ingredient ⇒ Object
Essence Validations:.
- #validate_presence(validate = true) ⇒ Object
- #validate_uniqueness(validate = true) ⇒ Object
- #validation_errors ⇒ Object
- #validations ⇒ Object
Instance Method Details
#acts_as_essence? ⇒ Boolean
218 219 220 |
# File 'lib/alchemy/essence.rb', line 218 def acts_as_essence? acts_as_essence_class.present? end |
#definition ⇒ Object
Essence definition from config/elements.yml
193 194 195 196 |
# File 'lib/alchemy/essence.rb', line 193 def definition return {} if element.nil? || element.content_definitions.nil? element.content_definitions.detect { |c| c['name'] == content.name } || {} end |
#duplicates ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/alchemy/essence.rb', line 165 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
226 227 228 |
# File 'lib/alchemy/essence.rb', line 226 def has_tinymce? false end |
#ingredient ⇒ Object
Returns the value stored from the database column that is configured as ingredient column.
174 175 176 177 178 |
# File 'lib/alchemy/essence.rb', line 174 def ingredient if respond_to?(ingredient_column) send(ingredient_column) end end |
#ingredient=(value) ⇒ Object
Returns the value stored from the database column that is configured as ingredient column.
181 182 183 184 185 |
# File 'lib/alchemy/essence.rb', line 181 def ingredient=(value) if respond_to?(ingredient_setter_method) send(ingredient_setter_method, value) end end |
#ingredient_setter_method ⇒ Object
Returns the setter method for ingredient column
188 189 190 |
# File 'lib/alchemy/essence.rb', line 188 def ingredient_setter_method ingredient_column.to_s + '=' end |
#open_link_in_new_window? ⇒ Boolean
210 211 212 |
# File 'lib/alchemy/essence.rb', line 210 def open_link_in_new_window? respond_to?(:link_target) && link_target == 'blank' end |
#partial_name ⇒ Object
214 215 216 |
# File 'lib/alchemy/essence.rb', line 214 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.
206 207 208 |
# File 'lib/alchemy/essence.rb', line 206 def preview_text(maxlength = 30) send(preview_text_column).to_s[0..maxlength - 1] end |
#to_partial_path ⇒ Object
222 223 224 |
# File 'lib/alchemy/essence.rb', line 222 def to_partial_path "alchemy/essences/#{partial_name}_view" end |
#touch_content ⇒ Object
Touch content. Called after update.
199 200 201 202 |
# File 'lib/alchemy/essence.rb', line 199 def touch_content return nil if content.nil? content.touch end |
#validate_format(format) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/alchemy/essence.rb', line 157 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_ingredient ⇒ Object
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']
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/alchemy/essence.rb', line 122 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
142 143 144 145 146 147 |
# File 'lib/alchemy/essence.rb', line 142 def validate_presence(validate = true) if validate && ingredient.blank? errors.add(ingredient_column, :blank) validation_errors << :blank end end |
#validate_uniqueness(validate = true) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/alchemy/essence.rb', line 149 def validate_uniqueness(validate = true) return if !validate || !public? if duplicates.any? errors.add(ingredient_column, :taken) validation_errors << :taken end end |
#validation_errors ⇒ Object
138 139 140 |
# File 'lib/alchemy/essence.rb', line 138 def validation_errors @validation_errors ||= [] end |
#validations ⇒ Object
134 135 136 |
# File 'lib/alchemy/essence.rb', line 134 def validations @validations ||= definition.present? ? definition['validate'] || [] : [] end |