Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- ActiveResource association has_many, added method resource=

  • Loading branch information...
commit 7223ca73799002665a90aa1d474ac584a7d3818c 1 parent 8edb72c
@gramos authored
View
7 activeresource/lib/active_resource/associations.rb
@@ -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
View
12 activeresource/test/cases/associations_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.