From 1a898179627572d0083560320e85a4c7da37b5d8 Mon Sep 17 00:00:00 2001 From: Libor Pichler Date: Wed, 20 Nov 2019 12:29:03 +0100 Subject: [PATCH] Rename model ChargebackRateDetailCurrency -> Currency --- app/models/chargeback_rate.rb | 2 +- app/models/chargeback_rate_detail.rb | 4 +- app/models/chargeback_rate_detail_currency.rb | 59 ------------------- app/models/currency.rb | 58 +++++++++++++++++- app/models/service_template.rb | 2 +- lib/evm_database.rb | 2 +- spec/factories/chargeback_rate_detail.rb | 2 +- .../chargeback_rate_detail_currency.rb | 9 --- spec/models/chargeback_rate_detail_spec.rb | 2 +- spec/models/chargeback_rate_spec.rb | 2 +- ...tail_currency_spec.rb => currency_spec.rb} | 20 +++---- 11 files changed, 75 insertions(+), 87 deletions(-) delete mode 100644 app/models/chargeback_rate_detail_currency.rb delete mode 100644 spec/factories/chargeback_rate_detail_currency.rb rename spec/models/{chargeback_rate_detail_currency_spec.rb => currency_spec.rb} (54%) diff --git a/app/models/chargeback_rate.rb b/app/models/chargeback_rate.rb index 893cac7f91d2..035a86f64be6 100644 --- a/app/models/chargeback_rate.rb +++ b/app/models/chargeback_rate.rb @@ -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 diff --git a/app/models/chargeback_rate_detail.rb b/app/models/chargeback_rate_detail.rb index f5c4357655c3..16e0d040265d 100644 --- a/app/models/chargeback_rate_detail.rb +++ b/app/models/chargeback_rate_detail.rb @@ -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)) } @@ -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)) diff --git a/app/models/chargeback_rate_detail_currency.rb b/app/models/chargeback_rate_detail_currency.rb deleted file mode 100644 index 9d434118acab..000000000000 --- a/app/models/chargeback_rate_detail_currency.rb +++ /dev/null @@ -1,59 +0,0 @@ -class ChargebackRateDetailCurrency < 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" - - self.table_name = 'currencies' - - 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 - ChargebackRateDetailCurrency.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 = ChargebackRateDetailCurrency.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]}]") - ChargebackRateDetailCurrency.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 diff --git a/app/models/currency.rb b/app/models/currency.rb index 197e989f6d8f..68668f7cf287 100644 --- a/app/models/currency.rb +++ b/app/models/currency.rb @@ -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 diff --git a/app/models/service_template.rb b/app/models/service_template.rb index dc175d7fb137..07be67c543bb 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -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 diff --git a/lib/evm_database.rb b/lib/evm_database.rb index 0b364837d2b3..39816396d189 100644 --- a/lib/evm_database.rb +++ b/lib/evm_database.rb @@ -29,8 +29,8 @@ class EvmDatabase MiqPolicySet ChargebackRateDetailMeasure ChargeableField - ChargebackRateDetailCurrency ChargebackRate + Currency BlacklistedEvent Classification diff --git a/spec/factories/chargeback_rate_detail.rb b/spec/factories/chargeback_rate_detail.rb index 1b88fbd5948f..3e93b2bbc412 100644 --- a/spec/factories/chargeback_rate_detail.rb +++ b/spec/factories/chargeback_rate_detail.rb @@ -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 } diff --git a/spec/factories/chargeback_rate_detail_currency.rb b/spec/factories/chargeback_rate_detail_currency.rb deleted file mode 100644 index 96218382a883..000000000000 --- a/spec/factories/chargeback_rate_detail_currency.rb +++ /dev/null @@ -1,9 +0,0 @@ -FactoryBot.define do - factory :chargeback_rate_detail_currency do - code { "EUR" } - name { "Euro" } - full_name { "Euro" } - symbol { "€" } - unicode_hex { "8364" } - end -end diff --git a/spec/models/chargeback_rate_detail_spec.rb b/spec/models/chargeback_rate_detail_spec.rb index 59bef224e50f..70c7406acaff 100644 --- a/spec/models/chargeback_rate_detail_spec.rb +++ b/spec/models/chargeback_rate_detail_spec.rb @@ -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") diff --git a/spec/models/chargeback_rate_spec.rb b/spec/models/chargeback_rate_spec.rb index b301c52938af..3c6dc644d05e 100644 --- a/spec/models/chargeback_rate_spec.rb +++ b/spec/models/chargeback_rate_spec.rb @@ -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) } diff --git a/spec/models/chargeback_rate_detail_currency_spec.rb b/spec/models/currency_spec.rb similarity index 54% rename from spec/models/chargeback_rate_detail_currency_spec.rb rename to spec/models/currency_spec.rb index 4ea8ce6a9e65..8f71df809af9 100644 --- a/spec/models/chargeback_rate_detail_currency_spec.rb +++ b/spec/models/currency_spec.rb @@ -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