Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting default currency to gbp breaks gem #413

Closed
dvdhsu opened this issue Feb 1, 2016 · 18 comments
Closed

Setting default currency to gbp breaks gem #413

dvdhsu opened this issue Feb 1, 2016 · 18 comments

Comments

@dvdhsu
Copy link

dvdhsu commented Feb 1, 2016

In my config/initializers/money.rb:

MoneyRails.configure do |config|

  # To set the default currency
  #
  config.default_currency = :gbp
end

When I try to set balance, the money-rails gem seems to be unitialized, and Rails tells me that balance= is not a defined method on my User object.

When I switch the default currency to either :eur or :usd, it works fine.

I'm on Rails 5, beta 1.

@bacrossland
Copy link

👍 I just hit this exact issue on a Rails 4.0.13 application I'm working on.

@antstorm
Copy link
Contributor

I'll take a look at that. Can you guys provide a bit more details on your setup? Schema, monetize call.

@bacrossland
Copy link

Sure. I'll get you something to reproduce it tonight.

@dvdhsu
Copy link
Author

dvdhsu commented May 11, 2016

For what it's worth, I solved this by doing this instead in my initializer:

Money.default_currency = Money::Currency.new("GBP")

@antstorm
Copy link
Contributor

@bacrossland thanks a lot!

@antstorm
Copy link
Contributor

@dvdhsu I'll check that myself as well, thanks

@bacrossland
Copy link

@antstorm I'm still working on a reproducible case. When just set up a clean rails install with only money and money_rails in the gem, I didn't have the issue. I suspect there is another gem in my Gemfile that is interfering with the load of money and money_rails when the currency is switched to GBP. I'll post more of my findings when I have it.

@antstorm
Copy link
Contributor

@bacrossland can you share your Gemfile? Might give us some clues

@dvdhsu
Copy link
Author

dvdhsu commented May 12, 2016

Here's mine -- maybe there's some intersection.

gem 'koala'
gem 'active_model_serializers'
gem 'figaro'
gem 'braintree'
gem 'money-rails'
gem 'monetize'
gem 'pg_search'
gem 'slack-notifier'
gem 'yaml_db', git: 'https://github.com/dvdhsu/yaml_db.git'
gem 'sidekiq'
gem 'redis-namespace'
gem 'one_signal'
gem 'sendgrid-ruby'

@bacrossland
Copy link

bacrossland commented May 17, 2016

Sorry, I was out sick for the last 5 days. Here is my Gemfile.

gem 'rails', '= 4.0.13'
gem 'hstore_translate'
gem 'pg', :platform => :ruby
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby

gem 'protected_attributes'                         # provides attr_accessible
gem 'actionpack-page_caching'
gem 'rails-observers'
gem 'sprockets-rails'



gem 'activemerchant'                               # payment processing
gem 'acts-as-taggable-on'                          # tagging
gem 'acts_as_list'                                 # object ordering
gem 'acts_as_tenant'                               # multi-tenancy
gem 'airbrake'                                     # error reporting
gem 'apipie-rails'                                 # API documentation
gem 'asin', '~> 2.0.1'                             # Amazon integration
gem 'axlsx'                                        # XSLX generation
gem 'asset_sync'
gem 'awesome_nested_set'
gem 'backbone-on-rails'
gem 'bootstrap-sass', '< 3.0.0'                    # we don't want a whole new bootstrap
gem 'business_time'                                # support for determining business days, quarters
gem 'cocaine'                                      # for running command lines
gem 'cocoon'                                       # nested forms
gem 'coffee-rails', '>= 3.2.1'
gem 'colorbox-rails'
gem 'countries'
gem 'devise', '~> 3.2.2'                           # authentication
gem 'dygraphs-rails'
gem 'easypost'
gem 'email_validator'                              # validates e-mail format
gem 'enumerize'                                    # enumerated attributes
gem 'eu_central_bank'                              # exchange rate fetching
gem 'evernote_oauth'
gem 'excon', '>= 0.28.0'                           # HTTP 1.1 library. Old versions report incorrect content-length, breaking paperclip+s3
gem 'faraday'                                      # http library wrapper
gem 'faraday_middleware'
gem 'fastimage'                                     # allows us to find image dimensions
gem 'fog'                                          # cloud storage integration.
gem 'font-awesome-rails', '>= 4.2.0'
#gem 'foreigner', '~> 1.6.0'                        # foreign key support
gem 'gepub', '>= 0.6.9.2'                          # epub creation
gem 'gon', '~> 4.1'                                # javascript objects
gem 'gravatar_image_tag'                           # gravatars
gem 'haml-rails'                                   # templating
gem 'haml_coffee_assets'
gem 'hominid'                                      # mailchimp integration
gem 'i18n-js'
gem 'isbn_validation'                              # isbn validation
gem 'iso-639'                                      # language codes
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'json'                                         # json support
gem 'jwt'
gem 'kaminari'                                     # pagination
gem 'liquid'                                       # customer templating
gem 'mailhopper'
gem 'marionette-rails', '~> 1.2'
gem 'maruku', '0.6.1'                              # markdown syntax support (> 0.6.1 seems to wrap the converted text in newlines)
gem 'money'                                        # money handling
gem 'money-rails'
gem 'monetize'
gem 'net-sftp'                                     # sftp support
gem 'nested-hstore'                                # Store nested hashes and other types in ActiveRecord hstores
gem 'nokogiri', '1.6.1'                            # html/xml parsing (locked because of java library inclusion bug: https://github.com/sparklemotion/nokogiri/issues/1114)
gem 'omnicontacts'                                 # contacts import
gem 'hundredth', '~> 1.0.1'
gem 'paper_trail', '>= 3.0.0.beta1'
gem 'paperclip'                                    # file attachments
gem 'permanent_records'                            # soft-delete
gem 'pg_search'                                    # postgres full-text search
gem 'prawn'                                        # PDF layout
gem 'prawn-svg', '< 0.22'
gem 'rabl', '< 0.9.0'                              # JSON API builder (locked because of bug in 0.9 that causes cover editor nodes to not load)
gem 'rack-ssl'                                     # ssl requirement
gem 'recaptcha', :require => 'recaptcha/rails'     # anti-bot
gem 'remotipart'                                   # enable ajax file uploads
gem 'responders'                                   # controller fun
gem 'rest-client'
gem 'roadie'                                       # css inlining in e-mails
gem 'rubytree',                                    # simple tree data structure. used for managing metric-calculation dependencies
  :github => 'evolve75/RubyTree'
gem 'rubyzip', '>= 1.0.0'    # zip file handling (api changes in 1.0.0)
gem 'zip-zip'
gem 'sass-rails', '>= 5.0.3'
gem 'savon', '~> 2.8.1'                            # SOAP
gem 'secure_headers'                               # prevent site framing
gem 'settingslogic'                                # application configuration
gem 'sidekiq'
gem 'sidetiq'
gem 'sidekiq-status'
gem 'simple_form'                                  # form building
gem 'simple_token_authentication'                  # devise removed token auth in 3.2.0
gem 'squeel'
gem 'stringex'                                     # string extensions
gem 'strip_attributes'                             # remove whitespace and empty strings
gem 'the_sortable_tree'
gem 'unf'                                          # Support for Unicode in S3 keys
gem 'uglifier'
gem 'whitewash'                                    # smart html sanitization
gem 'rack-cors', :require => 'rack/cors'           # CORS support
gem 'ruby-progressbar'                             # Ruby/ProgressBar: A Text Progress Bar Library for Ruby
gem 'redis-namespace'
gem 'redis-rails'
gem 'sinatra'
gem 'icalendar'

@antstorm
Copy link
Contributor

@dvdhsu @bacrossland I think I know what the problem is. Setting the default currency to GBP results in money-rails looking for a _pennies DB column instead of a _cents, because of the localization. In order to fix this, enable this in your initializer:

config.amount_column = { postfix: '_cents' }

@bacrossland
Copy link

Thanks @antstorm. I just tried your fix and still the same error occurred. You are correct that money_rails is looking for a DB column with the name _pennies in it. However, since the method set_amount_column_for_default_currency!is doing a merge! on the hash, the postfix setting being set in the config is being over written: https://github.com/RubyMoney/money-rails/blob/v1.6.1/lib/money-rails/configuration.rb#L39-L41 . It's not checking to see if anything was set in the config that should take precidence.

The issue will happen when you try to switch the default_currency to any currency that has a different subunit than the one you originally created the DB schema using. GBP, GEL, GHS, etc. can not switch between each other let alone USD. USD to EUR worked because they each have a subunit of Cents (DB column of _cents).

@antstorm
Copy link
Contributor

antstorm commented May 19, 2016

@bacrossland yep, although you can get away with making sure you change the amount_column after setting default_currency:

MoneyRails.configure do |config|
  config.default_currency = :gbp
  config.amount_column = { postfix: '_cents' }
end

It definitely requires a fix, which I will work on, but for now I think you can get away with this solution. Please tell me if that worked.

@bacrossland
Copy link

bacrossland commented May 24, 2016

@antstorm that worked. I was trying to set the column before the currency which is why it didn't work for me. Thanks!

@antstorm
Copy link
Contributor

@bacrossland great! I'll put together a fix for the config order issue as well as a more descriptive error for when it's unable to find a column

@bacrossland
Copy link

👍

@antstorm
Copy link
Contributor

#441

@antstorm
Copy link
Contributor

antstorm commented Jun 4, 2016

I will also fix order issue for configuration later, but with #441 in you should be getting a comprehensive error when amount column could not be inferred

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants