<commit_before>module Spree
  class GiftCardsController < Spree::BaseController
    helper 'spree/admin/base'

    def new
      find_gift_card_variants
      @gift_card = GiftCard.new
    end

    def create
      @gift_card = GiftCard.new(params[:gift_card])
      if @gift_card.save
        # Create line item
        line_item = LineItem.new(quantity: 1)
        line_item.gift_card = @gift_card
        line_item.variant = @gift_card.variant
        line_item.price = @gift_card.variant.price
        # Add to order
        order = current_order(true)
        order.line_items << line_item
        order.save
        # Save gift card
        @gift_card.line_item = line_item
        @gift_card.save
        redirect_to cart_path
      else
        find_gift_card_variants
        render :action => :new
      end
    end

    private

    def find_gift_card_variants
      gift_card_product_ids = Product.not_deleted.where(["is_gift_card = ?", true]).map(&:id)
      @gift_card_variants = Variant.where(["price > 0 AND product_id IN (?)", gift_card_product_ids]).order("price")
    end

  end
end<commit_msg>wrap create in a transaction<commit_after>module Spree
  class GiftCardsController < Spree::BaseController
    helper 'spree/admin/base'

    def new
      find_gift_card_variants
      @gift_card = GiftCard.new
    end

    def create
      #Wrap the transaction script in a transaction so it is an atomic operation
      Spree::GiftCard.transaction do
        @gift_card = GiftCard.new(params[:gift_card])
        if @gift_card.save
          # Create line item
          line_item = LineItem.new(quantity: 1)
          line_item.gift_card = @gift_card
          line_item.variant = @gift_card.variant
          line_item.price = @gift_card.variant.price
          Rails.logger.debug "#{line_item.errors.inspect}"
          # Add to order
          order = current_order(true)
          order.line_items << line_item
          line_item.order=order
          order.save
          # Save gift card
          @gift_card.line_item = line_item
          @gift_card.save
          redirect_to cart_path
        else
          find_gift_card_variants
          render :action => :new
        end
      end
    end

    private

    def find_gift_card_variants
      gift_card_product_ids = Product.not_deleted.where(["is_gift_card = ?", true]).map(&:id)
      @gift_card_variants = Variant.where(["price > 0 AND product_id IN (?)", gift_card_product_ids]).order("price")
    end

  end
end
{'fraction_non_alphanumeric': 0.08206565252201761, 'fraction_numerical': 0.0016012810248198558, 'mean_word_length': 2.3588709677419355, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1206922', 'n_tokens_mistral': 813, 'n_tokens_neox': 794, 'n_words': 162}