Add price expiration

#522
This commit is contained in:
Artur Beljajev 2017-05-25 19:17:11 +03:00
parent 3741e2d2a3
commit 58ae53b1e6
17 changed files with 264 additions and 13 deletions

View file

@ -2,13 +2,25 @@ module Admin
module Billing
class PricesController < AdminController
authorize_resource(class: 'Billing::Price')
before_action :load_price, only: %i[edit update destroy]
before_action :load_price, only: %i[edit update expire]
helper_method :zones
helper_method :operation_categories
helper_method :durations
def index
@q = ::Billing::Price.search(params[:q])
@search = OpenStruct.new(search_params)
unless @search.validity
@search.validity = 'unexpired'
end
prices = ::Billing::Price.all
if @search.validity.present?
prices = ::Billing::Price.send(@search.validity)
end
@q = prices.search(params[:q])
@q.sorts = ['zone_id asc', 'duration asc', 'operation_category asc',
'valid_from desc', 'valid_to asc'] if @q.sorts.empty?
@prices = @q.result.page(params[:page])
@ -41,6 +53,13 @@ module Admin
end
end
def expire
@price.expire
@price.save!
flash[:notice] = t('.expired')
redirect_to_index
end
private
def load_price
@ -60,6 +79,13 @@ module Admin
params.require(:price).permit(*allowed_params)
end
def search_params
allowed_params = %i[
validity
]
params.fetch(:search, {}).permit(*allowed_params)
end
def redirect_to_index
redirect_to admin_prices_url
end

View file

@ -1,6 +1,7 @@
module Billing
class Price < ActiveRecord::Base
include Versions
include Concerns::Billing::Price::Expirable
has_paper_trail class_name: '::PriceVersion'
self.auto_html5_validation = false
@ -11,6 +12,7 @@ module Billing
validates :operation_category, inclusion: { in: Proc.new { |price| price.class.operation_categories } }
validates :duration, inclusion: { in: Proc.new { |price| price.class.durations } }
alias_attribute :expire_time, :valid_to
monetize :price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_initialize :init_values

View file

@ -0,0 +1,21 @@
module Concerns::Billing::Price::Expirable
extend ActiveSupport::Concern
class_methods do
def unexpired
where("#{attribute_alias(:expire_time)} >= ?", Time.zone.now)
end
def expired
where("#{attribute_alias(:expire_time)} < ?", Time.zone.now)
end
end
def expire
self[:valid_to] = Time.zone.now - 1
end
def expired?
expire_time.past?
end
end

View file

@ -1,5 +1,5 @@
<tr>
<td><%= link_to price.zone_name, edit_admin_price_path(price), id: 'admin-edit-price-btn' %></td>
<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.operation_category %></td>
<td><%= number_to_currency price.price %></td>

View file

@ -0,0 +1,20 @@
<%= form_for :search, url: admin_prices_path, method: :get, html: { class: 'form-horizontal' } do |f| %>
<div class="form-group">
<%= f.label :validity, class: 'col-sm-2 control-label' %>
<div class="col-sm-3">
<%= f.select :validity, options_for_select(%w[unexpired expired], search.validity),
{ include_blank: true },
class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<%= f.submit t('.search_btn'), class: 'btn btn-primary', name: nil %>
<%= link_to t('.reset_btn'), admin_prices_path,
class: 'btn btn-default price-search-form-search-btn' %>
</div>
</div>
<% end %>

View file

@ -3,7 +3,18 @@
</ol>
<div class="page-header">
<h1><%= t '.title' %></h1>
<div class="row">
<div class="col-sm-10">
<h1><%= t '.title' %></h1>
</div>
<div class="col-sm-2 text-right">
<%= link_to(t('.expire_btn'), expire_admin_price_path(@price),
method: :patch,
data: { confirm: t('.expire_btn_confirm') },
class: 'btn btn-danger') %>
</div>
</div>
</div>
<% if @price.persisted? && @price.errors.none? %>

View file

@ -10,6 +10,8 @@
</div>
</div>
<%= render 'search_form', search: @search %>
<% if @prices.present? %>
<table class="table table-hover table-bordered table-wrapped">
<thead>