mirror of
https://github.com/internetee/registry.git
synced 2025-08-05 09:21:43 +02:00
Add support for PG interval field
This commit is contained in:
parent
f22fc2659f
commit
838522b81a
8 changed files with 47 additions and 43 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue