Add support for PG interval field

This commit is contained in:
Alex Sherman 2021-05-05 15:41:30 +05:00
parent f22fc2659f
commit 838522b81a
8 changed files with 47 additions and 43 deletions

View file

@ -40,7 +40,6 @@ module Admin
def create def create
@price = ::Billing::Price.new(price_params) @price = ::Billing::Price.new(price_params)
if @price.save if @price.save
flash[:notice] = t('.created') flash[:notice] = t('.created')
redirect_to_index redirect_to_index
@ -50,7 +49,7 @@ module Admin
end end
def update def update
if @price.update_attributes(price_params) if @price.update(price_params.compact_blank)
flash[:notice] = t('.updated') flash[:notice] = t('.updated')
redirect_to_index redirect_to_index
else else
@ -81,7 +80,9 @@ module Admin
valid_to valid_to
] ]
params.require(:price).permit(*allowed_params) allowed = params.require(:price).permit(*allowed_params)
allowed[:duration] = ActiveSupport::Duration.build(allowed[:duration].to_i) if allowed[:duration]
allowed
end end
def search_params def search_params
@ -104,8 +105,7 @@ module Admin
end end
def durations def durations
durations = ::Billing::Price::durations ::Billing::Price::durations
durations.collect { |duration| [duration.sub('mon', 'month'), duration] }
end end
def statuses def statuses

View file

@ -1,5 +1,6 @@
module Billing module Billing
class Price < ApplicationRecord class Price < ApplicationRecord
attribute :duration, :interval
include Billing::Price::Expirable include Billing::Price::Expirable
include Versions include Versions
@ -8,33 +9,37 @@ module Billing
validates :price, :valid_from, :operation_category, :duration, presence: true validates :price, :valid_from, :operation_category, :duration, presence: true
validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } } validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } }
validates :duration, inclusion: { in: Proc.new { |price| price.class.durations } } validates :duration, inclusion: { in: Proc.new { |price| price.class.durations.values } }, if: :should_validate_duration?
alias_attribute :effect_time, :valid_from alias_attribute :effect_time, :valid_from
alias_attribute :expire_time, :valid_to alias_attribute :expire_time, :valid_to
monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 } monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_initialize :init_values after_initialize :init_values
def should_validate_duration?
new_record? || duration_changed?
end
def self.operation_categories def self.operation_categories
%w[create renew] %w[create renew]
end end
def self.durations def self.durations
[ {
'3 mons', '3 months' => 3.months,
'6 mons', '6 months' => 6.months,
'9 mons', '9 months' => 9.months,
'1 year', '1 year' => 1.year,
'2 years', '2 years'=> 2.years,
'3 years', '3 years'=> 3.years,
'4 years', '4 years'=> 4.years,
'5 years', '5 years'=> 5.years,
'6 years', '6 years'=> 6.years,
'7 years', '7 years'=> 7.years,
'8 years', '8 years'=> 8.years,
'9 years', '9 years'=> 9.years,
'10 years', '10 years'=> 10.years,
] }
end end
def self.statuses def self.statuses

View file

@ -1,6 +1,6 @@
<tr class="price"> <tr class="price">
<td><%= link_to price.zone_name, edit_admin_price_path(price), class: 'edit-price-btn' %></td> <td><%= link_to price.zone_name, edit_admin_price_path(price), class: 'edit-price-btn' %></td>
<td><%= price.duration.sub('mons', 'months') %></td> <td><%= price.duration %></td>
<td><%= price.operation_category %></td> <td><%= price.operation_category %></td>
<td><%= number_to_currency price.price %></td> <td><%= number_to_currency price.price %></td>
<td><%= l price.valid_from, format: :date %></td> <td><%= l price.valid_from, format: :date %></td>

View file

@ -1,5 +1,5 @@
create_one_month: create_one_month:
duration: 3 mons duration: 3 months
price_cents: 100 price_cents: 100
operation_category: create operation_category: create
valid_from: 2010-07-05 valid_from: 2010-07-05
@ -7,7 +7,7 @@ create_one_month:
zone: one zone: one
renew_one_month: renew_one_month:
duration: 1 mons duration: 1 month
price_cents: 100 price_cents: 100
operation_category: renew operation_category: renew
valid_from: 2010-07-05 valid_from: 2010-07-05

View file

@ -75,7 +75,7 @@ class APIDomainTransfersTest < ApplicationIntegrationTest
data: { success: [], data: { success: [],
failed: [{ type: "domain_transfer", failed: [{ type: "domain_transfer",
domain_name: "shop.test", domain_name: "shop.test",
errors: [{:code=>"2304", :msg=>"Object status prohibits operation"}] }], errors: {:code=>"2304", :msg=>"Object status prohibits operation"} }],
}}), }}),
JSON.parse(response.body, symbolize_names: true) JSON.parse(response.body, symbolize_names: true)
end end

View file

@ -65,7 +65,7 @@ class Billing::PriceTest < ActiveSupport::TestCase
price.duration = 'invalid' price.duration = 'invalid'
assert price.invalid? assert price.invalid?
price.duration = Billing::Price.durations.first price.duration = Billing::Price.durations.values.first
assert price.valid? assert price.valid?
end end
@ -75,21 +75,21 @@ class Billing::PriceTest < ActiveSupport::TestCase
end end
def test_returns_durations def test_returns_durations
durations = [ durations = {
'3 mons', '3 months' => 3.months,
'6 mons', '6 months' => 6.months,
'9 mons', '9 months' => 9.months,
'1 year', '1 year' => 1.year,
'2 years', '2 years'=> 2.years,
'3 years', '3 years'=> 3.years,
'4 years', '4 years'=> 4.years,
'5 years', '5 years'=> 5.years,
'6 years', '6 years'=> 6.years,
'7 years', '7 years'=> 7.years,
'8 years', '8 years'=> 8.years,
'9 years', '9 years'=> 9.years,
'10 years', '10 years'=> 10.years,
] }
assert_equal durations, Billing::Price.durations assert_equal durations, Billing::Price.durations
end end

View file

@ -16,7 +16,7 @@ class RegistrantVerificationTest < ActiveSupport::TestCase
random_action = "random#{rand(100)}" random_action = "random#{rand(100)}"
assert_difference -> { Version::RegistrantVerificationVersion.count } do assert_difference -> { Version::RegistrantVerificationVersion.count } do
registrant_verification.update_attributes!(action: random_action) registrant_verification.update!(action: random_action)
end end
end end

View file

@ -20,7 +20,6 @@ class AdminAreaPricesTest < ApplicationSystemTestCase
fill_in 'Valid from', with: effective_date fill_in 'Valid from', with: effective_date
click_on 'Create price' click_on 'Create price'
assert_text 'Price has been created' assert_text 'Price has been created'
assert_text I18n.localize(effective_date) assert_text I18n.localize(effective_date)
end end