Refactor prices

#475
This commit is contained in:
Artur Beljajev 2017-04-26 00:51:06 +03:00
parent 5fdc1938af
commit 5a533e09bf
44 changed files with 1027 additions and 375 deletions

View file

@ -96,7 +96,7 @@ class Ability
can :manage, DNS::Zone
can :manage, DomainVersion
can :manage, ContactVersion
can :manage, Pricelist
can :manage, Billing::Price
can :manage, User
can :manage, ApiUser
can :manage, AdminUser

5
app/models/billing.rb Normal file
View file

@ -0,0 +1,5 @@
module Billing
def self.use_relative_model_naming?
true
end
end

View file

@ -0,0 +1,74 @@
module Billing
class Price < ActiveRecord::Base
self.auto_html5_validation = false
belongs_to :zone, class_name: 'DNS::Zone', required: true
validates :price, :valid_from, :operation_category, :duration, presence: true
validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } }
validates :duration, inclusion: { in: Proc.new { |price| price.class.durations } }
monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_initialize :init_values
def self.operation_categories
%w[create renew]
end
def self.durations
[
'3 months',
'6 months',
'9 months',
'1 year',
'2 years',
'3 years',
'4 years',
'5 years',
'6 years',
'7 years',
'8 years',
'9 years',
'10 years',
]
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)
end
def self.price_for(zone, operation_category, duration)
lists = valid.where(zone: zone, operation_category: operation_category, duration: duration)
return lists.first if lists.count == 1
lists.order(valid_from: :desc).first
end
def name
"#{operation_category} #{zone_name}"
end
def years_amount
duration.to_i
end
def price_decimal
price_cents / BigDecimal.new('100')
end
def zone_name
zone.origin
end
private
def to_partial_path
'price'
end
def init_values
return unless new_record?
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
end
end
end

View file

@ -1,47 +0,0 @@
class Pricelist < ActiveRecord::Base
include Versions # version/pricelist_version.rb
scope :valid, lambda {
where(
"valid_from <= ? AND (valid_to >= ? OR valid_to IS NULL)",
Time.zone.now.end_of_day, Time.zone.now.beginning_of_day
)
}
scope :valid_at, ->(time){ where("valid_from IS NULL OR valid_from <= ?", time).where("valid_to IS NULL OR valid_to >= ?", time) }
monetize :price_cents
validates :price_cents, :price_currency, :price,
:valid_from, :category, :operation_category, :duration, presence: true
CATEGORIES = %w(ee pri.ee fie.ee med.ee com.ee)
OPERATION_CATEGORIES = %w(create renew)
DURATIONS = %w(1year 2years 3years)
after_initialize :init_values
def init_values
return unless new_record?
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
end
def name
"#{operation_category} #{category}"
end
def years_amount
duration.to_i
end
def price_decimal
price_cents / BigDecimal.new('100')
end
class << self
def pricelist_for(zone, operation, period)
lists = valid.where(category: zone, operation_category: operation, duration: period)
return lists.first if lists.count == 1
lists.order(valid_from: :desc).first
end
end
end