Class: SlimValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/slim_validation.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr_name, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/slim_validation.rb', line 7

def validate_each(record, attr_name, value)
  result = Slim::Template.new(options[:options]) { value }.render(options[:scope] == :record ? record : options[:scope])
rescue => e
  if (error_storer = options[:error]).present?
    record.send("#{error_storer}=", e)
  end
  record.errors.add(attr_name, :invalid_slim, options)
ensure
  if (to = options[:to]).present?
    to.is_a?(Proc) ? to.(record, result) : record.send("#{to}=", result)
  end
end