Skip to content

Commit

Permalink
Swap to_fs & to_formatted_s alias
Browse files Browse the repository at this point in the history
Since rails/rails#44354 implements `to_formatted_s` as an alias for `to_fs`, it
makes sense for us to do it the same way.
  • Loading branch information
sambostock committed Mar 17, 2022
1 parent 79bcc50 commit f264bd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def to_d
value
end

def to_formatted_s(style = nil)
def to_fs(style = nil)
units = case style
when :legacy_dollars
2
Expand All @@ -241,8 +241,8 @@ def to_formatted_s(style = nil)
sprintf("%s%d.%0#{units}d", sign, rounded_value.truncate, rounded_value.frac * (10 ** units))
end
end
alias_method :to_s, :to_formatted_s
alias_method :to_fs, :to_formatted_s
alias_method :to_s, :to_fs
alias_method :to_formatted_s, :to_fs

def to_json(options = nil)
if (options.is_a?(Hash) && options.delete(:legacy_format)) || Money.config.legacy_json_format
Expand Down
24 changes: 12 additions & 12 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@
expect(non_fractional_money.to_s).to eq("1")
end

it "to_formatted_s with a legacy_dollars style" do
expect(amount_money.to_formatted_s(:legacy_dollars)).to eq("1.23")
expect(non_fractional_money.to_formatted_s(:legacy_dollars)).to eq("1.00")
it "to_fs with a legacy_dollars style" do
expect(amount_money.to_fs(:legacy_dollars)).to eq("1.23")
expect(non_fractional_money.to_fs(:legacy_dollars)).to eq("1.00")
end

it "to_formatted_s with a amount style" do
expect(amount_money.to_formatted_s(:amount)).to eq("1.23")
expect(non_fractional_money.to_formatted_s(:amount)).to eq("1")
it "to_fs with a amount style" do
expect(amount_money.to_fs(:amount)).to eq("1.23")
expect(non_fractional_money.to_fs(:amount)).to eq("1")
end

it "to_s correctly displays negative numbers" do
Expand All @@ -104,16 +104,16 @@
expect(Money.new("999999999999999999.99", "USD").to_s).to eq("999999999999999999.99")
end

it "to_formatted_s raises ArgumentError on unsupported style" do
expect{ money.to_formatted_s(:some_weird_style) }.to raise_error(ArgumentError)
it "to_fs raises ArgumentError on unsupported style" do
expect{ money.to_fs(:some_weird_style) }.to raise_error(ArgumentError)
end

it "to_formatted_s is aliased as to_s for backward compatibility" do
expect(money.method(:to_s)).to eq(money.method(:to_formatted_s))
it "to_fs is aliased as to_s for backward compatibility" do
expect(money.method(:to_s)).to eq(money.method(:to_fs))
end

it "to_formatted_s is aliased as to_fs for forward compatibility" do
expect(money.method(:to_fs)).to eq(money.method(:to_formatted_s))
it "to_fs is aliased as to_formatted_s for backward compatibility" do
expect(money.method(:to_formatted_s)).to eq(money.method(:to_fs))
end

it "legacy_json_format makes as_json return the legacy format" do
Expand Down

0 comments on commit f264bd6

Please sign in to comment.