Browse files

- ActiveResource Associations, better name for hash_options

  • Loading branch information...
1 parent 6a1507c commit 7ab233745f0074c66bf791a1faeb3986c3a4d1a7 @gramos committed Sep 29, 2010
Showing with 17 additions and 17 deletions.
  1. +17 −17 activeresource/lib/active_resource/associations.rb
View
34 activeresource/lib/active_resource/associations.rb
@@ -18,17 +18,17 @@ def set_resource_instance_variable(resource, default_value = nil)
module ClassMethods
- def hash_options(association, resource)
- h = { :klass => klass_for(association, resource) }
- h[:host_klass] = self
+ def options(association, resource)
+ o = { :klass => klass_for(association, resource) }
+ o[:host_klass] = self
case association
when :belongs_to
- h[:association_col] = "#{h[:klass].to_s.underscore}_id".to_sym
+ o[:association_col] = "#{o[:klass].to_s.underscore}_id".to_sym
when :has_one
- h[:association_col] = "#{h[:host_klass].to_s.underscore}_id".to_sym
+ o[:association_col] = "#{o[:host_klass].to_s.underscore}_id".to_sym
end
- h
+ o
end
def klass_for(association, resource)
@@ -44,15 +44,15 @@ def klass_for(association, resource)
#
def has_one(resource, opts = {})
- h = hash_options(:has_one, resource)
+ o = options(:has_one, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
set_resource_instance_variable(resource) do
- h[:klass].find(:first, :params => { h[:association_col] => id })
+ o[:klass].find(:first, :params => { o[:association_col] => id })
end
end
@@ -62,10 +62,10 @@ def has_one(resource, opts = {})
#----------------------------------------------------------------------#
define_method("#{resource}=") do |new_resource|
if send(resource).blank?
- new_resource.send("#{h[:association_col]}=", id)
+ new_resource.send("#{o[:association_col]}=", id)
instance_variable_set("@#{resource}", new_resource.save)
else
- instance_variable_get("@#{resource}").send(:update_attribute, h[:association_col], id)
+ instance_variable_get("@#{resource}").send(:update_attribute, o[:association_col], id)
end
end
end
@@ -75,25 +75,25 @@ def has_one(resource, opts = {})
#
def belongs_to(resource, opts = {})
- h = hash_options(:belongs_to, resource)
+ o = options(:belongs_to, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
- association_col = send h[:association_col]
+ association_col = send o[:association_col]
return nil if association_col.nil?
- set_resource_instance_variable(resource){ h[:klass].find(association_col) }
+ set_resource_instance_variable(resource){ o[:klass].find(association_col) }
end
#----------------------------------------------------------------------#
# Define writter method for resource
#
#----------------------------------------------------------------------#
define_method("#{resource}=") do |new_resource|
- if send(h[:association_col]) != new_resource.id
- send(:update_attribute, h[:association_col], new_resource.id)
+ if send(o[:association_col]) != new_resource.id
+ send(:update_attribute, o[:association_col], new_resource.id)
end
instance_variable_set("@#{resource}", new_resource)
end
@@ -104,15 +104,15 @@ def belongs_to(resource, opts = {})
#
def has_many(resource, opts = {})
- h = hash_options(:has_many, resource)
+ o = options(:has_many, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
set_resource_instance_variable(resource) do
- h[:klass].find(:all, :params => { h[:association_col] => id })
+ o[:klass].find(:all, :params => { o[:association_col] => id })
end
end
end

0 comments on commit 7ab2337

Please sign in to comment.