Sha256: 16c403ecd18a97948fb3d3775633a2dc7bb02582506ba9475873684e8c0f1f1f

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Kms
  module Catalog
    class OptionTypesController < ApplicationController

      def index
        if params[:product_id]
          @product = Product.find_by_id(params[:product_id])
          @option_types = @product.option_types
        else
          @option_types = OptionType.all.uniq
        end
        render json: @option_types.to_json
      end

      def create
        @option_type = OptionType.find_or_create_by(option_types_params)
        @product = Product.find_by_id(params[:product_id])
        @product.option_types << @option_type unless @product.option_types.include?(@option_type)
        render json: @option_type
      end

      def destroy
        @product = Product.find_by_id(params[:product_id])
        @option_type = @product.option_types.find(params[:id])
        @product.option_types.delete @option_type
        @option_type.destroy if @option_type.products.empty?
        render json: @option_types
      end

      def tags
        @product = Product.find_by_id(params[:product_id])
        render json: @product.option_types.pluck(:tag).uniq.to_json
      end

      protected

      def option_types_params
        params.require(:option_type).permit!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kms_catalog-0.5.0 app/controllers/kms/catalog/option_types_controller.rb
kms_catalog-0.4.0 app/controllers/kms/catalog/option_types_controller.rb