2012-12-11 7 views
4

나는 적극적인 관리자와 money - https://github.com/RubyMoney/money 보석을 사용하고 있습니다. 돈 보석에 의해 처리되는 속성이 있습니다.적극적인 관리자와 비용

돈 보석은 센트로 값을 저장합니다. 활성 admin으로 항목을 만들면 DB에 올바른 값이 생성됩니다 (50.00은 5000).

그러나 항목을 편집 할 때 값에 100이 곱해집니다. 즉, 50.00의 원래 입력에 대해 AA 디스플레이 5000을 의미합니다. 내가 돈 속성을 가지고 뭔가를 편집하면 100으로 배가 될 것입니다. 생성시, 값은 돈 로직을 거치지 만 편집에서는 어떻게 든 활성화 된 관리자가 최종 금전적 가치 대신 센트를 표시하는 부분을 건너 뜁니다. 액티브 관리자와 함께 돈을 사용하는 방법이 있습니까?

예 :

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 

모델 :

# 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 
+2

https://github.com/collectiveidea/money 또는 https://github.com/RubyMoney/money에서 돈을 보석으로 연결되는 링크를 제공 할 수 있습니까? 또한 모델의 소스 코드가 좋을 것입니다. – Fivell

답변

0

내 문제는 돈의 내 사용에서 온 :

composed_of :price, 
    :class_name => "Money", 
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)], 
    :constructor => Proc.new { |price_cents, currency| Money.new(price_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") } 

내가 price_cents에 의해 DB 내 가격을 이름을 변경하고, 나는 그것이 필요했다 클래스에 넣어 돈 선언. 나는 가격을 사용 했어야하는 센트를 사용하고 있었고 심지어 같은 이름을 사용하는 DB의 필드와 돈 오브젝트를 가지고도 작동하지 않는 것 같습니다. 결국 문제는 Active Admin과 관련이 없습니다.

1

Rails callbacks는 문제의이 종류에 대한 해결책을 만들기위한 매우 편리합니다.

콜백 만 사용하고 after_update를 사용합니다.

예 :

# encoding: utf-8 
    class Pricing < ActiveRecord::Base 
    after_update :fix_price 
    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") } 

    def fix_price 
    self.price = (self.price/100) 
    end 
    end