Sha256: ef2df510d7c79df1e392d3fd8f6e7f173b3b3aec2eea5aacb7669b06f8a71eea

Contents?: true

Size: 1.48 KB

Versions: 62

Compression:

Stored size: 1.48 KB

Contents

module Workarea
  module Search
    class FacetSorting
      attr_reader :name

      def initialize(name)
        @name = name.to_sym
      end

      def to_h
        return dynamic_sorting if dynamic?
        return {} unless query_values[option].present?

        { order: query_values[option] }
      end

      def apply(results, size = full_result_size)
        return results unless dynamic?
        Hash[dynamic_option.call(name, results).first(size)]
      end

      def option
        @option ||=
          Workarea.config.search_facet_sorts[name].presence ||
          default_option
      end

      def dynamic_option
        @dynamic_option ||=
          if option.respond_to?(:call)
            option
          elsif option.try(:constantize).respond_to?(:call)
            option.constantize
          end
      rescue NameError
        @dynamic_option = nil
      end

      def dynamic?
        dynamic_option.present?
      end

      private

      def full_result_size
        Workarea.config.search_facet_dynamic_sorting_size
      end

      def dynamic_sorting
        { size: full_result_size, order: query_values[default_option] }
      end

      def default_option
        Workarea.config.search_facet_default_sort
      end

      # TODO: ES6 _term will change to _key
      def query_values
        {
          count: { '_count' => 'desc' },
          alphabetical_asc: { '_term' => 'asc' },
          alphabetical_desc: { '_term' => 'desc' }
        }
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.26 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.45 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.25 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.23 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.44 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.22 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.43 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.21 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.42 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.20 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.41 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.19 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.40 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.18 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.39 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.17 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.38 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.16 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.37 app/queries/workarea/search/facet_sorting.rb