Skip to content
Browse files

database schema respects the default currency

when default currency is changed. the amount column in the database
has a postfix of the subunit of the currency so set

likewise for the currency column as well. It also changes the
default value to the currencies iso code

add gem debugger for ruby 1.9 and ruby-debug for ruby 1.8
  • Loading branch information...
1 parent 1fdd5fe commit 2961b0f7bb0745ebaed9b4b8a56b63404d89e023 @deepak committed Jan 31, 2013
Showing with 35 additions and 0 deletions.
  1. +11 −0 Gemfile
  2. +16 −0 lib/money-rails/configuration.rb
  3. +8 −0 spec/configuration_spec.rb
  4. BIN spec/dummy/db/test.sqlite3
View
11 Gemfile
@@ -11,3 +11,14 @@ end
platforms :ruby do
gem "sqlite3"
end
+
+platform :mri do
+ # gem "ruby-prof", "~> 0.11.2"
+
+ case RUBY_VERSION
+ when /^1.8/
+ gem 'ruby-debug'
+ when /^1.9/
+ gem 'debugger'
+ end
+end
View
16 lib/money-rails/configuration.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/attribute_accessors'
+require 'active_support/core_ext/string/inflections'
module MoneyRails
@@ -19,16 +20,31 @@ def configure
# Configuration parameters
+ def default_currency
+ Money.default_currency
+ end
+
# Set default currency of money library
def default_currency=(currency_name)
Money.default_currency = Money::Currency.new(currency_name)
+ set_amount_column_for_default_currency!
+ set_currecy_column_for_default_currency!
end
# Register a custom currency
def register_currency=(currency_options)
Money::Currency.register(currency_options)
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
#
# example (given that eu_central_bank is in Gemfile):
View
8 spec/configuration_spec.rb
@@ -50,5 +50,13 @@
# Reset global setting
MoneyRails.symbol = nil
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
View
BIN spec/dummy/db/test.sqlite3
Binary file not shown.

0 comments on commit 2961b0f

Please sign in to comment.
Something went wrong with that request. Please try again.