Sha256: 229b32780950db5e169a8ab9bdd461494bd9c6e1ae0e7d11cdd9caad63e3e8a9

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

class RailsTradeAdmin::ServeChargesController < RailsTradeAdmin::BaseController
  before_action :set_serve
  before_action :set_charge, only: [:edit, :update, :destroy]

  def index
    extra_params = params.fetch(:q, {}).permit(@serve.extra)
    value_params = params.fetch(:q, {}).permit(:value)
    extra_params.merge! 'min-lte': value_params[:value], 'max-gte': value_params[:value]
    @charges = @serve.charges.default_where(extra_params)
    @charges = @charges.order(min: :asc).page(params[:page]).per(20)
  end

  def new
    @charge = @serve.charges.build
  end

  def create
    @charge = @serve.charges.build(charge_params)

    respond_to do |format|
      if @charge.save
        format.html { redirect_to admin_serve_charges_url(@serve), notice: 'Charge was successfully created.' }
        format.js { redirect_to admin_serve_charges_url, notice: 'Charge was successfully created.' }
      else
        format.html { render :new }
        format.js
      end
    end
  end

  def edit

  end

  def update
    respond_to do |format|
      if @charge.update(charge_params)
        format.html { redirect_to admin_serve_charges_url(@serve), notice: 'Charge was successfully updated.' }
        format.js { redirect_to admin_serve_charges_url(@serve), notice: 'Charge was successfully created.' }
      else
        format.html { render :edit }
        format.js
      end
    end
  end

  def destroy
    if @charge.destroy
      redirect_to admin_serve_charges_url(@serve)
    end
  end

  private
  def charge_params
    attrs = [:min, :max, :price, :base_price, :type] + @serve.extra
    params.fetch(:charge, {}).permit(attrs)
  end

  def set_serve
    @serve = Serve.find params[:serve_id]
  end

  def set_charge
    @charge = ServeCharge.find params[:id]
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_trade-0.0.1 app/controllers/the_trade_admin/serve_charges_controller.rb