<commit_before>require 'open_food_network/enterprise_injection_data'

module InjectionHelper
  include SerializerHelper

  def inject_enterprises(enterprises = Enterprise.activated.includes(address: :state).all)
    inject_json_ams(
      'enterprises',
      enterprises,
      Api::EnterpriseSerializer,
      enterprise_injection_data
    )
  end

  def inject_groups
    select_only = required_attributes EnterpriseGroup, Api::GroupListSerializer

    inject_json_ams(
      'groups',
      EnterpriseGroup.on_front_page.by_position.select(select_only).includes(address: :state).all,
      Api::GroupListSerializer
    )
  end

  def inject_enterprise_shopfront_list
    select_only = required_attributes Enterprise, Api::EnterpriseShopfrontListSerializer

    inject_json_ams(
      'enterprises',
      Enterprise.activated.select(select_only).includes(address: :state).all,
      Api::EnterpriseShopfrontListSerializer
    )
  end

  def inject_enterprise_and_relatives
    inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data
  end

  def inject_group_enterprises
    inject_json_ams(
      "enterprises",
      @group.enterprises.activated.all,
      Api::EnterpriseSerializer,
      enterprise_injection_data
    )
  end

  def inject_current_hub
    inject_json_ams "currentHub", current_distributor, Api::EnterpriseSerializer, enterprise_injection_data
  end

  def inject_current_order
    inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer, current_distributor: current_distributor, current_order_cycle: current_order_cycle
  end

  def inject_available_shipping_methods
    inject_json_ams "shippingMethods", available_shipping_methods,
                    Api::ShippingMethodSerializer, current_order: current_order
  end

  def inject_available_payment_methods
    inject_json_ams "paymentMethods", available_payment_methods,
                    Api::PaymentMethodSerializer, current_order: current_order
  end

  def inject_taxons
    inject_json_ams "taxons", Spree::Taxon.all, Api::TaxonSerializer
  end

  def inject_properties
    inject_json_ams "properties", Spree::Property.all, Api::PropertySerializer
  end

  def inject_currency_config
    inject_json_ams "currencyConfig", {}, Api::CurrencyConfigSerializer
  end

  def inject_spree_api_key
    render partial: "json/injection_ams", locals: { name: 'spreeApiKey', json: "'#{@spree_api_key}'" }
  end

  def inject_available_countries
    inject_json_ams "availableCountries", available_countries, Api::CountrySerializer
  end

  def inject_enterprise_attributes
    render partial: "json/injection_ams", locals: { name: 'enterpriseAttributes', json: @enterprise_attributes.to_json.to_s }
  end

  def inject_orders
    inject_json_ams "orders", @orders.all, Api::OrderSerializer
  end

  def inject_shops
    customers = spree_current_user.customers
    shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq | customers.pluck(:enterprise_id))
    inject_json_ams "shops", shops.all, Api::ShopForOrdersSerializer
  end

  def inject_saved_credit_cards
    data = if spree_current_user
             spree_current_user.credit_cards.with_payment_profile.all
           else
             []
           end

    inject_json_ams "savedCreditCards", data, Api::CreditCardSerializer
  end

  def inject_json(name, partial, opts = {})
    render partial: "json/injection", locals: { name: name, partial: partial }.merge(opts)
  end

  def inject_json_ams(name, data, serializer, opts = {})
    if data.is_a?(Array)
      opts = { each_serializer: serializer }.merge(opts)
      serializer = ActiveModel::ArraySerializer
    end

    serializer_instance = serializer.new(data, opts)
    json = serializer_instance.to_json<commit_msg>Improve syntax in injection_helper<commit_after>require 'open_food_network/enterprise_injection_data'

module InjectionHelper
  include SerializerHelper

  def inject_enterprises(enterprises = Enterprise.activated.includes(address: :state).all)
    inject_json_ams(
      "enterprises",
      enterprises,
      Api::EnterpriseSerializer,
      enterprise_injection_data
    )
  end

  def inject_groups
    select_only = required_attributes EnterpriseGroup, Api::GroupListSerializer

    inject_json_ams(
      "groups",
      EnterpriseGroup.on_front_page.by_position.select(select_only).includes(address: :state).all,
      Api::GroupListSerializer
    )
  end

  def inject_enterprise_shopfront_list
    select_only = required_attributes Enterprise, Api::EnterpriseShopfrontListSerializer

    inject_json_ams(
      "enterprises",
      Enterprise.activated.select(select_only).includes(address: :state).all,
      Api::EnterpriseShopfrontListSerializer
    )
  end

  def inject_enterprise_and_relatives
    inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data
  end

  def inject_group_enterprises
    inject_json_ams(
      "enterprises",
      @group.enterprises.activated.all,
      Api::EnterpriseSerializer,
      enterprise_injection_data
    )
  end

  def inject_current_hub
    inject_json_ams "currentHub", current_distributor, Api::EnterpriseSerializer, enterprise_injection_data
  end

  def inject_current_order
    inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer, current_distributor: current_distributor, current_order_cycle: current_order_cycle
  end

  def inject_available_shipping_methods
    inject_json_ams "shippingMethods", available_shipping_methods,
                    Api::ShippingMethodSerializer, current_order: current_order
  end

  def inject_available_payment_methods
    inject_json_ams "paymentMethods", available_payment_methods,
                    Api::PaymentMethodSerializer, current_order: current_order
  end

  def inject_taxons
    inject_json_ams "taxons", Spree::Taxon.all, Api::TaxonSerializer
  end

  def inject_properties
    inject_json_ams "properties", Spree::Property.all, Api::PropertySerializer
  end

  def inject_currency_config
    inject_json_ams "currencyConfig", {}, Api::CurrencyConfigSerializer
  end

  def inject_spree_api_key
    render partial: "json/injection_ams", locals: { name: 'spreeApiKey', json: "'#{@spree_api_key}'" }
  end

  def inject_available_countries
    inject_json_ams "availableCountries", available_countries, Api::CountrySerializer
  end

  def inject_enterprise_attributes
    render partial: "json/injection_ams", locals: { name: 'enterpriseAttributes', json: @enterprise_attributes.to_json.to_s }
  end

  def inject_orders
    inject_json_ams "orders", @orders.all, Api::OrderSerializer
  end

  def inject_shops
    customers = spree_current_user.customers
    shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq | customers.pluck(:enterprise_id))
    inject_json_ams "shops", shops.all, Api::ShopForOrdersSerializer
  end

  def inject_saved_credit_cards
    data = spree_current_user ? spree_current_user.credit_cards.with_payment_profile.all : []

    inject_json_ams "savedCreditCards", data, Api::CreditCardSerializer
  end

  def inject_json(name, partial, opts = {})
    render partial: "json/injection", locals: { name: name, partial: partial }.merge(opts)
  end

  def inject_json_ams(name, data, serializer, opts = {})
    if data.is_a?(Array)
      opts = { each_serializer: serializer }.merge(opts)
      serializer = ActiveModel::ArraySerializer
    end

    serializer_instance = serializer.new(data, opts)
    json = serializer_instance.to_json
{'fraction_non_alphanumeric': 0.07717377134663017, 'fraction_numerical': 0.0, 'mean_word_length': 4.712583767684289, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4868299', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2241, 'n_words': 417}