<commit_before>require 'open_food_network/spree_api_key_loader'

module Admin
  class VariantOverridesController < ResourceController
    include OpenFoodNetwork::SpreeApiKeyLoader
    include EnterprisesHelper

    prepend_before_filter :load_data
    before_filter :load_collection, only: [:bulk_update]
    before_filter :load_spree_api_key, only: :index


    def index
    end

    def bulk_update
      # Ensure we're authorised to update all variant overrides
      @vo_set.collection.each { |vo| authorize! :update, vo }

      if @vo_set.save
        # Return saved VOs with IDs
        render json: @vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer
      else
        if @vo_set.errors.present?
          render json: { errors: @vo_set.errors }, status: 400
        else
          render nothing: true, status: 500
        end
      end
    end

    def bulk_reset
      # Ensure we're authorised to update all variant overrides.
      @collection.each { |vo| authorize! :bulk_reset, vo }
      @collection.each(&:reset_stock!)

      if collection_errors.present?
        render json: { errors: collection_errors }, status: 400
      else
        render json: @collection, each_serializer: Api::Admin::VariantOverrideSerializer
      end
    end


    private

    def load_data
      @hubs = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_hubs.by_name

      # Used in JS to look up the name of the producer of each product
      @producers = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_producers

      @hub_permissions = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_enterprises_per_hub

      @inventory_items = InventoryItem.where(enterprise_id: @hubs)
      @import_dates = inventory_import_dates.uniq.to_json
    end

    def inventory_import_dates
      import_dates = VariantOverride.
        distinct_import_dates.
        for_hubs(editable_enterprises.collect(&:id))

      options = [{ id: '0', name: 'All' }]
      import_dates.collect(&:import_date).map { |i| options.push(id: i.to_date, name: i.to_date.to_formatted_s(:long)) }

      options
    end

    def load_collection
      collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }]
      @vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash
    end

    def collection
      @variant_overrides = VariantOverride.for_hubs(params[:hub_id] || @hubs)
      @variant_overrides.select { |vo| vo.variant.present? }
    end

    def collection_actions
      [:index, :bulk_update, :bulk_reset]
    end

    # This has been pulled from ModelSet as it is useful for compiling a list of errors on any generic collection (not necessarily a ModelSet)
    # Could be pulled down into a lower level controller if it is useful in other high level controllers
    def collection_errors
      errors = ActiveModel::Errors.new self
      full_messages = @collection.map { |element| element.errors.full_messages }.flatten
      full_messages.each { |fm| errors.add(:base, fm) }
      errors
    end
  end
end<commit_msg>Eager load variants with variant overrides to avoid n+1 in the following line<commit_after>require 'open_food_network/spree_api_key_loader'

module Admin
  class VariantOverridesController < ResourceController
    include OpenFoodNetwork::SpreeApiKeyLoader
    include EnterprisesHelper

    prepend_before_filter :load_data
    before_filter :load_collection, only: [:bulk_update]
    before_filter :load_spree_api_key, only: :index


    def index
    end

    def bulk_update
      # Ensure we're authorised to update all variant overrides
      @vo_set.collection.each { |vo| authorize! :update, vo }

      if @vo_set.save
        # Return saved VOs with IDs
        render json: @vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer
      else
        if @vo_set.errors.present?
          render json: { errors: @vo_set.errors }, status: 400
        else
          render nothing: true, status: 500
        end
      end
    end

    def bulk_reset
      # Ensure we're authorised to update all variant overrides.
      @collection.each { |vo| authorize! :bulk_reset, vo }
      @collection.each(&:reset_stock!)

      if collection_errors.present?
        render json: { errors: collection_errors }, status: 400
      else
        render json: @collection, each_serializer: Api::Admin::VariantOverrideSerializer
      end
    end


    private

    def load_data
      @hubs = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_hubs.by_name

      # Used in JS to look up the name of the producer of each product
      @producers = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_producers

      @hub_permissions = OpenFoodNetwork::Permissions.new(spree_current_user).
        variant_override_enterprises_per_hub

      @inventory_items = InventoryItem.where(enterprise_id: @hubs)
      @import_dates = inventory_import_dates.uniq.to_json
    end

    def inventory_import_dates
      import_dates = VariantOverride.
        distinct_import_dates.
        for_hubs(editable_enterprises.collect(&:id))

      options = [{ id: '0', name: 'All' }]
      import_dates.collect(&:import_date).map { |i| options.push(id: i.to_date, name: i.to_date.to_formatted_s(:long)) }

      options
    end

    def load_collection
      collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }]
      @vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash
    end

    def collection
      @variant_overrides = VariantOverride.includes(:variant).for_hubs(params[:hub_id] || @hubs)
      @variant_overrides.select { |vo| vo.variant.present? }
    end

    def collection_actions
      [:index, :bulk_update, :bulk_reset]
    end

    # This has been pulled from ModelSet as it is useful for compiling a list of errors on any generic collection (not necessarily a ModelSet)
    # Could be pulled down into a lower level controller if it is useful in other high level controllers
    def collection_errors
      errors = ActiveModel::Errors.new self
      full_messages = @collection.map { |element| element.errors.full_messages }.flatten
      full_messages.each { |fm| errors.add(:base, fm) }
      errors
    end
  end
end
{'fraction_non_alphanumeric': 0.07897595034910783, 'fraction_numerical': 0.0032583397982932504, 'mean_word_length': 3.263227513227513, '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': '4038746', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1878, 'n_words': 493}