Skip to content

Instantly share code, notes, and snippets.

@sas1ni69
Last active July 6, 2017 23:59
Show Gist options
  • Save sas1ni69/fa3085ffc65d565292fa47b7de8eeaf4 to your computer and use it in GitHub Desktop.
Save sas1ni69/fa3085ffc65d565292fa47b7de8eeaf4 to your computer and use it in GitHub Desktop.
Extending the monetize gem to use jsonb attributes. (Currency not included)
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