-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19538 from lpichler/rename_ChargebackRateDetailCu…
…rrencyToCurrency Rename: ChargebackRateDetailCurrency -> Currency
- Loading branch information
Showing
10 changed files
with
73 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 8 additions & 9 deletions
17
...s/chargeback_rate_detail_currency_spec.rb → spec/models/currency_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
describe ChargebackRateDetailCurrency do | ||
describe Currency do | ||
describe '.seed' do | ||
it "returns supported currencies" do | ||
ChargebackRateDetailCurrency.seed | ||
Currency.seed | ||
|
||
expected_currencies = %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 MRU 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 UZS VES VND VUV WST XAF XAG XAU XCD XDR XOF XPD XPF XPT YER ZAR ZMK ZMW] | ||
|
||
expect(ChargebackRateDetailCurrency.all.map(&:code)).to match_array(expected_currencies) | ||
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 |