I am using active admin with the money – https://github.com/RubyMoney/money gem. I have some attributes handled by the money gem.
The money gem stores values in cents. When i create an entry with active admin, the correct value is created in DB (5000 for 50.00).
However when i edit an entry, the value is multiplied by 100, meaning that AA display 5000 for an original input of 50.00. If i edit anything with a money attribute, it will multiply by 100. At creation, the value goes through money logic, but at edition, somehow active admin skip that part displaying cents instead of the final monetary value.
Is there a way to use the money gem with active admin?
example :
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Products" do
......
f.has_many :pricings do |p|
p.input :price
p.input :_destroy, :as => :boolean,:label=>"Effacer"
end
f.actions :publish
end
Model :
# encoding: utf-8
class Pricing < ActiveRecord::Base
belongs_to :priceable, :polymorphic => true
attr_accessible :price
composed_of :price,
:class_name => "Money",
:mapping => [%w(price cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
end
My problem came from my usage of Money :
I renamed my price in DB by price_cents, and i put it in the class where it was needed in the money declaration. I was using cent where i should have used price, and even then having the money object and the field in DB using the same name doesn’t seem to work. In the end the problem was not related to Active Admin.