Sha256: 3a2ed1e2a81d50a93f5d86bd78aa70628b338217a9a4caeab8fa62d1536046c2

Contents?: true

Size: 994 Bytes

Versions: 34

Compression:

Stored size: 994 Bytes

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
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.44 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.43 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.42 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.41 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.40 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.39 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.38 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.37 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.36 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.35 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.34 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.33 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.32 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.31 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.30 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.29 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.28 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.27 app/queries/workarea/product_primary_navigation.rb
workarea-core-3.4.26 app/queries/workarea/product_primary_navigation.rb