<commit_before># Represents the properties of an Enterprise when viewing the details of listed shopfronts
module Api
  class EnterpriseShopfrontSerializer < ActiveModel::Serializer
    include SerializerHelper

    attributes :name, :id, :description, :latitude, :longitude, :long_description, :website,
               :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor,
               :phone, :visible, :email_address, :hash, :logo, :promo_image, :path, :category,
               :active, :producers, :orders_close_at, :hubs, :taxons, :supplied_taxons, :pickup,
               :delivery

    has_one :address, serializer: Api::AddressSerializer
    has_many :supplied_properties, serializer: Api::PropertySerializer
    has_many :distributed_properties, serializer: Api::PropertySerializer

    def orders_close_at
      OrderCycle.with_distributor(enterprise).soonest_closing.first.andand.orders_close_at
    end

    def active
      @active ||=
        enterprise.ready_for_checkout? && OrderCycle.active.with_distributor(enterprise).exists?
    end

    def pickup
      shipping_types? :pickup
    end

    def delivery
      shipping_types? :delivery
    end

    def email_address
      enterprise.email_address.to_s.reverse
    end

    def hash
      enterprise.to_param
    end

    def logo
      enterprise.logo(:medium) if enterprise.logo?
    end

    def promo_image
      enterprise.promo_image(:large) if enterprise.promo_image?
    end

    def path
      enterprise_shop_path(enterprise)
    end

    def producers
      ActiveModel::ArraySerializer.new(
        enterprise.plus_relatives_and_oc_producers(
          OrderCycle.not_closed.with_distributor(enterprise)
        ),
        each_serializer: Api::EnterpriseThinSerializer
      )
    end

    def hubs
      ActiveModel::ArraySerializer.new(
        enterprise.distributors, each_serializer: Api::EnterpriseThinSerializer
      )
    end

    def taxons
      taxons = active ? enterprise.current_distributed_taxons : enterprise.distributed_taxons

      ActiveModel::ArraySerializer.new(
        taxons, each_serializer: Api::TaxonSerializer
      )
    end

    def supplied_taxons
      ActiveModel::ArraySerializer.new(
        enterprise.supplied_taxons, each_serializer: Api::TaxonSerializer
      )
    end

    def supplied_properties
      (product_properties + producer_properties).uniq do |property_object|
        property_object.property.presentation
      end
    end

    def distributed_properties
      (distributed_product_properties + distributed_producer_properties).uniq do |property_object|
        property_object.property.presentation
      end
    end

    def distributed_product_properties
      properties = Spree::Property.joins(products: { variants: { exchanges: :order_cycle } })
        .merge(Exchange.outgoing)
        .merge(Exchange.to_enterprise(enterprise))
        .select('DISTINCT spree_properties.*')

      return properties.merge(OrderCycle.active) if active

      properties
    end

    def distributed_producer_properties
      properties = Spree::Property.joins(
        producer_properties: {
          producer: { supplied_products: { variants: { exchanges: :order_cycle } } }
        }
      )
        .merge(Exchange.outgoing).merge(Exchange.to_enterprise(enterprise))
        .select('DISTINCT spree_properties.*')

      return properties.merge(OrderCycle.active) if active

      properties
    end

    private

    def product_properties
      enterprise.supplied_products.flat_map(&:properties)
    end

    def producer_properties
      enterprise.properties
    end

    def enterprise
      object
    end

    def shipping_types?(type)
      require_shipping = type == :delivery ? 't' : 'f'
      Spree::ShippingMethod.
        joins(:distributor_shipping_methods).
        where('distributors_shipping_methods.distributor_id = ?', enterprise.id).
        where("spree_shipping_methods.require_ship_address = '#{require_shipping}'").exists?
    end
  end
end<commit_msg>Eager-load :properties on supplied products in enterprise_shopfront_serializer<commit_after># Represents the properties of an Enterprise when viewing the details of listed shopfronts
module Api
  class EnterpriseShopfrontSerializer < ActiveModel::Serializer
    include SerializerHelper

    attributes :name, :id, :description, :latitude, :longitude, :long_description, :website,
               :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor,
               :phone, :visible, :email_address, :hash, :logo, :promo_image, :path, :category,
               :active, :producers, :orders_close_at, :hubs, :taxons, :supplied_taxons, :pickup,
               :delivery

    has_one :address, serializer: Api::AddressSerializer
    has_many :supplied_properties, serializer: Api::PropertySerializer
    has_many :distributed_properties, serializer: Api::PropertySerializer

    def orders_close_at
      OrderCycle.with_distributor(enterprise).soonest_closing.first.andand.orders_close_at
    end

    def active
      @active ||=
        enterprise.ready_for_checkout? && OrderCycle.active.with_distributor(enterprise).exists?
    end

    def pickup
      shipping_types? :pickup
    end

    def delivery
      shipping_types? :delivery
    end

    def email_address
      enterprise.email_address.to_s.reverse
    end

    def hash
      enterprise.to_param
    end

    def logo
      enterprise.logo(:medium) if enterprise.logo?
    end

    def promo_image
      enterprise.promo_image(:large) if enterprise.promo_image?
    end

    def path
      enterprise_shop_path(enterprise)
    end

    def producers
      ActiveModel::ArraySerializer.new(
        enterprise.plus_relatives_and_oc_producers(
          OrderCycle.not_closed.with_distributor(enterprise)
        ),
        each_serializer: Api::EnterpriseThinSerializer
      )
    end

    def hubs
      ActiveModel::ArraySerializer.new(
        enterprise.distributors, each_serializer: Api::EnterpriseThinSerializer
      )
    end

    def taxons
      taxons = active ? enterprise.current_distributed_taxons : enterprise.distributed_taxons

      ActiveModel::ArraySerializer.new(
        taxons, each_serializer: Api::TaxonSerializer
      )
    end

    def supplied_taxons
      ActiveModel::ArraySerializer.new(
        enterprise.supplied_taxons, each_serializer: Api::TaxonSerializer
      )
    end

    def supplied_properties
      (product_properties + producer_properties).uniq do |property_object|
        property_object.property.presentation
      end
    end

    def distributed_properties
      (distributed_product_properties + distributed_producer_properties).uniq do |property_object|
        property_object.property.presentation
      end
    end

    def distributed_product_properties
      properties = Spree::Property.joins(products: { variants: { exchanges: :order_cycle } })
        .merge(Exchange.outgoing)
        .merge(Exchange.to_enterprise(enterprise))
        .select('DISTINCT spree_properties.*')

      return properties.merge(OrderCycle.active) if active

      properties
    end

    def distributed_producer_properties
      properties = Spree::Property.joins(
        producer_properties: {
          producer: { supplied_products: { variants: { exchanges: :order_cycle } } }
        }
      )
        .merge(Exchange.outgoing).merge(Exchange.to_enterprise(enterprise))
        .select('DISTINCT spree_properties.*')

      return properties.merge(OrderCycle.active) if active

      properties
    end

    private

    def product_properties
      enterprise.supplied_products.includes(:properties).flat_map(&:properties)
    end

    def producer_properties
      enterprise.properties
    end

    def enterprise
      object
    end

    def shipping_types?(type)
      require_shipping = type == :delivery ? 't' : 'f'
      Spree::ShippingMethod.
        joins(:distributor_shipping_methods).
        where('distributors_shipping_methods.distributor_id = ?', enterprise.id).
        where("spree_shipping_methods.require_ship_address = '#{require_shipping}'").exists?
    end
  end
end
{'fraction_non_alphanumeric': 0.0775155887027754, 'fraction_numerical': 0.0, 'mean_word_length': 3.154393092940579, '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': '3867275', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2282, 'n_words': 451}