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
@price = ::Billing::Price.new(price_params)
if @price.save
flash[:notice] = t('.created')
redirect_to_index
@ -50,7 +49,7 @@ module Admin
end
def update
if @price.update_attributes(price_params)
if @price.update(price_params.compact_blank)
flash[:notice] = t('.updated')
redirect_to_index
else
@ -81,7 +80,9 @@ module Admin
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
def search_params
@ -104,8 +105,7 @@ module Admin
end
def durations
durations = ::Billing::Price::durations
durations.collect { |duration| [duration.sub('mon', 'month'), duration] }
::Billing::Price::durations
end
def statuses

View file

@ -1,5 +1,6 @@
module Billing
class Price < ApplicationRecord
attribute :duration, :interval
include Billing::Price::Expirable
include Versions
@ -8,33 +9,37 @@ module Billing
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 } }
validates :duration, inclusion: { in: Proc.new { |price| price.class.durations.values } }, if: :should_validate_duration?
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
def should_validate_duration?
new_record? || duration_changed?
end
def self.operation_categories
%w[create renew]
end
def self.durations
[
'3 mons',
'6 mons',
'9 mons',
'1 year',
'2 years',
'3 years',
'4 years',
'5 years',
'6 years',
'7 years',
'8 years',
'9 years',
'10 years',
]
{
'3 months' => 3.months,
'6 months' => 6.months,
'9 months' => 9.months,
'1 year' => 1.year,
'2 years'=> 2.years,
'3 years'=> 3.years,
'4 years'=> 4.years,
'5 years'=> 5.years,
'6 years'=> 6.years,
'7 years'=> 7.years,
'8 years'=> 8.years,
'9 years'=> 9.years,
'10 years'=> 10.years,
}
end
def self.statuses

View file

@ -1,6 +1,6 @@
<tr class="price">
<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><%= number_to_currency price.price %></td>
<td><%= l price.valid_from, format: :date %></td>

View file

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

View file

@ -75,7 +75,7 @@ class APIDomainTransfersTest < ApplicationIntegrationTest
data: { success: [],
failed: [{ type: "domain_transfer",
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)
end

View file

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

View file

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

View file

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