Sha256: 0d6ca437625720b23165bd705e9803ee66e8dbe7d29c82b8ca6e22da217123b8

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require "active_record"

module ActiveRecord
  module QueryMethods
    module Like
      def like(opts, *rest)
        chain_node(Arel::Nodes::Matches, opts, *rest) do |nodes|
          nodes.inject { |memo, node| Arel::Nodes::Or.new(memo, node) }
        end
      end

      def not_like(opts, *rest)
        opts = opts.reject { |_, v| v.is_a?(Array) && v.empty? }
        chain_node(Arel::Nodes::DoesNotMatch, opts, *rest) do |nodes|
          Arel::Nodes::And.new(nodes)
        end
      end

    private

      def chain_node(node_type, opts, *rest, &block)
        @scope.tap do |s|
          s.where_values += s.send(:build_where, opts, *rest).map do |r|
            if r.right.is_a?(Array)
              nodes = r.right.map { |expr| node_type.new(r.left, expr) }
              Arel::Nodes::Grouping.new block.call(nodes)
            else
              node_type.new(r.left, r.right)
            end
          end
        end
      end
    end
  end
end

ActiveRecord::QueryMethods::WhereChain.send(:include, ActiveRecord::QueryMethods::Like)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-like-0.0.6 lib/active_record/like.rb