Where to put methods that make date formatting simple

I have two models: BusinessHour and StaffHour, which inherit from the Hour.

In the BusinessHour and StaffHour models, I do a “search” to get the requested hours. After that, I need to format each time in a specific format, for which I have a method. I am trying to figure out where to put this method. It seems like it will go to Helper, in which case I will need to call it from the BusinessHour and StaffHour controllers AFTER they call the model to retrieve the data.

Otherwise, I can put the formatting method in the Hour model and call it directly from the BusinessHour and StaffHour models, since they both inherit from the Hour.

What is the recommended way?

Here's the method:

  def self.format_hours_for_display(hours)
    hours_for_day = Hash.new("")
    hours.each do |h|
      h.start_time  = format_time(h.start_time)
      h.stop_time   = format_time(h.stop_time)
      hours_for_day[h.day] = h
    end  
    hours_for_day
  end
+3
2

to_s . .rb config/initializers - :

formats = {:short_full_year => '%m/%d/%Y', :short_datetime => '%m/%d/%Y %I:%M%p'}

Date::DATE_FORMATS.update(formats)
Time::DATE_FORMATS.update(formats)

to_s , . :

time = Time.now
time.to_s(:short_full_year)
+4

, , format_hours_for_display . Rails group_by , , , , :

hours.group_by(&:day).each do |day, h|
  puts h.start_time.to_s(:custom_format)
  puts h.stop_time.to_s(:custom_format)
end
0

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


All Articles