From ec5a2194562e3eece215dec228f99ab8316d24c5 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Thu, 2 Jul 2015 13:06:58 +0300 Subject: [PATCH] Add method to return correct price for an operation #2741 --- app/models/pricelist.rb | 12 ++++- spec/models/pricelist_spec.rb | 95 +++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/app/models/pricelist.rb b/app/models/pricelist.rb index cfdb53776..1c47c1b83 100644 --- a/app/models/pricelist.rb +++ b/app/models/pricelist.rb @@ -1,6 +1,8 @@ class Pricelist < ActiveRecord::Base include Versions # version/pricelist_version.rb + scope :valid, -> { where("valid_from <= ? AND valid_to >= ? OR valid_to IS NULL", Time.zone.now, Time.zone.now) } + monetize :price_cents validates :price_cents, :price_currency, :price, @@ -13,10 +15,18 @@ class Pricelist < ActiveRecord::Base after_initialize :init_values def init_values return unless new_record? - self.valid_from = Time.zone.now.beginning_of_year + self.valid_from = Time.zone.now.beginning_of_year unless valid_from end def name "#{operation_category} #{category}" end + + class << self + def price_for(category, operation, duration) + lists = valid.where(category: category, operation_category: operation, duration: duration) + return lists.first.price if lists.count == 1 + lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price + end + end end diff --git a/spec/models/pricelist_spec.rb b/spec/models/pricelist_spec.rb index f52b1aeeb..9bdabf1d8 100644 --- a/spec/models/pricelist_spec.rb +++ b/spec/models/pricelist_spec.rb @@ -15,8 +15,8 @@ describe Pricelist do it 'should not be valid' do @pricelist.valid? @pricelist.errors.full_messages.should match_array([ - "Category is missing", - "Duration is missing", + "Category is missing", + "Duration is missing", "Operation category is missing" ]) end @@ -36,7 +36,6 @@ describe Pricelist do it 'should not have name' do @pricelist.name.should == ' ' end - end context 'with valid attributes' do @@ -69,4 +68,94 @@ describe Pricelist do end end end + + it 'should return correct price' do + Fabricate(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.50, + valid_from: Time.zone.parse('2198-01-01'), + valid_to: Time.zone.parse('2199-01-01') + }) + + expect { Pricelist.price_for('ee', 'create', '1year') }.to raise_error(NoMethodError) + + Fabricate(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.50, + valid_from: Time.zone.parse('2015-01-01'), + valid_to: nil + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.50 + + Fabricate(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.30, + valid_from: Time.zone.parse('2015-01-01'), + valid_to: Time.zone.parse('2999-01-01') + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.30 + + Fabricate.create(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.20, + valid_from: Time.zone.parse('2015-06-01'), + valid_to: Time.zone.parse('2999-01-01') + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.20 + + Fabricate.create(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.10, + valid_from: Time.zone.parse('2014-01-01'), + valid_to: Time.zone.parse('2999-01-01') + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.20 + + Fabricate.create(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.10, + valid_from: Time.zone.parse('2999-02-01'), + valid_to: Time.zone.parse('2999-01-01') + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.20 + + Fabricate.create(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.10, + valid_from: Time.zone.parse('2015-06-02'), + valid_to: nil + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.20 + + Fabricate.create(:pricelist, { + category: 'ee', + operation_category: 'create', + duration: '1year', + price: 1.10, + valid_from: Time.zone.parse('2015-07-01'), + valid_to: Time.zone.parse('2999-01-01') + }) + + Pricelist.price_for('ee', 'create', '1year').should == 1.10 + end end