mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 09:57:23 +02:00
Add method to return correct price for an operation #2741
This commit is contained in:
parent
bdbf59e6d1
commit
ec5a219456
2 changed files with 103 additions and 4 deletions
|
@ -1,6 +1,8 @@
|
||||||
class Pricelist < ActiveRecord::Base
|
class Pricelist < ActiveRecord::Base
|
||||||
include Versions # version/pricelist_version.rb
|
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
|
monetize :price_cents
|
||||||
|
|
||||||
validates :price_cents, :price_currency, :price,
|
validates :price_cents, :price_currency, :price,
|
||||||
|
@ -13,10 +15,18 @@ class Pricelist < ActiveRecord::Base
|
||||||
after_initialize :init_values
|
after_initialize :init_values
|
||||||
def init_values
|
def init_values
|
||||||
return unless new_record?
|
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
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
"#{operation_category} #{category}"
|
"#{operation_category} #{category}"
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -15,8 +15,8 @@ describe Pricelist do
|
||||||
it 'should not be valid' do
|
it 'should not be valid' do
|
||||||
@pricelist.valid?
|
@pricelist.valid?
|
||||||
@pricelist.errors.full_messages.should match_array([
|
@pricelist.errors.full_messages.should match_array([
|
||||||
"Category is missing",
|
"Category is missing",
|
||||||
"Duration is missing",
|
"Duration is missing",
|
||||||
"Operation category is missing"
|
"Operation category is missing"
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
@ -36,7 +36,6 @@ describe Pricelist do
|
||||||
it 'should not have name' do
|
it 'should not have name' do
|
||||||
@pricelist.name.should == ' '
|
@pricelist.name.should == ' '
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with valid attributes' do
|
context 'with valid attributes' do
|
||||||
|
@ -69,4 +68,94 @@ describe Pricelist do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue