Skip to content

Commit

Permalink
Merge pull request #75 from deepak/feature/support-inr-paise
Browse files Browse the repository at this point in the history
database schema respects the default currency
  • Loading branch information
alup committed Feb 15, 2013
2 parents 1cc92f8 + f0394eb commit 87ffa51
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ end
platforms :ruby do platforms :ruby do
gem "sqlite3" gem "sqlite3"
end end

platform :mri do
# gem "ruby-prof", "~> 0.11.2"

case RUBY_VERSION
when /^1.9/
gem 'debugger'
end
end
16 changes: 16 additions & 0 deletions lib/money-rails/configuration.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'


module MoneyRails module MoneyRails


Expand All @@ -19,16 +20,31 @@ def configure


# Configuration parameters # Configuration parameters


def default_currency
Money.default_currency
end

# Set default currency of money library # Set default currency of money library
def default_currency=(currency_name) def default_currency=(currency_name)
Money.default_currency = Money::Currency.new(currency_name) Money.default_currency = Money::Currency.new(currency_name)
set_amount_column_for_default_currency!
set_currecy_column_for_default_currency!
end end


# Register a custom currency # Register a custom currency
def register_currency=(currency_options) def register_currency=(currency_options)
Money::Currency.register(currency_options) Money::Currency.register(currency_options)
end end


def set_amount_column_for_default_currency!
amount_column.merge! postfix: "_#{default_currency.subunit.downcase.pluralize}"
end

def set_currecy_column_for_default_currency!
iso_code = default_currency.iso_code
currency_column.merge! default: iso_code
end

# Set default bank object # Set default bank object
# #
# example (given that eu_central_bank is in Gemfile): # example (given that eu_central_bank is in Gemfile):
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class Item < ActiveRecord::Base; end
describe 'currency' do describe 'currency' do
subject { Item.columns_hash['price_currency'] } subject { Item.columns_hash['price_currency'] }


its (:default) { should eq 'USD' } # set in spec/dummy/config/initializers/money.rb
its (:default) { should eq 'EUR' }

its (:null) { should be_false } its (:null) { should be_false }
its (:type) { should eq :string } its (:type) { should eq :string }
end end
Expand Down
4 changes: 3 additions & 1 deletion spec/active_record/migration_extensions/table_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Item < ActiveRecord::Base; end
describe 'currency' do describe 'currency' do
subject { Item.columns_hash['price_currency'] } subject { Item.columns_hash['price_currency'] }


its (:default) { should eq 'USD' } # set in spec/dummy/config/initializers/money.rb
its (:default) { should eq 'EUR' }

its (:null) { should be_false } its (:null) { should be_false }
its (:type) { should eq :string } its (:type) { should eq :string }
end end
Expand Down
8 changes: 8 additions & 0 deletions spec/configuration_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@
# Reset global setting # Reset global setting
MoneyRails.symbol = nil MoneyRails.symbol = nil
end end

it "changes the amount and currency column settings based on the default currency" do
MoneyRails.default_currency = :inr

MoneyRails.amount_column[:postfix].should == "_#{MoneyRails.default_currency.subunit.downcase.pluralize}"
MoneyRails.currency_column[:default].should == MoneyRails.default_currency.iso_code
end

end end
end end
Binary file modified spec/dummy/db/test.sqlite3
Binary file not shown.

0 comments on commit 87ffa51

Please sign in to comment.