Skip to content
Browse files

fix bug in has_many which led to incorrect class creation.

basically has_many :friends => :user would create the class Use. So if a hash is passed as params, we just do a singular transform on the symbol to generate the class.
  • Loading branch information...
1 parent bc4232a commit f32db9423f46ae3358b08e2ab082d33ef4f66a8a @clayallsopp committed May 9, 2012
Showing with 10 additions and 5 deletions.
  1. +10 −5 lib/remote_model.rb
View
15 lib/remote_model.rb
@@ -15,7 +15,7 @@ class << self
# self.has_one :question, :answer, :camel_case
# => {:question => Question, :answer => Answer, :camel_case => CamelCase}
def has_one(params = [])
- make_fn_lookup "has_one", params, lambda { |sym| sym.to_s.split("_").collect {|s| s.capitalize}.join }
+ make_fn_lookup "has_one", params, singular_klass_str_lambda
end
# EX
@@ -29,7 +29,7 @@ def has_many(params = [])
# self.belongs_to :question, :answer, :camel_case
# => {:question => Question, :answer => Answer, :camel_case => CamelCase}
def belongs_to(params = [])
- make_fn_lookup "belongs_to", params, lambda { |sym| sym.to_s.split("_").collect {|s| s.capitalize}.join }
+ make_fn_lookup "belongs_to", params, singular_klass_str_lambda
end
def pluralize
@@ -51,6 +51,10 @@ def make_klass(klass_str)
end
end
+ def singular_klass_str_lambda
+ lambda { |sym| sym.to_s.split("_").collect {|s| s.capitalize}.join }
+ end
+
# How we fake define_method, essentially.
# ivar_suffix -> what is the new @ivar called
# params -> the :symbols to map to classes
@@ -63,17 +67,18 @@ def make_fn_lookup(ivar_suffix, params, transform)
sym_to_klass_sym = {}
if params.class == Symbol
- sym_to_klass_sym[params] = params
+ sym_to_klass_sym[params] = transform.call(params)
elsif params.class == Array
params.each {|klass_sym|
- sym_to_klass_sym[klass_sym] = klass_sym
+ sym_to_klass_sym[klass_sym] = transform.call(klass_sym)
}
else
+ params.each { |fn_sym, klass_sym| params[fn_sym] = singular_klass_str_lambda.call(klass_sym) }
sym_to_klass_sym = params
end
sym_to_klass_sym.each do |relation_sym, klass_sym|
- klass_str = transform.call(klass_sym)
+ klass_str = klass_sym.to_s
instance_variable_get(ivar)[relation_sym] = make_klass(klass_str)
end

0 comments on commit f32db94

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