Sha256: 760c682072d24ba2b224a7666195cd610360b717c76d752e16b7e02b353ce44f

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 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

ActiveSupport.on_load(:active_record) do
  ActiveRecord::Base.send(:extend, Minidusen::ActiveRecordExtensions::ClassMethods)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minidusen-0.11.2 lib/minidusen/active_record_ext.rb
minidusen-0.11.1 lib/minidusen/active_record_ext.rb