Sha256: 0acc619af7bf0611279df592797d997640a741a0e9df027cd75cd110cb7d6daf

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module FortyFacets
  class RangeFilterDefinition < FilterDefinition
    class RangeFilter < Filter
      RANGE_REGEX = /(\d*) - (\d*)/.freeze

      def build_scope
        return Proc.new { |base| base } if empty?

        Proc.new do |base|
          scope = base.joins(definition.joins)
          scope = scope.where("#{definition.qualified_column_name} >= ?", min_value) if min_value.present?
          scope = scope.where("#{definition.qualified_column_name} <= ?", max_value) if max_value.present?
          scope
        end
      end

      def min_value
        min, _max = range_values
        min
      end

      def max_value
        _min, max = range_values
        max
      end

      def absolute_interval
        @abosultes ||= without.result.reorder('').select("min(#{definition.qualified_column_name}) AS min, max(#{definition.qualified_column_name}) as max").first
      end

      def absolute_min
        absolute_interval.min
      end

      def absolute_max
        absolute_interval.max
      end

      private

      def range_values
        value&.match(RANGE_REGEX)&.captures
      end
    end

    def build_filter(search_instance, value)
      RangeFilter.new(self, search_instance, value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
forty_facets-0.2 lib/forty_facets/filter/range_filter_definition.rb