Permalink
Browse files

- Active Resource associations refactoring

  * Added method set_resource_instance_variable(resource)
  * added modules InstanceMethods and ClassMethods
  * include instead of extend in ActiveResource::Base
  • Loading branch information...
1 parent 51e9cc0 commit df72be3a686ffbb665d4b5cd7cfb695cbc056f33 @gramos committed Sep 29, 2010
Showing with 63 additions and 50 deletions.
  1. +62 −49 activeresource/lib/active_resource/associations.rb
  2. +1 −1 activeresource/lib/active_resource/base.rb
@@ -1,72 +1,85 @@
module ActiveResource
module Associations
- def hash_options(association, resource)
- h = { :klass => klass_for(association, resource) }
- h[:host_klass] = self
- case association
- when :belongs_to
- 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
+ def self.included(klass)
+ klass.send :include, InstanceMethods
+ klass.extend ClassMethods
end
- def klass_for(association, resource)
- resource = resource.to_s
- resource = resource.singularize if association == :has_many
- resource.camelize.constantize
+ module InstanceMethods
+ def set_resource_instance_variable(resource)
+ if !instance_variable_defined?("@#{resource}") ||
+ instance_variable_get("@#{resource}").nil?
+ instance_variable_set("@#{resource}", yield)
+ end
+ instance_variable_get("@#{resource}")
+ end
end
- def has_one(resource, opts = {})
- h = hash_options(:has_one, resource)
- klass_name = opts[:class_name].nil? ? resource : opts[:class_name]
+ module ClassMethods
- #----------------------------------------------------------------------#
- # 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(:first, :params => { h[:association_col] => id }))
+ def hash_options(association, resource)
+ h = { :klass => klass_for(association, resource) }
+ h[:host_klass] = self
+ case association
+ when :belongs_to
+ 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
- return instance_variable_get("@#{resource}")
+ h
+ end
+
+ def klass_for(association, resource)
+ resource = resource.to_s
+ resource = resource.singularize if association == :has_many
+ resource.camelize.constantize
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)
+ def has_one(resource, opts = {})
+ h = hash_options(:has_one, 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(:first, :params => { h[:association_col] => id })
+ end
+ 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)
+ 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]
+ 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?
+ #----------------------------------------------------------------------#
+ # Define accessor method for resource
+ #
+ #----------------------------------------------------------------------#
+ define_method(klass_name) do
association_col = send h[:association_col]
return nil if association_col.nil?
+ set_resource_instance_variable(resource){ h[:klass].find(association_col) }
instance_variable_set("@#{resource}", h[:klass].find(association_col))
end
- return instance_variable_get("@#{resource}")
end
+
end
end
end
@@ -263,7 +263,7 @@ class Base
# :singleton-method:
# The logger for diagnosing and tracing Active Resource calls.
cattr_accessor :logger
- extend Associations
+ include Associations
class << self
# Creates a schema for this resource - setting the attributes that are
# known prior to fetching an instance from the remote system.

0 comments on commit df72be3

Please sign in to comment.