Sha256: 1ee2287dcfffe9e7e1b443546b9611e1288c63e31626990de311d2326483299b

Contents?: true

Size: 1.64 KB

Versions: 47

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Spree
  module Admin
    class VariantsController < ResourceController
      helper 'spree/admin/products'

      belongs_to 'spree/product', find_by: :slug
      new_action.before :new_before
      before_action :redirect_on_empty_option_values, only: [:new]
      before_action :load_data, only: [:new, :create, :edit, :update]

      private

      def new_before
        @object.attributes = @object.product.master.attributes.except('id', 'created_at', 'deleted_at',
                                                                      'sku', 'is_master')
        # Shallow Clone of the default price to populate the price field.
        @object.default_price = @object.product.master.default_price.clone
      end

      def collection
        if params[:deleted] == "on"
          base_variant_scope ||= super.with_deleted
        else
          base_variant_scope ||= super
        end

        search = Spree::Config.variant_search_class.new(params[:variant_search_term], scope: base_variant_scope)
        @collection = search.results.includes(variant_includes).page(params[:page]).per(Spree::Config[:admin_variants_per_page])
      end

      def load_data
        @tax_categories = Spree::TaxCategory.order(:name)
      end

      def variant_includes
        [{ option_values: :option_type }, :default_price]
      end

      def redirect_on_empty_option_values
        redirect_to admin_product_variants_url(params[:product_id]) if @product.empty_option_values?
      end

      def parent
        @parent ||= Spree::Product.with_deleted.find_by(slug: params[:product_id])
        @product = @parent
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
solidus_backend-2.10.5 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.10.3 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.10.2 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.6 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.8.6 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.10.1 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.5 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.10.0 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.4 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.6.6 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.7.4 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.8.5 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.3 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.2 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.10.0.beta1 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.7.3 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.6.5 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.1 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.0 app/controllers/spree/admin/variants_controller.rb
solidus_backend-2.9.0.rc.1 app/controllers/spree/admin/variants_controller.rb