From 702a551a0391b314bc11b791fda4921c4ced875a Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Mon, 24 Aug 2020 14:30:26 +0200 Subject: [PATCH] Prepare for newer rubies --- .ruby-version | 2 +- lib/payday/invoice.rb | 4 ++-- lib/payday/invoiceable.rb | 2 +- lib/payday/line_item.rb | 4 ++-- lib/payday/money.rb | 1 + lib/payday/pdf_renderer.rb | 2 +- spec/invoice_spec.rb | 12 ++++++------ spec/line_item_spec.rb | 8 ++++---- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.ruby-version b/.ruby-version index cd57a8b..338a5b5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.1.5 +2.6.6 diff --git a/lib/payday/invoice.rb b/lib/payday/invoice.rb index f0137db..be46b7e 100644 --- a/lib/payday/invoice.rb +++ b/lib/payday/invoice.rb @@ -28,12 +28,12 @@ def initialize(options = {}) # The tax rate that we're applying, as a BigDecimal def tax_rate=(value) - @tax_rate = BigDecimal.new(value.to_s) + @tax_rate = BigDecimal((value || 0).to_s) end # Shipping rate def shipping_rate=(value) - @shipping_rate = BigDecimal.new(value.to_s) + @shipping_rate = BigDecimal((value || 0).to_s) end end end diff --git a/lib/payday/invoiceable.rb b/lib/payday/invoiceable.rb index ff26a5d..6dba07e 100644 --- a/lib/payday/invoiceable.rb +++ b/lib/payday/invoiceable.rb @@ -20,7 +20,7 @@ def bill_to # Calculates the subtotal of this invoice by adding up all of the line items def subtotal - line_items.reduce(BigDecimal.new("0")) { |result, item| result += item.amount } + line_items.reduce(BigDecimal("0")) { |result, item| result += item.amount } end # The tax for this invoice, as a BigDecimal diff --git a/lib/payday/line_item.rb b/lib/payday/line_item.rb index 89bf132..bd57bdb 100644 --- a/lib/payday/line_item.rb +++ b/lib/payday/line_item.rb @@ -20,12 +20,12 @@ def initialize(options = {}) # Sets the quantity of this {LineItem} def quantity=(value) - @quantity = BigDecimal.new(value.to_s) + @quantity = BigDecimal((value || 0).to_s) end # Sets the price for this {LineItem} def price=(value) - @price = BigDecimal.new(value.to_s) + @price = BigDecimal((value || 0).to_s) end end end diff --git a/lib/payday/money.rb b/lib/payday/money.rb index 452a5f5..e02c7e5 100644 --- a/lib/payday/money.rb +++ b/lib/payday/money.rb @@ -1 +1,2 @@ Money.locale_backend = :i18n +Money.rounding_mode = BigDecimal::ROUND_HALF_UP diff --git a/lib/payday/pdf_renderer.rb b/lib/payday/pdf_renderer.rb index 2b8d92b..b3fa893 100644 --- a/lib/payday/pdf_renderer.rb +++ b/lib/payday/pdf_renderer.rb @@ -194,7 +194,7 @@ def self.line_items_table(invoice, pdf) invoice.line_items.each do |line| table_data << [line.description, (line.display_price || number_to_currency(line.price, invoice)), - (line.display_quantity || BigDecimal.new(line.quantity.to_s).to_s("F")), + (line.display_quantity || BigDecimal(line.quantity.to_s).to_s("F")), number_to_currency(line.amount, invoice)] end diff --git a/spec/invoice_spec.rb b/spec/invoice_spec.rb index e748ce9..74c4595 100644 --- a/spec/invoice_spec.rb +++ b/spec/invoice_spec.rb @@ -17,9 +17,9 @@ module Payday expect(i.ship_to).to eq("There") expect(i.notes).to eq("These are some notes.") expect(i.line_items[0].description).to eq("Shirts") - expect(i.shipping_rate).to eq(BigDecimal.new("15.00")) + expect(i.shipping_rate).to eq(BigDecimal("15.00")) expect(i.shipping_description).to eq("USPS Priority Mail:") - expect(i.tax_rate).to eq(BigDecimal.new("0.125")) + expect(i.tax_rate).to eq(BigDecimal("0.125")) expect(i.tax_description).to eq("Local Sales Tax, 12.5%") expect(i.invoice_date).to eq(Date.civil(1993, 4, 12)) end @@ -37,14 +37,14 @@ module Payday # $1000 in Hats i.line_items << LineItem.new(price: 5, quantity: 200, description: "Hats") - expect(i.subtotal).to eq(BigDecimal.new("1130")) + expect(i.subtotal).to eq(BigDecimal("1130")) end it "should calculate the correct tax rounded to two decimal places" do i = Invoice.new(tax_rate: 0.1) i.line_items << LineItem.new(price: 20, quantity: 5, description: "Pants") - expect(i.tax).to eq(BigDecimal.new("10")) + expect(i.tax).to eq(BigDecimal("10")) end it "shouldn't apply taxes to invoices with subtotal <= 0" do @@ -52,7 +52,7 @@ module Payday i.line_items << LineItem.new(price: -1, quantity: 100, description: "Negative Priced Pants") - expect(i.tax).to eq(BigDecimal.new("0")) + expect(i.tax).to eq(BigDecimal("0")) end it "should calculate the total for an invoice correctly" do @@ -68,7 +68,7 @@ module Payday # $1000 in Hats i.line_items << LineItem.new(price: 5, quantity: 200, description: "Hats") - expect(i.total).to eq(BigDecimal.new("1243")) + expect(i.total).to eq(BigDecimal("1243")) end it "is overdue when it's past date and unpaid" do diff --git a/spec/line_item_spec.rb b/spec/line_item_spec.rb index 39ab600..dbc6a4b 100644 --- a/spec/line_item_spec.rb +++ b/spec/line_item_spec.rb @@ -3,13 +3,13 @@ module Payday describe LineItem do it "should be able to be initilized with a price" do - li = LineItem.new(price: BigDecimal.new("20")) - expect(li.price).to eq(BigDecimal.new("20")) + li = LineItem.new(price: BigDecimal("20")) + expect(li.price).to eq(BigDecimal("20")) end it "should be able to be initialized with a quantity" do li = LineItem.new(quantity: 30) - expect(li.quantity).to eq(BigDecimal.new("30")) + expect(li.quantity).to eq(BigDecimal("30")) end it "should be able to be initlialized with a description" do @@ -19,7 +19,7 @@ module Payday it "should return the correct amount" do li = LineItem.new(price: 10, quantity: 12) - expect(li.amount).to eq(BigDecimal.new("120")) + expect(li.amount).to eq(BigDecimal("120")) end end end