Skip to content

Commit

Permalink
make #to_hash a Composition method, it is no longer available in Form…
Browse files Browse the repository at this point in the history
…. use #save with block.
  • Loading branch information
apotonick committed May 3, 2014
1 parent 62a787a commit 3f4124d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
6 changes: 0 additions & 6 deletions lib/reform/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ def aliased_model
require 'reform/form/multi_parameter_attributes'
include MultiParameterAttributes # TODO: make features dynamic.


# Use representer to return current key-value form hash.
def to_hash(*args)
mapper.new(self).to_hash(*args)
end

attr_accessor :model

private
Expand Down
4 changes: 4 additions & 0 deletions lib/reform/form/composition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ def aliased_model # we don't need an Expose as we save the Composition instance
def to_nested_hash
model.nested_hash_for(to_hash) # use composition to compute nested hash.
end

def to_hash(*args)
mapper.new(self).to_hash(*args)
end
end
end
2 changes: 1 addition & 1 deletion test/coercion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ class CoercionTest < BaseTest
end.new(OpenStruct.new())

form.validate("id" => "1")
form.to_hash.must_equal("id" => 1)
form.id.must_equal 1
end
end
8 changes: 6 additions & 2 deletions test/nested_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ class AlbumForm < Reform::Form
let (:songs) { [OpenStruct.new(:title => "Calling")] }
let (:form) { AlbumForm.new(album) }

it "responds to #to_hash" do
form.to_hash.must_equal({"hit"=>{"title"=>"Downtown"}, "title" => "Blackhawks Over Los Angeles", "songs"=>[{"title"=>"Calling"}]})
it "responds to #save" do
hsh = nil
form.save do |f, nested|
hsh = nested
end
hsh.must_equal({"hit"=>{"title"=>"Downtown"}, "title" => "Blackhawks Over Los Angeles", "songs"=>[{"title"=>"Calling"}]})
end


Expand Down
7 changes: 5 additions & 2 deletions test/reform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ class ReformTest < ReformSpec


describe "::properties" do
it do
subject do
Class.new(Reform::Form) do
properties [:name, :title]
end.new(comp).to_hash.must_equal({"name"=>"Duran Duran", "title"=>"Rio"})
end.new(comp)
end

it { subject.name.must_equal "Duran Duran" }
it { subject.title.must_equal "Rio" }
end

class SongForm < Reform::Form
Expand Down
12 changes: 0 additions & 12 deletions test/to_hash_test.rb

This file was deleted.

0 comments on commit 3f4124d

Please sign in to comment.