mirror of
https://github.com/internetee/registry.git
synced 2025-08-06 01:35:10 +02:00
Merge branch 'master' into registry-475
# Conflicts: # app/models/billing/price.rb # spec/models/billing/price_spec.rb
This commit is contained in:
commit
dfdce5dedc
29 changed files with 313 additions and 132 deletions
|
@ -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)
|
||||
|
|
17
app/models/concerns/billing/price/expirable.rb
Normal file
17
app/models/concerns/billing/price/expirable.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue