Skip to content

Commit

Permalink
Merge pull request #13860 from isimluk/chargeback-extract-currency_sy…
Browse files Browse the repository at this point in the history
…mbol

Chargeback refactor: extract currency_symbol method
  • Loading branch information
kbrock authored Feb 10, 2017
2 parents 14e35fe + 53d868d commit cb46b9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/chargeback/rates_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def currency_for_report
# A very problematic way to get currency info when formatting a chargeback report.
# The only right way is to carry currency info. TBD.
rate = ChargebackRate.get_assignments(:compute)[0] || ChargebackRate.get_assignments(:storage)[0]
rate[:cb_rate].chargeback_rate_details[0].detail_currency.symbol unless rate.nil?
rate[:cb_rate].currency_symbol unless rate.nil?
end
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ChargebackRate < ApplicationRecord
validates_uniqueness_of :guid
validates_uniqueness_of :description, :scope => :rate_type

delegate :symbol, :to => :currency, :prefix => true, :allow_nil => true

VALID_CB_RATE_TYPES = ["Compute", "Storage"]

def rate_details_relevant_to(report_cols)
Expand Down Expand Up @@ -188,6 +190,12 @@ def assigned_tags?

private

def currency
# Note that the currency should be relation to ChargebackRate, not ChargebackRateDetail. We cannot work
# with various currencies within single ChargebackRate. This is to be fixed later in series of db migrations.
chargeback_rate_details.first.try(:detail_currency)
end

def ensure_unassigned
if assigned?
errors.add(:rate, "rate is assigned and cannot be deleted")
Expand Down
17 changes: 17 additions & 0 deletions spec/models/chargeback_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,21 @@
expect(cbr.errors.count).to be(0)
end
end

describe '#currency_symbol' do
let(:rate) { FactoryGirl.build(:chargeback_rate, :chargeback_rate_details => details) }
subject { rate.currency_symbol }

context 'when there are no rate details' do
let(:details) { [] }
it { is_expected.to be_nil }
end

context 'when there are valid rate details' do
let(:symbol) { '฿' }
let(:currency) { FactoryGirl.create(:chargeback_rate_detail_currency, :symbol => symbol) }
let(:details) { [FactoryGirl.create(:chargeback_rate_detail, :detail_currency => currency)] }
it { is_expected.to eq(symbol) }
end
end
end

0 comments on commit cb46b9b

Please sign in to comment.