Last active
July 6, 2017 23:59
-
-
Save sas1ni69/fa3085ffc65d565292fa47b7de8eeaf4 to your computer and use it in GitHub Desktop.
Extending the monetize gem to use jsonb attributes. (Currency not included)
This file contains hidden or 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
module MonetizeJsonb | |
def monetize_jsonb(*attrs) | |
attrs.each do |attr_name| | |
define_method("#{attr_name}=") do |value| | |
value_in_cents = Monetize.parse(value, account.currency).cents | |
self.send("#{attr_name}_cents=", value_in_cents) | |
end | |
define_method(attr_name) do | |
Money.new(self.send("#{attr_name}_cents"), account.currency) | |
end | |
end | |
end | |
end | |
# monetize_jsonb :price, :discounted_price | |
# store_accessor :price_attributes, :price_cents, :discounted_price_cents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment