Sha256: abab0b0ac4587f55fbdd2935345403ba344ef78fef79f41f604b134eb625ef19

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

require 'search_dimensions'

module NTEE
  class HierarchicalDimension < SearchDimensions::HierarchicalDimension
    def value_class
      NTEE::HierarchicalDimensionValue
    end
  end
  
  class HierarchicalDimensionValue < SearchDimensions::HierarchicalValue
    def category
      NTEE.category(leaf_value)
    end
    
    def value=(new_value)
      category = NTEE.category(new_value)
      
      if category
        # we got an NTEE code as our value, let's convert it to a hierarchical path
        self.value = self.class.values_for_category(category).last
      else
        super
      end
    end
    
    def self.values_for_category(category)
      values_for_path((category.ancestors.reverse + [category]).map(&:code))
    end
  
    def label
      category ? category.name : super
    end
    
    def param_value
      category ? category.code : super
    end
    
    def facet_children(search)
      super.sort_by(&:label)
    end
  end
  
  class FlatDimension < SearchDimensions::Dimension
    def value_class
      NTEE::FlatDimensionValue
    end
  end
  
  class FlatDimensionValue < SearchDimensions::DimensionValue
    def category
      NTEE.category(value)
    end
    
    def label
      category ? category.name : super
    end
    
    def param_value
      category ? category.code : super
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ntee-1.0.0 lib/ntee/search_dimension.rb
ntee-0.0.4 lib/ntee/search_dimension.rb
ntee-0.0.3 lib/ntee/search_dimension.rb
ntee-0.0.2 lib/ntee/search_dimension.rb