Created
August 12, 2013 05:48
-
-
Save seban/6208495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Class validates if passed record's attribute value has proper Liquid template syntax. | |
class LiquidSyntaxValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, template) | |
Liquid::Template.parse(template) | |
rescue Liquid::SyntaxError | |
record.errors.add(attribute, :liquid_syntax_error) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe LiquidSyntaxValidator do | |
let(:email_template) { build(:email_template) } | |
let(:attribute_to_valid) { :body } | |
let(:validator_options) { { attributes: attribute_to_valid } } | |
let(:validator) { LiquidSyntaxValidator.new(validator_options) } | |
describe "#validate_each" do | |
context "and record is valid" do | |
before { validator.validate_each(email_template, attribute_to_valid, email_template.body) } | |
it("doesn't add errors on :body field") { email_template.errors[:body].should be_blank } | |
end | |
context "and record isn't valid" do | |
before do | |
email_template.body = "Bad syntax template {{ username" | |
validator.validate_each(email_template, attribute_to_valid, email_template.body) | |
end | |
it("adds error on :body field") { email_template.errors[:body].should be_present } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment