Add price expiration

#522
This commit is contained in:
Artur Beljajev 2017-05-25 19:17:11 +03:00
parent 3741e2d2a3
commit 58ae53b1e6
17 changed files with 264 additions and 13 deletions

View file

@ -1,6 +1,7 @@
module Billing
class Price < ActiveRecord::Base
include Versions
include Concerns::Billing::Price::Expirable
has_paper_trail class_name: '::PriceVersion'
self.auto_html5_validation = false
@ -11,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 :expire_time, :valid_to
monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_initialize :init_values

View file

@ -0,0 +1,21 @@
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
end
def expire
self[:valid_to] = Time.zone.now - 1
end
def expired?
expire_time.past?
end
end