Skip to content

Commit

Permalink
Rename model ChargebackRateDetailCurrency -> Currency
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Nov 28, 2019
1 parent bfb2439 commit 1a89817
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 87 deletions.
2 changes: 1 addition & 1 deletion app/models/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def self.seed
rates = cbr.delete(:rates)

rates.each do |rate_detail|
currency = ChargebackRateDetailCurrency.find_by(:code => rate_detail.delete(:type_currency))
currency = Currency.find_by(:code => rate_detail.delete(:type_currency))
field = ChargeableField.find_by(:metric => rate_detail.delete(:metric))
rate_detail[:chargeable_field_id] = field.id
if currency
Expand Down
4 changes: 2 additions & 2 deletions app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ChargebackRateDetail < ApplicationRecord
belongs_to :chargeback_rate
belongs_to :chargeable_field
belongs_to :detail_measure, :class_name => "ChargebackRateDetailMeasure", :foreign_key => :chargeback_rate_detail_measure_id
belongs_to :detail_currency, :class_name => "ChargebackRateDetailCurrency", :foreign_key => :chargeback_rate_detail_currency_id
belongs_to :detail_currency, :class_name => "Currency", :foreign_key => :chargeback_rate_detail_currency_id, :inverse_of => :chargeback_rate_detail
has_many :chargeback_tiers, :dependent => :destroy, :autosave => true

default_scope { joins(:chargeable_field).merge(ChargeableField.order(:group => :asc, :description => :asc)) }
Expand Down Expand Up @@ -278,7 +278,7 @@ def self.default_rate_details_for(rate_type)

chargeback_rate[:rates].each do |detail|
detail_new = ChargebackRateDetail.new(detail.slice(*ChargebackRateDetail::FORM_ATTRIBUTES))
detail_new.detail_currency = ChargebackRateDetailCurrency.find_by(:code => detail[:type_currency])
detail_new.detail_currency = Currency.find_by(:code => detail[:type_currency])
detail_new.metric = detail[:metric]
detail_new.chargeable_field = ChargeableField.find_by(:metric => detail.delete(:metric))

Expand Down
59 changes: 0 additions & 59 deletions app/models/chargeback_rate_detail_currency.rb

This file was deleted.

58 changes: 57 additions & 1 deletion app/models/currency.rb
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
Currency = ChargebackRateDetailCurrency
class Currency < ApplicationRecord
belongs_to :chargeback_rate_detail

validates :code, :presence => true, :length => {:maximum => 100}
validates :name, :presence => true, :length => {:maximum => 100}
validates :full_name, :presence => true, :length => {:maximum => 100}
validates :symbol, :presence => true, :length => {:maximum => 100}

has_many :chargeback_rate_detail, :foreign_key => "chargeback_rate_detail_currency_id", :inverse_of => :detail_currency, :dependent => :nullify

CURRENCY_FILE = "/currency_iso.json".freeze
FIXTURE_DIR = Money::Currency::Loader::DATA_PATH.freeze

def self.currencies_for_select
# Return a hash where the keys are the possible currencies and the values are their ids
Currency.all.each_with_object({}) do |currency, hsh|
currency_code = "#{currency.symbol} [#{currency.full_name}]"
hsh[currency_code] = currency.id
end
end

def self.currency_file_source
File.join(FIXTURE_DIR, CURRENCY_FILE)
end

def self.currencies
parse_currency_file.transform_values.map do |x|
{:code => x[:iso_code], :name => x[:name], :full_name => x[:name], :symbol => x[:symbol]}
end
end

def self.parse_currency_file
json = File.read(currency_file_source)
json.force_encoding(::Encoding::UTF_8) if defined?(::Encoding)
JSON.parse(json, :symbolize_names => true)
end

def self.seed
db_currencies = Currency.all.index_by(&:code)
if File.exist?(currency_file_source)
fixture_mtime_currency = File.mtime(currency_file_source).utc
currencies.each do |currency|
rec = db_currencies[currency[:code]]
if rec.nil?
_log.info("Creating [#{currency[:code]}] with symbols=[#{currency[:symbol]}]")
Currency.create(currency)
elsif fixture_mtime_currency > rec.created_at
rec.attributes = currency
if rec.changed?
_log.info("Updating [#{currency[:code]}] with symbols=[#{currency[:symbol]}]")
rec.update(:created_at => fixture_mtime_currency)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ServiceTemplate < ApplicationRecord

belongs_to :service_template_catalog
belongs_to :zone
belongs_to :currency, :class_name => "ChargebackRateDetailCurrency", :inverse_of => false
belongs_to :currency, :inverse_of => false

has_many :dialogs, -> { distinct }, :through => :resource_actions
has_many :miq_schedules, :as => :resource, :dependent => :destroy
Expand Down
2 changes: 1 addition & 1 deletion lib/evm_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class EvmDatabase
MiqPolicySet
ChargebackRateDetailMeasure
ChargeableField
ChargebackRateDetailCurrency
ChargebackRate
Currency

BlacklistedEvent
Classification
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :chargeback_rate_detail do
chargeback_rate
detail_currency { FactoryBot.create(:chargeback_rate_detail_currency) }
detail_currency { FactoryBot.create(:currency) }

transient do
tiers_params { nil }
Expand Down
9 changes: 0 additions & 9 deletions spec/factories/chargeback_rate_detail_currency.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/models/chargeback_rate_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
end

it "#show_rates" do
cbc = FactoryBot.create(:chargeback_rate_detail_currency, :code => "EUR")
cbc = FactoryBot.create(:currency, :code => "EUR")

cbd = FactoryBot.build(:chargeback_rate_detail_fixed_compute_cost, :detail_currency => cbc)
expect(cbd.show_rates).to eq("€ [Euro] / Day")
Expand Down
2 changes: 1 addition & 1 deletion spec/models/chargeback_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

context 'when there are valid rate details' do
let(:symbol) { '฿' }
let(:currency) { FactoryBot.create(:chargeback_rate_detail_currency, :symbol => symbol) }
let(:currency) { FactoryBot.create(:currency, :symbol => symbol) }
let(:field) { FactoryBot.create(:chargeable_field) }
let(:details) { [FactoryBot.create(:chargeback_rate_detail, :detail_currency => currency, :chargeable_field => field)] }
it { is_expected.to eq(symbol) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
describe ChargebackRateDetailCurrency do
describe Currency do
describe '.seed' do
before do
ChargebackRateDetailCurrency.seed
Currency.seed
end

let(:expected_currencies) do
%w(AED AFN ALL AMD ANG AOA ARS AUD AWG AZN BAM BBD BDT BGN BHD BIF BMD BND BOB BRL BSD BTN BWP BYN BYR BZD CAD CDF CHF CLF CLP CNY COP CRC CUC CUP CVE CZK DJF DKK DOP DZD EGP ERN ETB EUR FJD FKP GBP GEL GHS GIP GMD GNF GTQ GYD HKD HNL HRK HTG HUF IDR ILS INR IQD IRR ISK JMD JOD JPY KES KGS KHR KMF KPW KRW KWD KYD KZT LAK LBP LKR LRD LSL LYD MAD MDL MGA MKD MMK MNT MOP MRO MUR MVR MWK MXN MYR MZN NAD NGN NIO NOK NPR NZD OMR PAB PEN PGK PHP PKR PLN PYG QAR RON RSD RUB RWF SAR SBD SCR SDG SEK SGD SHP SKK SLL SOS SRD SSP STD SVC SYP SZL THB TJS TMT TND TOP TRY TTD TWD TZS UAH UGX USD UYU VES VND VUV WST XAF XAG XAU XCD XDR XOF XPD XPF XPT YER ZAR ZMK ZMW)
%w[AED AFN ALL AMD ANG AOA ARS AUD AWG AZN BAM BBD BDT BGN BHD BIF BMD BND BOB BRL BSD BTN BWP BYN BYR BZD CAD CDF CHF CLF CLP CNY COP CRC CUC CUP CVE CZK DJF DKK DOP DZD EGP ERN ETB EUR FJD FKP GBP GEL GHS GIP GMD GNF GTQ GYD HKD HNL HRK HTG HUF IDR ILS INR IQD IRR ISK JMD JOD JPY KES KGS KHR KMF KPW KRW KWD KYD KZT LAK LBP LKR LRD LSL LYD MAD MDL MGA MKD MMK MNT MOP MRO MUR MVR MWK MXN MYR MZN NAD NGN NIO NOK NPR NZD OMR PAB PEN PGK PHP PKR PLN PYG QAR RON RSD RUB RWF SAR SBD SCR SDG SEK SGD SHP SKK SLL SOS SRD SSP STD SVC SYP SZL THB TJS TMT TND TOP TRY TTD TWD TZS UAH UGX USD UYU VES VND VUV WST XAF XAG XAU XCD XDR XOF XPD XPF XPT YER ZAR ZMK ZMW]
end

it "returns supported currencies" do
expect(ChargebackRateDetailCurrency.count).to eq(164)
expect(ChargebackRateDetailCurrency.all.map(&:code)).to match_array(expected_currencies)
expect(Currency.count).to eq(164)
expect(Currency.all.map(&:code)).to match_array(expected_currencies)
end
end

it "has a valid factory" do
expect(FactoryBot.create(:chargeback_rate_detail_currency)).to be_valid
expect(FactoryBot.create(:currency)).to be_valid
end
it "is invalid without a code" do
expect(FactoryBot.build(:chargeback_rate_detail_currency, :code => nil)).not_to be_valid
expect(FactoryBot.build(:currency, :code => nil)).not_to be_valid
end
it "is invalid without a name" do
expect(FactoryBot.build(:chargeback_rate_detail_currency, :name => nil)).not_to be_valid
expect(FactoryBot.build(:currency, :name => nil)).not_to be_valid
end
it "is invalid without a full_name" do
expect(FactoryBot.build(:chargeback_rate_detail_currency, :full_name => nil)).not_to be_valid
expect(FactoryBot.build(:currency, :full_name => nil)).not_to be_valid
end
it "is invalid without a symbol" do
expect(FactoryBot.build(:chargeback_rate_detail_currency, :symbol => nil)).not_to be_valid
expect(FactoryBot.build(:currency, :symbol => nil)).not_to be_valid
end
end

0 comments on commit 1a89817

Please sign in to comment.