Best place for a reusable helper method in Rails 2.2

I plan to create a method that will format time in a certain way (specialized formatting in Russian).

I would like to reuse this helper method in several models, controllers, and possibly views. I would also like to call this helper method an instance of the Time class as follows:

t=Time.now
t.my_super_shiny_helper

Question: where should I implement this helper? (module or class, where in the structure of the application directory?). After its creation, what should I call it?

I am new to Ruby / rails and try to make this work properly.

Thanks.

+3
source share
2 answers

I would add the file to lib/time_extensions.rb

class Time
  def my_shiny_helper
    ...
  end
end

config/intitializers

require 'time_extensions'

-, , , . .

+9

, Rails ( Agile Web Development) , to_s:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( 
  :russian => "%A %d %B %Y" 
) 

Time.now.to_s(:russian) #=> "Tuesday 17 February 2009"

config/environment.rb config/initializers.

+4

Source: https://habr.com/ru/post/1703362/


All Articles