Sha256: 02ad98cabdbe02defefd51e2e4af58c826e588ed12c9ee3d0f20dce5765bd4fc

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

module Minidusen
  module ActiveRecordExtensions
    module ClassMethods

      def where_like(conditions, options = {})
        scope = scoped

        ilike_operator = Util.ilike_operator(scope)

        if options[:negate]
          match_operator = "NOT #{ilike_operator}"
          join_operator = 'AND'
        else
          match_operator = ilike_operator
          join_operator = 'OR'
        end

        conditions.each do |field_or_fields, query|
          fields = Array(field_or_fields).collect do |field|
            Util.qualify_column_name(scope, field)
          end
          Array.wrap(query).each do |phrase|
            phrase_with_placeholders = fields.collect { |field|
              "#{field} #{match_operator} ?"
            }.join(" #{join_operator} ")
            like_expression = Minidusen::Util.like_expression(phrase)
            bindings = [like_expression] * fields.size
            conditions = [ phrase_with_placeholders, *bindings ]
            scope = scope.where(conditions)
          end
        end
        scope
      end

    end
  end
end

ActiveRecord::Base.send(:extend, Minidusen::ActiveRecordExtensions::ClassMethods)

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
minidusen-0.11.0 lib/minidusen/active_record_ext.rb
minidusen-0.10.1 lib/minidusen/active_record_ext.rb
minidusen-0.10 lib/minidusen/active_record_ext.rb
minidusen-0.9 lib/minidusen/active_record_ext.rb
minidusen-0.8.3 lib/minidusen/active_record_ext.rb
minidusen-0.8.2 lib/minidusen/active_record_ext.rb
minidusen-0.8.1 lib/minidusen/active_record_ext.rb
minidusen-0.8.0 lib/minidusen/active_record_ext.rb
minidusen-0.7.0 lib/minidusen/active_record_ext.rb