Module: Dusen::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/dusen/util.rb

Instance Method Summary collapse

Instance Method Details

#append_scope_conditions(scope, conditions) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/dusen/util.rb', line 50

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

#boolean_fulltext_query(phrases) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/dusen/util.rb', line 29

def boolean_fulltext_query(phrases)
  phrases.collect do |word|
    escaped_word = Dusen::Util.escape_for_boolean_fulltext_query(word)
    if escaped_word =~ /\s/
      %{+"#{escaped_word}"} # no prefixed wildcard possible for phrases
    else
      %{+#{escaped_word}*}
    end
  end.join(' ')
end

#drop_all_tablesObject



70
71
72
73
74
75
# File 'lib/dusen/util.rb', line 70

def drop_all_tables
  connection = ::ActiveRecord::Base.connection
  connection.tables.each do |table|
    connection.drop_table table
  end
end

#escape_for_boolean_fulltext_query(phrase) ⇒ Object



25
26
27
# File 'lib/dusen/util.rb', line 25

def escape_for_boolean_fulltext_query(phrase)
  escape_with_backslash(phrase, ['+', '-', '<', '>', '(', ')', '~', '*', '"'])
end

#escape_for_like_query(phrase) ⇒ Object



20
21
22
23
# File 'lib/dusen/util.rb', line 20

def escape_for_like_query(phrase)
  # phrase.gsub("%", "\\%").gsub("_", "\\_")
  escape_with_backslash(phrase, ['%', '_'])
end

#escape_with_backslash(phrase, characters) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/dusen/util.rb', line 11

def escape_with_backslash(phrase, characters)
  characters << '\\'
  pattern = /[#{characters.collect(&Regexp.method(:quote)).join('')}]/
  # debugger
  phrase.gsub(pattern) do |match|
    "\\#{match}"
  end
end

#like_expression(phrase) ⇒ Object



7
8
9
# File 'lib/dusen/util.rb', line 7

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

#migrate_test_databaseObject



77
78
79
80
81
82
# File 'lib/dusen/util.rb', line 77

def migrate_test_database
  print "\033[30m" # dark gray text
  drop_all_tables
  ::ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
  print "\033[0m"
end

#normalize_word_boundaries(text) ⇒ Object



84
85
86
87
# File 'lib/dusen/util.rb', line 84

def normalize_word_boundaries(text)
  unwanted_mysql_boundary = /[\.;\-]/
  text.gsub(unwanted_mysql_boundary, '')
end

#qualify_column_name(model, column_name) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/dusen/util.rb', line 40

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

#select_scope_fields(scope, fields) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/dusen/util.rb', line 60

def select_scope_fields(scope, fields)
  if scope.respond_to?(:select)
    # Rails 3
    scope.select(fields)
  else
    # Rails 2
    scope.scoped(:select => fields)
  end
end