<commit_before>require 'pg_search'
require 'ransack'

module Stradivari
  module Filter
    module Model

      module ActiveRecord
        def self.included(base)
          base.module_eval do
            # Load the appropriate stradivari_all adapter
            #
            case (ver = ::ActiveRecord::VERSION::MAJOR)
            when 3 then extend Rails3
            when 4 then extend Rails4
            else
              raise Error, "Unsupported Active Record version (#{ver})"
            end

            # Add agnostic module API
            include Base
            extend ClassMethods

            # Add PG Full Text Search adapter
            include ::PgSearch
          end
        end

        module ClassMethods
          def stradivari_scope(name, options = {}, &block)
            if options[:type] == :full_text
              full_text_search name, options, &block
              stradivari_scopes.store(name.to_sym, options)
            else
              super
            end
          end

          def stradivari_type(column_name)
            columns_hash.fetch(column_name.to_s, nil).try(:type)
          end

          def stradivari_filter(stradivari_filter_options)
            params = stradivari_filter_options.deep_dup
            arel = self.stradivari_all
            sort, dir = params.values_at(:sort, :direction)

            # Process search scopes
            ransack_params = params.delete_if do |k, v|
              if v.blank?
                true # Don't bother processing blank values

              elsif (scope = stradivari_scopes.fetch(k.to_sym, nil))
                # Process it through a named scope
                value = scope.type == :boolean ? v == 'true' : v
                arel = arel.public_send(scope.name, value)

              else
                false # Bring it on
              end
            end

            # Process ransack, and remove eventual sorting added
            # by previously invoked named scopes. It is useful to
            # have sorting in some scopes, but when searching
            # through ransack we want always to have full control
            # on the order clause.
            if sort.present?
              ransack = arel.except(:order).ransack(ransack_params)

              arel_sort = ['sort_by', sort, dir].join('_')
              if arel.respond_to?(arel_sort) # Named scope
                ransack.result.public_send(arel_sort)

              else # Try ransack sort<commit_msg>Do not rely on ranskackers API anymore<commit_after>require 'pg_search'
require 'ransack'

module Stradivari
  module Filter
    module Model

      module ActiveRecord
        def self.included(base)
          base.module_eval do
            # Load the appropriate stradivari_all adapter
            #
            case (ver = ::ActiveRecord::VERSION::MAJOR)
            when 3 then extend Rails3
            when 4 then extend Rails4
            else
              raise Error, "Unsupported Active Record version (#{ver})"
            end

            # Add agnostic module API
            include Base
            extend ClassMethods

            # Add PG Full Text Search adapter
            include ::PgSearch
          end
        end

        module ClassMethods
          def stradivari_scope(name, options = {}, &block)
            if options[:type] == :full_text
              full_text_search name, options, &block
              stradivari_scopes.store(name.to_sym, options)
            else
              super
            end
          end

          def stradivari_type(column_name)
            columns_hash.fetch(column_name.to_s, nil).try(:type)
          end

          def stradivari_filter(stradivari_filter_options)
            params = stradivari_filter_options.deep_dup
            arel = self.stradivari_all
            sort, dir = params.values_at(:sort, :direction)

            # Process search scopes
            ransack_params = params.delete_if do |k, v|
              if v.blank?
                true # Don't bother processing blank values

              elsif (scope = stradivari_scopes.fetch(k.to_sym, nil))
                # Process it through a named scope
                value = scope[:type] == :boolean ? v == 'true' : v
                arel = arel.public_send(k, value)

              else
                false # Bring it on
              end
            end

            # Process ransack, and remove eventual sorting added
            # by previously invoked named scopes. It is useful to
            # have sorting in some scopes, but when searching
            # through ransack we want always to have full control
            # on the order clause.
            if sort.present?
              ransack = arel.except(:order).ransack(ransack_params)

              arel_sort = ['sort_by', sort, dir].join('_')
              if arel.respond_to?(arel_sort) # Named scope
                ransack.result.public_send(arel_sort)

              else # Try ransack sort
{'fraction_non_alphanumeric': 0.06403050983540746, 'fraction_numerical': 0.0016057808109193096, 'mean_word_length': 1.5698813821557505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1189', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1267, 'n_words': 387}