Permalink
Browse files

- fix ActiveResource has_one association

  resource_id column is on the associated resource (belogns_to)
- refactoring association test cases
  • Loading branch information...
1 parent e440e23 commit 95d53054dee164c1c988ffc0549143e6c16412bf @gramos committed Sep 27, 2010
Showing with 12 additions and 24 deletions.
  1. +6 −6 activeresource/lib/active_resource/associations.rb
  2. +6 −18 activeresource/test/cases/associations_test.rb
@@ -4,10 +4,12 @@ module Associations
def hash_options(association, resource)
h = { :klass => klass_for(association, resource) }
h[:host_klass] = self
- if association == :has_many
+ case association
+ when :has_many
h[:association_col] = "#{h[:host_klass].to_s.downcase}_id".to_sym
- else
- h[:association_col] = "#{h[:klass].to_s.underscore}_id".to_sym
+
+ when :has_one
+ h[:association_col] = "#{h[:host_klass].to_s.underscore}_id".to_sym
end
h
end
@@ -27,11 +29,9 @@ def has_one(resource, opts = {})
#
#----------------------------------------------------------------------#
define_method(klass_name) do
- resource_id = send(h[:association_col])
-
if instance_variable_get("@#{resource}").nil?
instance_variable_set("@#{resource}",
- h[:klass].find(resource_id) )
+ h[:klass].find(:first, :params => { h[:association_col] => id }) )
end
return instance_variable_get("@#{resource}")
end
@@ -10,26 +10,14 @@ class Project < ActiveResource::Base
has_one :project_manager
end
-@project = <<-eof.strip
- <?xml version=\"1.0\" encoding=\"UTF-8\"?>
- <project>
- <id type=\"integer\">1</id>
- <name>Rails</name>
- <project_manager_id>5</project_manager_id>
- </project>
-eof
-
-@project_manager = <<-eof.strip
- <?xml version=\"1.0\" encoding=\"UTF-8\"?>
- <project_manager>
- <id type=\"integer\">5</id>
- <name>David</name>
- </project_manager>
-eof
+@project = { :id => 1, :name => "Rails"}
+@project_manager = {:id => 5, :name => "David", :project_id =>1}
+@project_managers = [@project_manager]
ActiveResource::HttpMock.respond_to do |mock|
- mock.get "/projects/1.xml", {}, @project
- mock.get "/project_managers/5.xml", {}, @project_manager
+ mock.get "/projects/1.xml", {}, @project.to_xml(:root => 'project')
+ mock.get "/project_managers/5.xml", {}, @project_manager.to_xml(:root => 'project_manager')
+ mock.get "/project_managers.xml?project_id=1", {}, @project_managers.to_xml
end
class AssociationsTest < Test::Unit::TestCase

0 comments on commit 95d5305

Please sign in to comment.