<commit_before>module Glysellin
  module CartSteps
    class ProductsController < CartController
      def create
        @line_item = current_cart.line_items.build(line_item_params)
        @variant = Glysellin::Variant.where(id: line_item_params[:variant_id]).first

        if @variant.present?
          @line_item.autofill_from(@variant)
          current_cart.line_items_added!
          render_cart_partial
        else
          render json: { error: 'choose_variant' }, status: 404
        end
      end

      def update
        current_cart.update_attributes(cart_params)

        line_item = current_cart.line_item(params[:id])

        render json: {
          quantity: line_item.quantity,
          eot_price: number_to_currency(line_item.total_eot_price),
          price: number_to_currency(line_item.total_price)
        }.merge(totals_hash)
      end

      def destroy
        current_cart.remove_line_item(params[:id])
        redirect_to cart_path
      end

      def validate
        current_cart.update_attributes(cart_params)
        current_cart.customer = current_user.customer if user_signed_in?
        current_cart.validated! if current_cart.valid?
        redirect_to cart_path
      end

      private

      def cart_params
        params.require(:cart).permit(
          :discount_code,
          line_items_attributes: [:id, :variant_id, :quantity, :_destroy]
        )
      end

      def line_item_params
        params.require(:line_item).permit(:variant_id, :quantity)
      end<commit_msg>render actions and no redirect to keep form errors<commit_after>module Glysellin
  module CartSteps
    class ProductsController < CartController
      def create
        @line_item = current_cart.line_items.build(line_item_params)
        @variant = Glysellin::Variant.where(id: line_item_params[:variant_id]).first

        if @variant.present?
          @line_item.autofill_from(@variant)
          current_cart.line_items_added!
          render_cart_partial
        else
          render json: { error: 'choose_variant' }, status: 404
        end
      end

      def update
        current_cart.update_attributes(cart_params)

        line_item = current_cart.line_item(params[:id])

        render json: {
          quantity: line_item.quantity,
          eot_price: number_to_currency(line_item.total_eot_price),
          price: number_to_currency(line_item.total_price)
        }.merge(totals_hash)
      end

      def destroy
        current_cart.remove_line_item(params[:id])
        redirect_to cart_path
      end

      def validate
        current_cart.update_attributes(cart_params)
        current_cart.customer = current_user.customer if user_signed_in?
        current_cart.validated! if current_cart.valid?
        
        render action: :show, controller: :carts
      end

      private

      def cart_params
        params.require(:cart).permit(
          :discount_code,
          line_items_attributes: [:id, :variant_id, :quantity, :_destroy]
        )
      end

      def line_item_params
        params.require(:line_item).permit(:variant_id, :quantity)
      end
{'fraction_non_alphanumeric': 0.07451231212024305, 'fraction_numerical': 0.0019187719859290054, 'mean_word_length': 2.5027995520716684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4624978', 'n_tokens_mistral': 936, 'n_tokens_neox': 893, 'n_words': 166}