Skip to content
Browse files

- added ActiveResource has_many association

  • Loading branch information...
1 parent df72be3 commit 59dc21af856a9df89d54a82219e4c0905d584bf8 @gramos committed
Showing with 36 additions and 3 deletions.
  1. +22 −3 activeresource/lib/active_resource/associations.rb
  2. +14 −0 activeresource/test/cases/associations_test.rb
View
25 activeresource/lib/active_resource/associations.rb
@@ -7,9 +7,9 @@ def self.included(klass)
end
module InstanceMethods
- def set_resource_instance_variable(resource)
+ def set_resource_instance_variable(resource, default_value = nil)
if !instance_variable_defined?("@#{resource}") ||
- instance_variable_get("@#{resource}").nil?
+ instance_variable_get("@#{resource}").blank?
instance_variable_set("@#{resource}", yield)
end
instance_variable_get("@#{resource}")
@@ -20,7 +20,8 @@ module ClassMethods
def hash_options(association, resource)
h = { :klass => klass_for(association, resource) }
- h[:host_klass] = self
+ h[:host_klass] = self
+
case association
when :belongs_to
h[:association_col] = "#{h[:klass].to_s.underscore}_id".to_sym
@@ -33,6 +34,8 @@ def hash_options(association, resource)
def klass_for(association, resource)
resource = resource.to_s
resource = resource.singularize if association == :has_many
+
+ # FIXME constantize only when use it
resource.camelize.constantize
end
@@ -80,6 +83,22 @@ def belongs_to(resource, opts = {})
end
end
+ def has_many(resource, opts = {})
+ h = hash_options(:has_many, resource)
+ klass_name = opts[:class_name].nil? ? resource : opts[:class_name]
+
+ #----------------------------------------------------------------------#
+ # Define accessor method for resource
+ #
+ #----------------------------------------------------------------------#
+ define_method(klass_name) do
+ set_resource_instance_variable(resource) do
+ h[:klass].find(:all, :params => { h[:association_col] => id })
+ end
+ end
+ end
+
end
end
+
end
View
14 activeresource/test/cases/associations_test.rb
@@ -1,6 +1,10 @@
require 'abstract_unit'
require "fixtures/project"
+class Milestone < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+end
+
class ProjectManager < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
belongs_to :project
@@ -9,8 +13,10 @@ class ProjectManager < ActiveResource::Base
class Project < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
has_one :project_manager
+ has_many :milestones
end
+
@project = { :id => 1, :name => "Rails"}
@other_project = { :id => 2, :name => "Ruby"}
@project_manager = {:id => 5, :name => "David", :project_id =>1}
@@ -73,5 +79,13 @@ def test_belongs_to_accessor_should_return_the_associated_project
def test_belongs_to_accessor_should_return_nil_when_the_does_not_has_an_associated_resource
assert_nil @other_project_manager.project
end
+
+ #----------------------------------------------------------------------
+ # has_many association
+ #----------------------------------------------------------------------
+
+ def test_has_many_should_add_a_resource_accessor
+ assert @project.respond_to? :milestones
+ end
end

0 comments on commit 59dc21a

Please sign in to comment.
Something went wrong with that request. Please try again.