Refactor prices

#475
This commit is contained in:
Artur Beljajev 2017-04-26 00:51:06 +03:00
parent 5fdc1938af
commit 5a533e09bf
44 changed files with 1027 additions and 375 deletions

View file

@ -0,0 +1,86 @@
module Admin
module Billing
class PricesController < AdminController
authorize_resource(class: 'Billing::Price')
before_action :load_price, only: %i[edit update destroy]
helper_method :zones
helper_method :operation_categories
helper_method :durations
def index
@q = ::Billing::Price.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])
end
def new
@price = ::Billing::Price.new
end
def edit
end
def create
@price = ::Billing::Price.new(price_params)
if @price.save
flash[:notice] = t('.created')
redirect_to_index
else
render :new
end
end
def update
if @price.update_attributes(price_params)
flash[:notice] = t('.updated')
redirect_to_index
else
render :edit
end
end
def destroy
@price.destroy!
flash[:notice] = t('.destroyed')
redirect_to_index
end
private
def load_price
@price = ::Billing::Price.find(params[:id])
end
def price_params
allowed_params = %i[
zone_id
operation_category
duration
price
valid_from
valid_to
]
params.require(:price).permit(*allowed_params)
end
def redirect_to_index
redirect_to admin_prices_url
end
def zones
::DNS::Zone.all
end
def operation_categories
::Billing::Price::operation_categories
end
def durations
::Billing::Price::durations
end
end
end
end

View file

@ -1,54 +0,0 @@
class Admin::PricelistsController < AdminController
load_and_authorize_resource
before_action :set_pricelist, only: [:show, :edit, :update]
def index
@q = Pricelist.search(params[:q])
@q.sorts = ['category asc', 'duration asc', 'operation_category asc',
'valid_from desc', 'valid_to asc'] if @q.sorts.empty?
@pricelists = @q.result.page(params[:page])
end
def new
@pricelist = Pricelist.new
end
def edit
end
def create
comma_support_for(:pricelist, :price)
@pricelist = Pricelist.new(pricelist_params)
if @pricelist.save
redirect_to admin_pricelists_url
else
render 'new'
end
end
def update
comma_support_for(:pricelist, :price)
if @pricelist.update_attributes(pricelist_params)
redirect_to admin_pricelists_url
else
render 'edit'
end
end
def destroy
@pricelist.destroy
redirect_to admin_pricelists_url
end
private
def set_pricelist
@pricelist = Pricelist.find(params[:id])
end
def pricelist_params
params.require(:pricelist).permit(:operation_category, :category, :price_category,
:duration, :price, :valid_from, :valid_to)
end
end