Skip to content

Commit

Permalink
- ActiveResource associaions
Browse files Browse the repository at this point in the history
  test_has_many_accessor_should_return_the_an_array_including_the_added_obj
  • Loading branch information
gramos committed Oct 1, 2010
1 parent 2a78944 commit 7e367a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion activeresource/lib/active_resource/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,17 @@ def has_many(resource, opts = {})
#
#----------------------------------------------------------------------#
define_method(resource) do
collection = o[:klass].constantize.find(:all,
:params => { o[:association_col] => id })

eval "
def collection.<<(member)
member.send(:#{o[:association_col]}=, #{id})
member.save
end"

set_resource_instance_variable(resource) do
o[:klass].constantize.find(:all, :params => { o[:association_col] => id })
collection
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion activeresource/test/cases/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Milestone < ActiveResource::Base
@other_project_manager = {:id => 6, :name => "John", :project_id => nil}
@project_managers = [@project_manager]
@milestone = { :id => 1, :title => "pre", :project_id => nil}
@other_milestone = { :id => 2, :title => "rc", :project_id => nil}

ActiveResource::HttpMock.respond_to do |mock|
mock.get "/projects/.xml", {}, @project.to_xml(:root => 'project')
mock.get "/projects/1.xml", {}, @project.to_xml(:root => 'project')
mock.get "/milestones/1.xml", {}, @milestone.to_xml(:root => 'milestone')
mock.get "/projects/2.xml", {}, @other_project.to_xml(:root => 'project')
mock.get "/project_managers/5.xml", {}, @project_manager.to_xml(:root => 'project_manager')
mock.get "/project_managers/6.xml", {}, @other_project_manager.to_xml(:root => 'project_manager')
Expand All @@ -36,6 +36,9 @@ class Milestone < ActiveResource::Base
mock.get "/milestones.xml?project_id=2", {}, [].to_xml
mock.get "/milestones.xml?project_id=1", {}, [@milestone].to_xml
mock.put "/project_managers/6.xml", {}, nil, 204
mock.put "/milestones/2.xml", {}, nil, 204
mock.get "/milestones/1.xml", {}, @milestone.to_xml(:root => 'milestone')
mock.get "/milestones/2.xml", {}, @other_milestone.to_xml(:root => 'milestone')
end

class AssociationsTest < Test::Unit::TestCase
Expand All @@ -46,6 +49,7 @@ def setup
@project_manager = ProjectManager.find(5)
@other_project_manager = ProjectManager.find(6)
@milestone = Milestone.find(1)
@other_milestone = Milestone.find(2)
end

#----------------------------------------------------------------------
Expand Down Expand Up @@ -105,5 +109,10 @@ def test_has_many_accessor_should_return_the_an_array_with_the_associated_milest
def test_has_many_accessor_should_return_the_an_empty_array_when_it_does_not_has_milestones
assert_equal [], @other_project.milestones
end

def test_has_many_accessor_should_return_the_an_array_including_the_added_obj
@project.milestones << @other_milestone
assert_equal @other_milestone.project_id, @project.id
end
end

0 comments on commit 7e367a1

Please sign in to comment.