Reputation: 939
Right now, I have the following application_helper.rb
method:
module ApplicationHelper
def grades_k_5
[
['',],
['Preschool', -1],
['Kindergarten', 0],
['First grade', 1],
['Second grade', 2],
['Third grade', 3],
['Fourth grade', 4],
['Fifth grade', 5],
]
end
Up until now, I've only used it in views (to populate a dropdown selector), but now I'd also like to use it in one of my models. How can I take that array and put it somewhere so that I can reference it from this helper method, and also another method that I'd include in my model? Ultimately, what I'm looking for is one place (DRY) to change this if I ever need to modify the structure rather than having to go to multiple methods.
Upvotes: 0
Views: 108
Reputation: 230356
Many options exist for static data like this. The simplest way is a class method.
class StaticData
def self.grades_k_5
[
['',],
['Preschool', -1],
['Kindergarten', 0],
['First grade', 1],
['Second grade', 2],
['Third grade', 3],
['Fourth grade', 4],
['Fifth grade', 5],
]
end
end
# then
StaticData.grades_k_5
You can also put it into a YAML file and have a piece of logic that parses that file and gives you the data. But it's a bit more complicated and probably not needed in your case.
Upvotes: 2