Skip to content

Commit 86bdabd

Browse files
committed
adding support for more versions of ActiveSupport
1 parent 6f5d6c0 commit 86bdabd

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

lib/ice_cube/occurrence.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff 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
102112
end

spec/examples/active_support_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require File.dirname(__FILE__) + "/../spec_helper"
2+
require "logger"
23
require "active_support"
34
require "active_support/time"
45
require "active_support/version"

spec/examples/occurrence_spec.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@
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

spec/examples/to_ical_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require File.dirname(__FILE__) + "/../spec_helper"
2+
require "logger"
23
require "active_support"
34
require "active_support/time"
45

0 commit comments

Comments
 (0)