From 838522b81a2edf1420925906097b5a4d74349bd5 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 5 May 2021 15:41:30 +0500 Subject: [PATCH] Add support for PG interval field --- .../admin/billing/prices_controller.rb | 10 ++--- app/models/billing/price.rb | 37 +++++++++++-------- .../admin/billing/prices/_price.html.erb | 2 +- test/fixtures/billing/prices.yml | 4 +- test/integration/api/domain_transfers_test.rb | 2 +- test/models/billing/price_test.rb | 32 ++++++++-------- test/models/registrant_verification_test.rb | 2 +- test/system/admin_area/prices_test.rb | 1 - 8 files changed, 47 insertions(+), 43 deletions(-) diff --git a/app/controllers/admin/billing/prices_controller.rb b/app/controllers/admin/billing/prices_controller.rb index 609ebd21e..7bd0c3856 100644 --- a/app/controllers/admin/billing/prices_controller.rb +++ b/app/controllers/admin/billing/prices_controller.rb @@ -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 diff --git a/app/models/billing/price.rb b/app/models/billing/price.rb index 9cd32f55c..d6705e0be 100644 --- a/app/models/billing/price.rb +++ b/app/models/billing/price.rb @@ -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 diff --git a/app/views/admin/billing/prices/_price.html.erb b/app/views/admin/billing/prices/_price.html.erb index 199f53e81..b9f81f8d4 100644 --- a/app/views/admin/billing/prices/_price.html.erb +++ b/app/views/admin/billing/prices/_price.html.erb @@ -1,6 +1,6 @@ <%= link_to price.zone_name, edit_admin_price_path(price), class: 'edit-price-btn' %> - <%= price.duration.sub('mons', 'months') %> + <%= price.duration %> <%= price.operation_category %> <%= number_to_currency price.price %> <%= l price.valid_from, format: :date %> diff --git a/test/fixtures/billing/prices.yml b/test/fixtures/billing/prices.yml index 24aa9f980..4be1bc9d0 100644 --- a/test/fixtures/billing/prices.yml +++ b/test/fixtures/billing/prices.yml @@ -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 diff --git a/test/integration/api/domain_transfers_test.rb b/test/integration/api/domain_transfers_test.rb index 3ed5b0fc6..c56417f4d 100644 --- a/test/integration/api/domain_transfers_test.rb +++ b/test/integration/api/domain_transfers_test.rb @@ -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 diff --git a/test/models/billing/price_test.rb b/test/models/billing/price_test.rb index ccef56910..e87eb3765 100644 --- a/test/models/billing/price_test.rb +++ b/test/models/billing/price_test.rb @@ -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 diff --git a/test/models/registrant_verification_test.rb b/test/models/registrant_verification_test.rb index a8707fd85..f5428b2cb 100644 --- a/test/models/registrant_verification_test.rb +++ b/test/models/registrant_verification_test.rb @@ -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 diff --git a/test/system/admin_area/prices_test.rb b/test/system/admin_area/prices_test.rb index f5a299c38..dbb91966a 100644 --- a/test/system/admin_area/prices_test.rb +++ b/test/system/admin_area/prices_test.rb @@ -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