4

私はお金でアクティブな管理者を使用しています - https://github.com/RubyMoney/money gem。money gem によって処理されるいくつかの属性があります。

money ジェムは値をセント単位で格納します。アクティブな管理者でエントリを作成すると、DB に正しい値が作成されます (50.00 に対して 5000)。

ただし、エントリを編集すると、値が 100 倍されます。つまり、元の入力が 50.00 の場合、AA は 5000 を表示します。お金の属性を持つものを編集すると、100 倍になります。作成時に、値はお金のロジックを通過しますが、編集時に、何らかの形でアクティブな管理者がその部分をスキップして、最終的な金額の代わりにセントを表示します。アクティブな管理者で money gem を使用する方法はありますか?

例 :

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
4

2 に答える 2

1

Railsコールバックは、この種の問題の解決策を作成するのに非常に便利です。

と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
于 2013-01-30T16:59:39.463 に答える
0

私の問題は、私の Money の使用法から来ました:

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") }

DB の価格の名前を price_cents に変更し、お金の宣言で必要なクラスに配置しました。私は価格を使用する必要があった場所でセントを使用していましたが、同じ名前を使用してお金のオブジェクトとDBのフィールドを使用しても機能しないようです。結局、問題は Active Admin とは関係ありませんでした。

于 2013-02-10T17:45:17.350 に答える