File tree Expand file tree Collapse file tree 4 files changed +30
-11
lines changed
Expand file tree Collapse file tree 4 files changed +30
-11
lines changed Original file line number Diff line number Diff line change @@ -85,11 +85,8 @@ def to_time
8585 # time formats and is only used when ActiveSupport is available.
8686 #
8787 def to_s ( format = nil )
88- if format && to_time . public_method ( :to_s ) . arity != 0
89- t0 , t1 = start_time . to_s ( format ) , end_time . to_s ( format )
90- else
91- t0 , t1 = start_time . to_s , end_time . to_s
92- end
88+ t0 = format_time ( start_time , format )
89+ t1 = format_time ( end_time , format )
9390 ( duration > 0 ) ? "#{ t0 } - #{ t1 } " : t0
9491 end
9592
@@ -98,5 +95,18 @@ def overnight?
9895 midnight = Time . new ( offset . year , offset . month , offset . day )
9996 midnight < end_time
10097 end
98+
99+ private
100+
101+ # Normalize formatted output across ActiveSupport versions:
102+ # Rails 7.1+ prefers to_fs, older versions use to_formatted_s or to_s(:format).
103+ def format_time ( time , format )
104+ return time . to_s unless format
105+ return time . to_fs ( format ) if time . respond_to? ( :to_fs )
106+ return time . to_formatted_s ( format ) if time . respond_to? ( :to_formatted_s )
107+ return time . to_s ( format ) if time . public_method ( :to_s ) . arity != 0
108+
109+ time . to_s
110+ end
101111 end
102112end
Original file line number Diff line number Diff line change 11require File . dirname ( __FILE__ ) + "/../spec_helper"
2+ require "logger"
23require "active_support"
34require "active_support/time"
45require "active_support/version"
Original file line number Diff line number Diff line change 2828 time_now = Time . current
2929 occurrence = Occurrence . new ( time_now )
3030
31- # From Rails 7.1 onwards, support for format options was removed
32- if time_now . public_method ( :to_s ) . arity != 0
33- expect ( occurrence . to_s ( :short ) ) . to eq time_now . to_s ( :short )
34- else
35- expect ( occurrence . to_s ( :short ) ) . to eq time_now . to_s
36- end
31+ # Match ActiveSupport formatting behavior across versions.
32+ expected =
33+ if time_now . respond_to? ( :to_fs )
34+ time_now . to_fs ( :short )
35+ elsif time_now . respond_to? ( :to_formatted_s )
36+ time_now . to_formatted_s ( :short )
37+ elsif time_now . public_method ( :to_s ) . arity != 0
38+ time_now . to_s ( :short )
39+ else
40+ time_now . to_s
41+ end
42+
43+ expect ( occurrence . to_s ( :short ) ) . to eq expected
3744 end
3845 end
3946
Original file line number Diff line number Diff line change 11require File . dirname ( __FILE__ ) + "/../spec_helper"
2+ require "logger"
23require "active_support"
34require "active_support/time"
45
You can’t perform that action at this time.
0 commit comments