Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- added belongs_to association to ActiveResource

  • Loading branch information...
commit 8a7f3d99d8fc61bba0dc74c48888e43fc2988c22 1 parent 79c4e02
@gramos authored
View
46 activeresource/lib/active_resource/associations.rb
@@ -5,10 +5,10 @@ def hash_options(association, resource)
h = { :klass => klass_for(association, resource) }
h[:host_klass] = self
case association
- when :has_many
+ when :has_many
h[:association_col] = "#{h[:host_klass].to_s.downcase}_id".to_sym
- when :has_one
+ when :has_one
h[:association_col] = "#{h[:host_klass].to_s.underscore}_id".to_sym
end
h
@@ -30,26 +30,42 @@ def has_one(resource, opts = {})
#----------------------------------------------------------------------#
define_method(klass_name) do
if !instance_variable_defined?("@#{resource}") ||
- instance_variable_get("@#{resource}").nil?
+ instance_variable_get("@#{resource}").nil?
instance_variable_set("@#{resource}",
- h[:klass].find(:first, :params => { h[:association_col] => id }))
+ h[:klass].find(:first, :params => { h[:association_col] => id }))
end
return instance_variable_get("@#{resource}")
end
- #----------------------------------------------------------------------#
- # Define writter method for resource
- #
- #----------------------------------------------------------------------#
- define_method("#{klass_name}=") do |new_resource|
- if send(resource).blank?
- new_resource.send("#{h[:association_col]}=", id)
- instance_variable_set("@#{resource}", new_resource.save)
- else
- instance_variable_get("@#{resource}").send(:update_attribute, h[:association_col], id)
+ #----------------------------------------------------------------------#
+ # Define writter method for resource
+ #
+ #----------------------------------------------------------------------#
+ define_method("#{klass_name}=") do |new_resource|
+ if send(resource).blank?
+ new_resource.send("#{h[:association_col]}=", id)
+ instance_variable_set("@#{resource}", new_resource.save)
+ else
+ instance_variable_get("@#{resource}").send(:update_attribute, h[:association_col], id)
+ end
end
end
- end
+ def belongs_to(resource, opts = {})
+ h = hash_options(:belongs_to, resource)
+ klass_name = opts[:class_name].nil? ? resource : opts[:class_name]
+
+ #----------------------------------------------------------------------#
+ # Define accessor method for resource
+ #
+ #----------------------------------------------------------------------#
+ define_method(klass_name) do
+ if !instance_variable_defined?("@#{resource}") ||
+ instance_variable_get("@#{resource}").nil?
+ instance_variable_set("@#{resource}", h[:klass].find(h[:association_col]))
+ end
+ return instance_variable_get("@#{resource}")
+ end
+ end
end
end
View
9 activeresource/test/cases/associations_test.rb
@@ -3,6 +3,7 @@
class ProjectManager < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
+ belongs_to :project
end
class Project < ActiveResource::Base
@@ -55,5 +56,13 @@ def test_should_assign_a_new_project_manager
@other_project.project_manager = @other_project_manager
assert_equal @other_project.id, @other_project_manager.project_id
end
+
+ #----------------------------------------------------------------------
+ # belogns_to association
+ #----------------------------------------------------------------------
+
+ def test_belogns_to_should_add_a_resource_accessor
+ assert @project_manager.respond_to? :project
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.