Skip to content

Instantly share code, notes, and snippets.

@seban
Created August 12, 2013 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seban/6208495 to your computer and use it in GitHub Desktop.
Save seban/6208495 to your computer and use it in GitHub Desktop.
# 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
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