<commit_before>require 'spree/core/controller_helpers/order_decorator'

class CartController < BaseController
  before_filter :check_authorization

  def populate
    order = current_order(true)

    # Without intervention, the Spree::Adjustment#update_adjustable callback is called many times
    # during cart population, for both taxation and enterprise fees. This operation triggers a
    # costly Spree::Order#update!, which only needs to be run once. We avoid this by disabling
    # callbacks on Spree::Adjustment and then manually invoke Spree::Order#update! on success.
    Spree::Adjustment.without_callbacks do
      cart_service = CartService.new(order)

      if cart_service.populate(params.slice(:products, :variants, :quantity), true)
        fire_event('spree.cart.add')
        fire_event('spree.order.contents_changed')

        order.cap_quantity_at_stock!
        order.update!

        variant_ids = variant_ids_in(cart_service.variants_h)

        render json: { error: false,
                       stock_levels: VariantsStockLevels.new.call(order, variant_ids) },
               status: :ok
      else
        render json: { error: true }, status: :precondition_failed
      end
    end<commit_msg>Replace fired events with clearer method call<commit_after>require 'spree/core/controller_helpers/order_decorator'

class CartController < BaseController
  before_filter :check_authorization

  def populate
    order = current_order(true)

    # Without intervention, the Spree::Adjustment#update_adjustable callback is called many times
    # during cart population, for both taxation and enterprise fees. This operation triggers a
    # costly Spree::Order#update!, which only needs to be run once. We avoid this by disabling
    # callbacks on Spree::Adjustment and then manually invoke Spree::Order#update! on success.
    Spree::Adjustment.without_callbacks do
      cart_service = CartService.new(order)

      if cart_service.populate(params.slice(:products, :variants, :quantity), true)
        order.update_distribution_charge!
        order.cap_quantity_at_stock!
        order.update!

        variant_ids = variant_ids_in(cart_service.variants_h)

        render json: { error: false,
                       stock_levels: VariantsStockLevels.new.call(order, variant_ids) },
               status: :ok
      else
        render json: { error: true }, status: :precondition_failed
      end
    end
{'fraction_non_alphanumeric': 0.0748663101604278, 'fraction_numerical': 0.0, 'mean_word_length': 3.437956204379562, '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': '4534120', 'n_tokens_mistral': 682, 'n_tokens_neox': 646, 'n_words': 184}