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.4.27 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.4 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.26 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.3 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.25 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.2 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.24 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.1 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.23 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.22 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.0 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.21 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.20 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.19 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.18 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.17 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.16 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.15 app/queries/workarea/search/facet_sorting.rb
workarea-core-3.4.14 app/queries/workarea/search/facet_sorting.rb