Merge branch 'master' into registry-475

# Conflicts:
#	app/models/billing/price.rb
#	spec/models/billing/price_spec.rb
This commit is contained in:
Artur Beljajev 2017-06-15 09:39:14 +03:00
commit dfdce5dedc
29 changed files with 313 additions and 132 deletions

View file

@ -1,5 +1,7 @@
module Billing
class Price < ActiveRecord::Base
include Concerns::Billing::Price::Expirable
self.auto_html5_validation = false
belongs_to :zone, class_name: 'DNS::Zone', required: true
@ -9,6 +11,8 @@ 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
@ -34,6 +38,21 @@ module Billing
]
end
def self.statuses
%w[upcoming effective expired]
end
def self.upcoming
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

@ -0,0 +1,17 @@
module Concerns::Billing::Price::Expirable
extend ActiveSupport::Concern
class_methods do
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