Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Rails: add support for :decimal field instead of cents/integers #25
I agree with gamov.
No longer than yesterday I had to implement a new routine in one Rails app to deal with money. Indeed, users can't play with cents. I can't ask to an user "Enter here your order price in cents".
While I was working on the feature, I realized that Money could probably be improved on this side. I'd like to propose a few additional methods:
Equivalent to String.to_s.to_money or viceversa.
Ideally, I would be able to call
Money.new_from_decimal([Fixnum/Float], ...) (or ala Objective-C new_with_decimal)
Gets/Sets the value in Fixnum/Float.
I'm sure these methods will significantly improve the library.
What about just enhancing
Honestly, I would consider to keep them separate.
For instance, it would be hard to understand if Money.new(10) should be 10 cents or 10 dollars.
I believe using separate method provides additional security and readability, in this case.
Okay, I can agree with that. How about something like this then?
They're unambiguous, and we can easily add more methods in the future. In this case I wouldn't use
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
I agree with weppos that it must be clear when Money will deal with :decimal instead of cents.
Why not #from_dec? It seems you consider the evil float equal to the righteous Bigdecimal.
Now that I have a few minutes to stop and relax, I'm reviewing all the comments to this issue to organize the changes.
I'd like to summarize all the activities for a final feedback. Honestly, there's also a small piece of these changes that doesn't please me very much. I'm going to explain why.
First, the goal of this ticket: add support for getting/setting/creating Money objects from a
The idea is to have something like
Here's my first question. I'm not a native English/American speaker so forgive me if this is a stupid question. We initially agreed on using
This word will be consistently use across the entire library, so that we can implement
Needless to say, the
Second question is: do we really need a list of
In my opinion, we should abstract the money implementation from a specific data type and the ticket #27 is an excellent demonstration.
I believe Money should provide methods to create money from "concepts", rather than from "types".
What does it means in practice?
First, we can provide a reasonable set of allowed types when defining a method API. For instance,
Then, we can add more "meaningful" methods to extend Money usage always trying to work with concepts rather than types (Ruby is not really a strongly typed language).
There's an small exception to my feedback. We can think about adding more
String deserves a special mention here because a
This is the reason why we can try to provide two methods:
The idea behind this change is to clean up the core_extensions by moving all the logic into the
and update the core extensions accordingly.
We can also decide to move the section 2 to an other ticket.
Sorry for the very long comment.
I like the ideas, but the word
I'd also like to see a separate ticket (and set of patches) submitted for the core extensions cleanup.
I don't like very much the word
Any feedback before the merge?
There are a couple of
Units is ambiguous, it can refer to either dollars and cents. I understands that dollars is specific to a handful of countries (approximately 36 countries use the dollar to refer to their whole unit of money), but so is cents (approximately 42 countries). I'd rather use a term that a majority of people understand and can reason about, than something more ambiguous that everyone would be forced to look at documentation for.
If dollars/cents is an issue, we could always add two new keys to the Currency table for