Sha256: f6a6b6b793140c8917871c419ddd2daefa533c032eb30bdde49ff47eca60327d

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

module Workarea
  class ProductPrimaryNavigation
    def initialize(product, options = {})
      @product = product
      @options = options
    end

    def name
      if main_nav_taxon.present?
        main_nav_taxon.name
      elsif oldest_category.present?
        oldest_category.name
      else
        ''
      end
    end

    def main_nav_taxon
      return nil unless highest_category.try(:taxon).try(:ancestors).present?
      highest_category.taxon.ancestors_and_self.second
    end

    def highest_category
      @highest_category ||= categories
                              .select { |c| c.taxon.present? }
                              .sort { |a, b| a.taxon.depth <=> b.taxon.depth }
                              .first
    end

    def oldest_category
      categories.sort { |a, b| a.created_at <=> b.created_at }.first
    end

    private

    def categories
      @categories ||=
        (@options[:categories] || Categorization.new(@product).to_models)
          .tap(&method(:load_taxons))
    end

    def load_taxons(categories)
      taxons = Navigation::Taxon
        .where(navigable_type: Catalog::Category.name)
        .in(navigable_id: categories.map(&:id))
        .map { |taxon| [taxon.navigable_id, taxon] }
        .to_h

      categories.each { |cat| cat.set_relation(:taxon, taxons[cat.id]) }
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.26 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.25 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.23 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.22 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.21 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.20 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.19 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.18 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.17 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.16 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.15 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.14 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.13 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.12 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.11 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.10 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.9 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.8 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.5.7 app/queries/workarea/product_primary_navigation.rb