Consider nil in price expire time as effective

#522
This commit is contained in:
Artur Beljajev 2017-06-03 23:12:16 +03:00
parent 8916eee169
commit f76e1259fc
11 changed files with 106 additions and 65 deletions

View file

@ -12,6 +12,7 @@ module Billing
validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } }
validates :duration, inclusion: { in: Proc.new { |price| price.class.durations } }
alias_attribute :effect_time, :valid_from
alias_attribute :expire_time, :valid_to
monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_initialize :init_values
@ -22,22 +23,37 @@ module Billing
def self.durations
[
'3 mons',
'6 mons',
'9 mons',
'1 year',
'2 years',
'3 years',
'4 years',
'5 years',
'6 years',
'7 years',
'8 years',
'9 years',
'10 years',
'3 mons',
'6 mons',
'9 mons',
'1 year',
'2 years',
'3 years',
'4 years',
'5 years',
'6 years',
'7 years',
'8 years',
'9 years',
'10 years',
]
end
def self.statuses
%w[pending effective expired]
end
def self.pending
where("#{attribute_alias(:effect_time)} > ?", Time.zone.now)
end
def self.effective
condition = "#{attribute_alias(:effect_time)} <= :now " \
" AND (#{attribute_alias(:expire_time)} >= :now" \
" OR #{attribute_alias(:expire_time)} IS NULL)"
where(condition, now: Time.zone.now)
end
def self.valid
where('valid_from <= ? AND (valid_to >= ? OR valid_to IS NULL)', Time.zone.now.end_of_day,
Time.zone.now.beginning_of_day)

View file

@ -2,10 +2,6 @@ module Concerns::Billing::Price::Expirable
extend ActiveSupport::Concern
class_methods do
def unexpired
where("#{attribute_alias(:expire_time)} >= ?", Time.zone.now)
end
def expired
where("#{attribute_alias(:expire_time)} < ?", Time.zone.now)
end