Sha256: e109cf84acc9157e213dc87f1c0e967b69a40de8e00152d27edba25a443d7fbf

Contents?: true

Size: 1.18 KB

Versions: 19

Compression:

Stored size: 1.18 KB

Contents

module Reform::Form::ORM
  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 = options_for(name)[:on]
    model[model_name]
  end

  module 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
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
reform-rails-0.2.0.rc2 lib/reform/form/orm.rb
reform-rails-0.2.0.rc1 lib/reform/form/orm.rb
reform-2.2.4 lib/reform/form/orm.rb
reform-2.2.3 lib/reform/form/orm.rb
reform-2.2.2 lib/reform/form/orm.rb
reform-rails-0.1.7 lib/reform/form/orm.rb
reform-rails-0.1.5 lib/reform/form/orm.rb
reform-rails-0.1.4 lib/reform/form/orm.rb
reform-2.2.1 lib/reform/form/orm.rb
reform-2.2.0 lib/reform/form/orm.rb
reform-2.2.0.rc1 lib/reform/form/orm.rb
reform-2.1.0 lib/reform/form/orm.rb
reform-2.1.0.rc1 lib/reform/form/orm.rb
reform-2.0.5 lib/reform/form/orm.rb
reform-2.0.4 lib/reform/form/orm.rb
reform-2.0.3 lib/reform/form/orm.rb
reform-2.0.2 lib/reform/form/orm.rb
reform-2.0.1 lib/reform/form/orm.rb
reform-2.0.0 lib/reform/form/orm.rb