Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- ActiveResource added writter to belongs_to association

  • Loading branch information...
commit 9ac13296f60f5aa58bf86c052eac34fd01f92803 1 parent 0b78260
@gramos authored
View
11 activeresource/lib/active_resource/associations.rb
@@ -80,6 +80,17 @@ def belongs_to(resource, opts = {})
return nil if association_col.nil?
set_resource_instance_variable(resource){ h[:klass].find(association_col) }
end
+
+ #----------------------------------------------------------------------#
+ # Define writter method for resource
+ #
+ #----------------------------------------------------------------------#
+ define_method("#{klass_name}=") do |new_resource|
+ if send(h[:association_col]) != new_resource.id
+ send(:update_attribute, h[:association_col], new_resource.id)
+ end
+ instance_variable_set("@#{resource}", new_resource)
+ end
end
def has_many(resource, opts = {})
View
7 activeresource/test/cases/associations_test.rb
@@ -59,7 +59,7 @@ def test_has_one_accessor_should_return_nil_when_the_does_not_has_an_associated_
assert_nil @other_project.project_manager
end
- def test_has_one_should_assign_a_new_project_manager
+ def test_has_one_should_assign_a_new_project_manager_when_it_does_not_has_a_project_manager
@other_project.project_manager = @other_project_manager
assert_equal @other_project.id, @other_project_manager.project_id
end
@@ -80,6 +80,11 @@ def test_belongs_to_accessor_should_return_nil_when_the_does_not_has_an_associat
assert_nil @other_project_manager.project
end
+ def test_has_one_should_assign_a_new_project_manager_when_it_does_not_has_a_project_manager
+ @other_project_manager.project = @other_project
+ assert_equal @other_project_manager.project_id, @other_project.id
+ end
+
#----------------------------------------------------------------------
# has_many association
#----------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.