Sha256: 881282074fdda1b4af440081da9fcc3b9c3fda3e90633414091fb84128cdc5f2

Contents?: true

Size: 848 Bytes

Versions: 3

Compression:

Stored size: 848 Bytes

Contents

module Dusen
  module Util
    extend self

    def like_expression(phrase)
      "%#{escape_for_like_query(phrase)}%"
    end

    def escape_for_like_query(phrase)
      phrase.gsub("%", "\\%").gsub("_", "\\_")
    end

    def qualify_column_name(model, column_name)
      column_name = column_name.to_s
      unless column_name.include?('.')
        quoted_table_name = model.connection.quote_table_name(model.table_name)
        quoted_column_name = model.connection.quote_column_name(column_name)
        column_name = "#{quoted_table_name}.#{quoted_column_name}"
      end
      column_name
    end

    def append_scope_conditions(scope, conditions)
      if scope.respond_to?(:where)
        # Rails 3
        scope.where(conditions)
      else
        # Rails 2
        scope.scoped(:conditions => conditions)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dusen-0.2.1 lib/dusen/util.rb
dusen-0.2.0 lib/dusen/util.rb
dusen-0.1.0 lib/dusen/util.rb