<commit_before>module Api
  class OrdersController < BaseController
    def show
      authorize! :read, order
      render json: order, serializer: Api::OrderDetailedSerializer
    end

    def index
      authorize! :admin, Spree::Order

      search_results = SearchOrders.new(params, spree_current_user)

      render json: {
        orders: serialized_orders(search_results.orders),
        pagination: search_results.pagination_data
      }
    end

    private

    def serialized_orders(orders)
      ActiveModel::ArraySerializer.new(
        orders,
        each_serializer: Api::Admin::OrderSerializer
      )
    end

    def order
      @order ||= Spree::Order.find_by_number!(params[:id])
    end
  end
end
<commit_msg>Improve preloading of order query to avoid N+1 queries<commit_after>module Api
  class OrdersController < BaseController
    def show
      authorize! :read, order
      render json: order, serializer: Api::OrderDetailedSerializer
    end

    def index
      authorize! :admin, Spree::Order

      search_results = SearchOrders.new(params, spree_current_user)

      render json: {
        orders: serialized_orders(search_results.orders),
        pagination: search_results.pagination_data
      }
    end

    private

    def serialized_orders(orders)
      ActiveModel::ArraySerializer.new(
        orders,
        each_serializer: Api::Admin::OrderSerializer
      )
    end

    def order
      @order ||= Spree::Order.
        where(number: params[:id]).
        includes(line_items: { variant: [:product, :stock_items, :default_price] }).
        first!
    end
  end
end

{'fraction_non_alphanumeric': 0.08245505269683819, 'fraction_numerical': 0.0006199628022318661, 'mean_word_length': 2.8156028368794326, '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': '1445197', 'n_tokens_mistral': 468, 'n_tokens_neox': 458, 'n_words': 108}