Sha256: bedff08d193b0b6f0066ad5c7bcc8adbacf270cb44dbce521af1392f388f6c27
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Reform::Form::ActiveRecord def self.included(base) base.class_eval do register_feature Reform::Form::ActiveRecord include Reform::Form::ActiveModel extend ClassMethods end end module ClassMethods def validates_uniqueness_of(attribute, options={}) options = options.merge(:attributes => [attribute]) validates_with(UniquenessValidator, options) end def i18n_scope :activerecord end end class UniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator # when calling validates it should create the Vali instance already and set @klass there! # TODO: fix this in AM. def validate(form) property = attributes.first # here is the thing: why does AM::UniquenessValidator require a filled-out record to work properly? also, why do we need to set # the class? it would be way easier to pass #validate a hash of attributes and get back an errors hash. # the class for the finder could either be infered from the record or set in the validator instance itself in the call to ::validates. record = form.model_for_property(property) record.send("#{property}=", form.send(property)) @klass = record.class # this is usually done in the super-sucky #setup method. super(record).tap do |res| form.errors.add(property, record.errors.first.last) if record.errors.present? end end end def model_for_property(name) return model unless is_a?(Reform::Form::Composition) # i am too lazy for proper inheritance. there should be a ActiveRecord::Composition that handles this. model_name = mapper.representable_attrs.get(name)[:on] model[model_name] end # Delegate column for attribute to the model to support simple_form's # attribute type interrogation. def column_for_attribute(name) model_for_property(name).column_for_attribute(name) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
reform-1.1.1 | lib/reform/form/active_record.rb |