Class: LiquidValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- LiquidValidator
show all
- Defined in:
- app/models/liquid_validator.rb
Overview
Validate whether an attribute has a parsable liquid template.
if { locales: true } is passed in, will assume it's a localized
field and check all locales
validates :content, :liquid => { :locales => true }
Instance Method Summary
collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'app/models/liquid_validator.rb', line 7
def validate_each(record, attribute, value)
if options[:locales]
Account.current.site_locales.each do |locale|
attribute_locale = "#{attribute.to_s}_#{locale}"
begin
Liquid::Template.parse(record.send(attribute_locale))
rescue Liquid::SyntaxError => e
record.errors.add attribute_locale, e.message
end
end
else
begin
Liquid::Template.parse(value)
rescue Liquid::SyntaxError => e
record.errors.add attribute, e.message
end
end
end
|