last day (15 days later) » 

7:23 PM
1
A: Loading a YML file in Rails, and maybe using a i18n file instead

jvillianI think I would make a utility class and module for this. Something like: module DropdownExt def self.extended(receiver) receiver.each do |k,v| define_method(k) do v.is_a?(Hash) ? v.extend(DropdownExt) : v end end end end class Dropdowns class << self ...

 
I really like this. Is there a way in my yml file to may a specific dropdown accessible from multiple methods? See above update.
 
Sure. I updated my answer to give you Dropdowns.truck_model and Dropdowns.bike_model. Instead of handling it in the YAML file, I aliased the class methods. (Doesn't this seem like the right answer? ;-))
 
How would you treak it to do Dropdowns.car_model.field1.yes, I assume a recursive function of some type?
 
Updated my answer. Seriously, shouldn't this be the accepted answer?
 
Yes, thank you, great answer and help!
 
7:23 PM
Glad it helped. BTW, thinking about it a little more, I think it's more spiffy to use a module to extend the functionality rather than using a wrapper class. I updated my answer to reflect this.
 
Are you saying I could extend the Car_model? with extend('DropdownExt')? so I could do something like a= Car.new(), then a.dropdown.field1?
 
Hi
 
No. DropdownExt extends each hash in car_model so that it has methods like 'field1'.
 
Can you give an an example? and BTW: THANK YOU!
 
7:28 PM
Otherwise, you'd have to do something like Dropdowns.car_model[:field1][:yes] instead of Dropdowns.car_model.field1.yes
 
However, I do not actually need to add extend to my model
it's all set in the code specific to dropdown
I think I understand.
 
Yes, that is correct.
 
Great, thank you
 
It's all just so that you can use dot notiation instead of [] notation. It's total candy.
 
I really appreciate all your help!
 
7:31 PM
Sure thing. BTW, where are you from? I'm always interested where we all come from.
 
Chicago
you?
 
I think the module approach is better than the prior class approach because you don't have to do Dropdowns.car_model.hsh to get at the original hash.
 
I agree.
 
So, if you want, you can still do stuff like Dropdowns.car_model.keys and get back field1 and region
San Francisco area
 
that's great.
love SF
 
7:33 PM
yeah, I'm a little South. But, general area.
 
Foster City?
 
San Mateo
OK. Have a good one. Back to my own coding now. Thanks for an interesting question!
 
thank you
 
 
1 hour later…
8:42 PM
I have tried using your example and I get Psych::SyntaxError ((<unknown>): mapping values are not allowed in this context at line 26 column 10), any idea?
thinking I need to convert and sentences into strings so adding quotes and retesting
 
9:01 PM
I fixed my invalid yml, but now getting NoMethodError (undefined method `each' for nil:NilClass)
I think the issue is with this part ```dropdowns_spec[:dropdowns].each do |k,v|
define_singleton_method k do
v.extend(DropdownExt)
end
end```
If you have any ideas, please let me know
got it working!
thanks
 

last day (15 days later) »