I have a dropdown.yml file that stores all my dropdown values for my multi-select fields in my forms. It is not environment dependent, so I do not have :development, :production, etc.
I would like cache the file into a constant so I can use across my app. I found this command.
config = Rails.application.config_for(:payment)
However, it looks like it is environment dependent. What is the best way to add the yml?
Second, should I use locales for this instead of a custom yml file?
config/dropdown.yml
dropdown:
car_model:
field1:
yes: true
no: false
region:
US: United States
CA: Canada
Also, is there a way to have a dropdown accessible from multiple names?
dropdown:
car_model| truck_model| bike_model:
field1:
yes: true
no: false
region:
US: United States
CA: Canada
So that I could reference field1, from any of the name keys, car_model, truck_model, or bike_model?
I think I would make a utility class and module for this. Something like:
Then you can do:
Wherever you like.
You'll notice I extended the model hash with a custom
DropdownExt
, so you can also do:When you do
extend(SomeModule)
on an instance, then only that instance is extended with the module so you don't poluteHash
(in this case) throughout your entire application.IMO, using
config
seems a little too low-level. But, I realize that's a matter of personal preference. Also, this will save you a little typing.Another advantage of this approach is that you get the class methods for each of your models "for free". So you can do:
instead of
I don't know. That just seems nicer to me.
Finally, I guess I like encapsulating the whole thing in a class. Again, a matter of personal preference. But, that seems more ruby-ish to me.
BTW, my YAML was wanting to change your
yes
andno
totrue
andfalse
. So, I did:Which returned
Load it in your application.rb, inside the config block:
use in code via `Rails.application.config.dropdown[:key]
note: I would drop the top level dropdown key, or add that on the end of the load line so you don't need to invoke it every time you want the config. e.g.
Could also just chuck it in a constant in your application.rb: