<commit_before>module PlazrStore
  class Admin::VariantsController < ApplicationController
    before_filter :get_product

    def show
      @variant = Variant.find(params[:id])
    end

    def index
      @variants = @product.variants_without_master
    end

    def create
      @variant = @product.variants.build params[:variant]

      if @variant.save
        redirect_to admin_product_variant_path(@product, @variant), :notice => 'Variant was successfully created.'
      else
        @variant.get_unselected_variant_categories_and_order_by_name
        @variant.get_multimedia
        render :new
      end
    end

    def new
      @variant = Variant.new(visible: true, :product_id => @product.id)
      build_relations_for_fields_for
    end

    def edit
      @variant = Variant.find params[:id]
      build_relations_for_fields_for
      @variant.multimedia.build unless !(@variant.multimedia.count > 1)
    end

    def update
      @variant = Variant.find(params[:id])

      if @variant.update_attributes(params[:variant])
        redirect_to admin_product_variant_path(@product, @variant), :notice => 'Variant was successfully updated.'
      else
        @variant.get_unselected_variant_categories_and_order_by_name
        @variant.get_multimedia
        render :edit 
      end
    end

    def destroy
      @variant = Variant.find(params[:id])
      @variant.destroy
      redirect_to admin_product_variants_path
    end

    protected
      def get_product
        @product = Product.find(params[:product_id])
      end

      # builds certain product relations so that fields_for can render properly
      def build_relations_for_fields_for
        # builds variant_categories that are not persisted so that fields_for can render them
        @variant.get_unselected_variant_categories_and_order_by_name
        # builds variant_property_values regarding the variant_properties of the product
        @variant.get_variant_properties_from_product
        # builds multimedia for an image
        @variant.get_multimedia<commit_msg>small change to improve edit action<commit_after>module PlazrStore
  class Admin::VariantsController < ApplicationController
    before_filter :get_product

    def show
      @variant = Variant.find(params[:id])
    end

    def index
      @variants = @product.variants_without_master
    end

    def create
      @variant = @product.variants.build params[:variant]

      if @variant.save
        redirect_to admin_product_variant_path(@product, @variant), :notice => 'Variant was successfully created.'
      else
        @variant.get_unselected_variant_categories_and_order_by_name
        @variant.get_multimedia
        render :new
      end
    end

    def new
      @variant = Variant.new(visible: true, :product_id => @product.id)
      build_relations_for_fields_for
    end

    def edit
      @variant = Variant.find params[:id]
      build_relations_for_fields_for
      @variant.multimedia.build unless !(@variant.multimedia.count > 1)
    end

    def update
      @variant = Variant.find(params[:id])

      if @variant.update_attributes(params[:variant])
        redirect_to admin_product_variant_path(@product, @variant), :notice => 'Variant was successfully updated.'
      else
        @variant.get_unselected_variant_categories_and_order_by_name
        @variant.get_multimedia
        @variant.multimedia.build unless !(@variant.multimedia.count > 1)
        render :edit 
      end
    end

    def destroy
      @variant = Variant.find(params[:id])
      @variant.destroy
      redirect_to admin_product_variants_path
    end

    protected
      def get_product
        @product = Product.find(params[:product_id])
      end

      # builds certain product relations so that fields_for can render properly
      def build_relations_for_fields_for
        # builds variant_categories that are not persisted so that fields_for can render them
        @variant.get_unselected_variant_categories_and_order_by_name
        # builds variant_property_values regarding the variant_properties of the product
        @variant.get_variant_properties_from_product
        # builds multimedia for an image
        @variant.get_multimedia
{'fraction_non_alphanumeric': 0.06606463878326996, 'fraction_numerical': 0.0007129277566539924, 'mean_word_length': 3.066666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2648994', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1132, 'n_words': 276}