Skip to content

Instantly share code, notes, and snippets.

@rikkipitt
Created June 9, 2015 15:15
Show Gist options
  • Save rikkipitt/4551c9b72b35cd7a4756 to your computer and use it in GitHub Desktop.
Save rikkipitt/4551c9b72b35cd7a4756 to your computer and use it in GitHub Desktop.
Dentaku expression validator
class ExpressionValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
Dentaku(value)
rescue Exception => e
record.errors[attribute] << (options[:message] || e.message)
end
end
end
@rikkipitt
Copy link
Author

For use with Dentaku.

Save to app/validators/expression_validator.rb and include in your model like so:

class Product < ActiveRecord::Base
  validates :expression, expression: true
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment