Reputation: 2578
I would like to have a new method for Date class in my Ruby 2.0 Rails 4 application. Adding a new like 'date_extensions.rb' in /lib used to work in another Ruby 1.9 Rails 3.2 app but not here. The extension is pretty simple now:
class Date
def week_day
self.wday == 0 ? 7 : self.wday
end
end
I do not like to put it in initializer as it keeps growing. Is there a good workaround?
Upvotes: 2
Views: 889
Reputation: 18037
At some point, Rails stopped adding the lib
directory to the autoload paths. The other app had probably set a setting to autoload the lib
directory and your new app doesn't have that (yet?). So that's why you're having to require the file directly via an initializer. For what it's worth: I think this is the better approach -- only load code when you need it. I usually add a config/initializers/application.rb
and then add requires such as require "date_extensions"
in there. Then the date_extensions.rb file goes in the lib folder as you suggest.
Upvotes: 2