Sha256: 72e35ce91b526d5b435de8a209733c4c88c762f6916a9bf37bbb950a69ace092

Contents?: true

Size: 991 Bytes

Versions: 1

Compression:

Stored size: 991 Bytes

Contents

# encoding: utf-8

module Dusen
  module ActiveRecord

    def search_syntax(&dsl)
      if dsl
        @search_syntax = Dusen::Description.read_syntax(&dsl)
        singleton_class.send(:define_method, :search) do |query_string|
          @search_syntax.search(self, query_string)
        end
      else
        @search_syntax
      end
    end

    def where_like(conditions)
      scope = self
      conditions.each do |field_or_fields, query|
        fields = Array(field_or_fields).collect do |field|
          Util.qualify_column_name(scope, field)
        end
        query_with_placeholders = fields.collect { |field| "#{field} LIKE ?" }.join(' OR ')
        like_expression = Dusen::Util.like_expression(query)
        bindings = [like_expression] * fields.size
        conditions = [ query_with_placeholders, *bindings ]
        scope = Util.append_scope_conditions(scope, conditions)
      end
      scope
    end

  end
end

ActiveRecord::Base.send(:extend, Dusen::ActiveRecord)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dusen-0.2.2 lib/dusen/active_record_ext.rb