Permalink
Browse files

- ActiveResource association has_many, added method resource=

  • Loading branch information...
1 parent 8edb72c commit 7223ca73799002665a90aa1d474ac584a7d3818c @gramos committed Oct 1, 2010
@@ -139,6 +139,13 @@ def collection.clear
collection
end
+
+ define_method("#{resource}=") do |new_collection|
+ collection = send(resource)
+ to_remove = collection - new_collection
+ to_remove.each{|m| collection.delete(m)}
+ instance_variable_set("@#{resource}", new_collection)
+ end
end
end
@@ -128,5 +128,17 @@ def test_has_many_accessor_should_return_the_an_empty_array_after_clear
assert_equal [], @project.milestones
end
+
+ def test_has_many_accessor_should_return_the_new_array_after_assign
+ @project.milestones = [@other_milestone]
+ assert_equal [@other_milestone], @project.milestones
+
+ @project.milestones = []
+ assert_equal [], @project.milestones
+
+ @project.milestones = [@milestone, @other_milestone]
+ assert_equal [@milestone, @other_milestone], @project.milestones
+ end
+
end

0 comments on commit 7223ca7

Please sign in to comment.