Class: Gitter::ActiveRecordDriver

Inherits:
AbstractDriver show all
Defined in:
lib/gitter/drivers/active_record_driver.rb

Instance Attribute Summary

Attributes inherited from AbstractDriver

#scope

Instance Method Summary collapse

Methods inherited from AbstractDriver

#initialize, #new

Constructor Details

This class inherits a constructor from Gitter::AbstractDriver

Instance Method Details

#distinct_values(attr) ⇒ Object



69
70
71
72
73
# File 'lib/gitter/drivers/active_record_driver.rb', line 69

def distinct_values attr
  attribute = attr.to_s.split(/\./).last || attr
  dv = scope.reorder.select(attr).uniq.map(&:"#{attribute}").uniq
  dv
end

#each(&block) ⇒ Object



61
62
63
# File 'lib/gitter/drivers/active_record_driver.rb', line 61

def each &block
  new scope.each(&block)
end

#greater_or_equal(attr, value) ⇒ Object



53
54
55
# File 'lib/gitter/drivers/active_record_driver.rb', line 53

def greater_or_equal attr, value
  new scope.where("#{attr} >= ?", value)
end

#group(arg) ⇒ Object



22
23
24
# File 'lib/gitter/drivers/active_record_driver.rb', line 22

def group arg
  new scope.group(arg)
end

#less_or_equal(attr, value) ⇒ Object



57
58
59
# File 'lib/gitter/drivers/active_record_driver.rb', line 57

def less_or_equal attr, value
  new scope.where("#{attr} <= ?", value)
end

#named_scope(name) ⇒ Object



65
66
67
# File 'lib/gitter/drivers/active_record_driver.rb', line 65

def named_scope name
  new scope.send(name)
end

#order(attr, desc = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/gitter/drivers/active_record_driver.rb', line 8

def order attr, desc = nil
  what = case desc
    when true, 'true' then "#{attr} DESC"
    when false, 'false' then attr
    when String then desc
    else attr 
  end
  new scope.except(:order).order(what.to_s)
end

#to_sObject



75
76
77
# File 'lib/gitter/drivers/active_record_driver.rb', line 75

def to_s
  scope.to_sql
end

#unorderedObject



18
19
20
# File 'lib/gitter/drivers/active_record_driver.rb', line 18

def unordered
  new scope.except(:order)
end

#where(attr_values, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitter/drivers/active_record_driver.rb', line 26

def where attr_values, opts = {}
  exact = opts.fetch(:exact){true}
  ignore_case = opts.fetch(:ignore_case){false}
  find_format = opts.fetch(:find_format){nil}
  strip_blank = opts.fetch(:strip_blank){false}

  # has range?
  return new scope.where(attr_values) if Range === attr_values.values.first

  tokens = {}
  token_i = 0

  conditions = attr_values.map do |attr,value| 
    raise ArgumentError, "invalid range #{value} for #{attr}" if Range === value
    value = value.strip if strip_blank
    text = exact ? value : "%#{value}%"
    col, token = attr, ":q#{token_i}"
    col, token = upper(col), upper(token) if ignore_case
    col = find_format.call(col) if find_format
    tokens[:"q#{token_i}"] = text 
    token_i += 1
    "#{col} #{exact ? '=' : 'LIKE'} #{token}"
  end

  new scope.where("( #{conditions * ') OR ('} )", tokens)
end