FactoryBot.define do factory :master_price, class: "Comee::Core::MasterPrice" do purchase_price { 100.0 } selling_price { 90.0 } valid_from { Date.current.advance(months: -1) } valid_to { Date.current.advance(months: 1) } primary { false } margin { 10.0 } lead_time { nil } status { Comee::Core::MasterPrice.statuses[:draft] } product supplier unit previous_price { nil } next_price { nil } product_lookup { nil } propagated_to_client { false } trait :past do valid_from { Date.current.advance(months: -2) } valid_to { Date.current.advance(months: -1) } end trait :future do valid_from { Date.current.advance(months: 1) } valid_to { Date.current.advance(months: 2) } end trait :with_previous_price do after(:create) do |price| previous_price = build( :master_price, valid_from: price.valid_from.advance(months: -1), valid_to: price.valid_from.advance(days: -1), primary: price.primary, margin: price.margin, product: price.product, supplier: price.supplier, unit: price.unit, next_price: price ) price.previous_price = previous_price price.save! price end end trait :with_next_price do after(:create) do |price| next_price = create( :master_price, valid_from: price.valid_to.advance(days: 1), valid_to: price.valid_to.advance(months: 1), primary: price.primary, margin: price.margin, product: price.product, supplier: price.supplier, unit: price.unit, previous_price: price ) price.next_price = next_price price.save! price end end end end