test/reform_test.rb in reform-1.0.4 vs test/reform_test.rb in reform-1.1.0

- old
+ new

@@ -202,30 +202,40 @@ comp.name.must_equal "Diesel Boy" comp.title.must_equal nil end describe "#save with block" do - it "provides data block argument" do + it "Deprecated: provides data block argument" do # TODO: remove in 1.1. hash = {} form.save do |data, map| hash[:name] = data.name hash[:title] = data.title end hash.must_equal({:name=>"Diesel Boy", :title=>nil}) end - it "provides nested symbolized hash as second block argument" do + it "Deprecated: provides nested symbolized hash as second block argument" do # TODO: remove in 1.1. hash = {} form.save do |data, map| hash = map end hash.must_equal({"name"=>"Diesel Boy"}) end + + it do + hash = {} + + form.save do |map| + hash = map + end + + hash.must_equal({"name"=>"Diesel Boy"}) + end end end describe "#model" do @@ -237,20 +247,85 @@ class HitForm < SongForm property :position validates :position, :presence => true end - let (:form) { HitForm.new(OpenStruct.new) } + let (:form) { HitForm.new(OpenStruct.new()) } it do form.validate({"title" => "The Body"}) form.title.must_equal "The Body" form.position.must_equal nil form.errors.messages.must_equal({:name=>["can't be blank"], :position=>["can't be blank"]}) end end + + describe "#column_for_attribute" do + let (:model) { Artist.new } + let (:klass) do + require 'reform/active_record' + + Class.new Reform::Form do + include Reform::Form::ActiveRecord + + model :artist + + property :name + end + end + let (:form) { klass.new(model) } + + it 'should delegate to the model' do + Calls = [] + + def model.column_for_attribute(*args) + Calls << [:column_for_attribute, *args] + end + + form.column_for_attribute(:name) + Calls.must_include [:column_for_attribute, :name] + end + end + + describe "#column_for_attribute with composition" do + let (:artist_model) { Artist.new } + let (:song_model) { Song.new } + let (:form_klass) do + require 'reform/active_record' + + Class.new Reform::Form do + include Reform::Form::ActiveRecord + include Reform::Form::Composition + + model :artist + + property :name, on: :artist + property :title, on: :song + end + end + let (:form) { form_klass.new(artist: artist_model, song: song_model) } + + it 'should delegate to the model' do + ArtistCalls, SongCalls = [], [] + + def artist_model.column_for_attribute(*args) + ArtistCalls << [:column_for_attribute, *args] + end + + def song_model.column_for_attribute(*args) + SongCalls << [:column_for_attribute, *args] + end + + form.column_for_attribute(:name) + ArtistCalls.must_include [:column_for_attribute, :name] + + form.column_for_attribute(:title) + SongCalls.must_include [:column_for_attribute, :title] + end + end end + class EmptyAttributesTest < MiniTest::Spec Credentials = Struct.new(:password) class PasswordForm < Reform::Form property :password @@ -268,11 +343,11 @@ form.sync cred.password.must_equal "123" hash = {} - form.save do |f, nested| + form.save do |nested| hash = nested end hash.must_equal("password"=> "123", "password_confirmation" => "321") } @@ -296,11 +371,11 @@ form.sync loc.country.must_equal "Australia" # the writer wasn't called. hash = {} - form.save do |f, nested| + form.save do |nested| hash = nested end hash.must_equal("country"=> "Germany") end @@ -333,11 +408,11 @@ # reader always used it { subject.title.must_equal "hey little worldhey little world" } # the reader is not used when saving/syncing. it do - subject.save do |f, hash| + subject.save do |hash| hash["title"].must_equal "Hey Little WorldHey Little World" end end # no reader or writer used when saving/syncing. @@ -345,6 +420,30 @@ song.extend(Saveable) subject.save song.title.must_equal "Hey Little WorldHey Little World" end end -end \ No newline at end of file +end + + +class MethodInFormTest < MiniTest::Spec + class AlbumForm < Reform::Form + property :title + + def title + "The Suffer And The Witness" + end + + property :hit do + property :title + + def title + "Drones" + end + end + end + + # methods can be used instead of created accessors. + subject { AlbumForm.new(OpenStruct.new(:hit => OpenStruct.new)) } + it { subject.title.must_equal "The Suffer And The Witness" } + it { subject.hit.title.must_equal "Drones" } +end