How to check composite ActiveRecord class?

I like to use compos_of in my Rails applications, it helps me to create beautiful problem space objects from raw database data.

I have a problem though, what is the best way to test them, ideally using the automatic ActiveRecord procedure?

Sometimes checking the source data is enough, but often the object is too complicated for this, and you want to tell the user significant information (close to the linker).

I found this: http://www.stephenchu.com/2008/05/rails-composedof-validation.html , but it doesn't seem very elegant or similar to Rails.

+3
source share
1 answer

Why can't you do this (example from one of my applications):

composed_of :cents_cost_amount,
            :class_name => "Money",
            :mapping => [%w(cents_cost_amount 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") }   

validates :cents_cost_amount,             
          :numericality => {:greater_than_or_equal_to => 0, 
                            :less_than_or_equal_to => 1000000, 
                            :message => "Only amounts in the range 0 to 10000.00 are allowed."  }    

It is difficult to give you more information about your problem without a specific example.

0
source

Source: https://habr.com/ru/post/1782640/


All Articles