Sha256: 1c45d4effd78b2e0c6e610e85acfeb7c5eca2deee6ef4d9e51a2486468c6d7d4

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require_dependency 'comable/admin/application_controller'

module Comable
  module Admin
    class VariantsController < Comable::Admin::ApplicationController
      # for /admin/variants
      load_and_authorize_resource :variant, class: Comable::Variant.name, only: :index

      # for /admin/products/:product_id/variants/:id
      load_and_authorize_resource :product, class: Comable::Product.name, except: :index
      load_and_authorize_resource :variant, class: Comable::Variant.name, except: :index, through: :product

      def index
        @q = @variants.ransack(params[:q])
        @variants = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability).by_newest

        respond_to do |format|
          format.json { render json: @variants }
        end
      end

      def show
        render :edit
      end

      def new
      end

      def create
        if @variant.save
          redirect_to comable.admin_product_variant_path(@product, @variant), notice: Comable.t('successful')
        else
          flash.now[:alert] = Comable.t('failure')
          render :new
        end
      end

      def edit
      end

      def update
        if @variant.update_attributes(variant_params)
          redirect_to comable.admin_product_variant_path(@product, @variant), notice: Comable.t('successful')
        else
          flash.now[:alert] = Comable.t('failure')
          render :edit
        end
      end

      def destroy
        @variant.destroy
        redirect_to comable.admin_product_path(@product), notice: Comable.t('successful')
      end

      private

      def variant_params
        params.require(:variant).permit(
          :price,
          :sku,
          stocks_attributes: [:id, :quantity, :stock_location_id, :_destroy],
          option_values_attributes: [:id, :name]
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
comable-backend-0.7.1 app/controllers/comable/admin/variants_controller.rb
comable-backend-0.7.0 app/controllers/comable/admin/variants_controller.rb