Class: Table::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/table/controllers/main_controller.rb

Instance Method Summary collapse

Instance Method Details

#any_match(query) ⇒ Object



72
73
74
75
76
77
78
# File 'app/table/controllers/main_controller.rb', line 72

def any_match(query)
  match = []
  search_fields.values.each do |field|
    match << {field => { '$regex' => "#{query}", '$options' => 'i' }}
  end
  match
end

#bodyObject



4
5
6
7
8
# File 'app/table/controllers/main_controller.rb', line 4

def body
  params._per_page ||= 10
  params._sort_direction ||= 1
  page._column_filt ||= []
end

#build_queryObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/table/controllers/main_controller.rb', line 35

def build_query
  if params._query
    ands = []
    # split at commas to get my array of queries (commas are AND)
    and_pieces = params._query.split(',').map(&:strip)
    and_pieces.each_with_index do |piece, i|
      if piece =~ /\|/
        or_query = []
        or_pieces = piece.split('|').map(&:strip)
        or_pieces.each_with_index do |p, i|
          if /:/.match(p)
            or_query.push(field_match(p)).flatten!
          else
            or_query.push(any_match(p)).flatten!
          end
        end
        ands << {'$or' => or_query}
      elsif /:/.match(piece) # if part of the query is a specific field search
        ands << field_match(piece)
      else
        ands << {'$or' => any_match(piece)}
      end
    end
    ands << column_filters if column_filters
    {'$and' => ands}
  end
end

#column_filtersObject



80
81
82
83
84
85
86
# File 'app/table/controllers/main_controller.rb', line 80

def column_filters
  ands = []
  page._column_filt.each do |filter|
    ands << {filter._col => {"#{filter._option}" => "#{filter._value}"}}
  end
  ands unless ands.empty?
end

#current_pageObject



106
107
108
109
110
# File 'app/table/controllers/main_controller.rb', line 106

def current_page
  per_page = params._per_page.to_i
  per_page = 10 unless per_page > 0
  ordered_data.skip(start_offset).limit(per_page)
end

#field_match(query) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/table/controllers/main_controller.rb', line 63

def field_match(query)
  clean_string = query.split(':').map(&:strip)
  if search_fields.has_key?(clean_string[0])
    {search_fields[clean_string[0]] => {"$regex"=>clean_string[1], "$options"=>"i"}}
  else
    {}
  end
end

#ordered_dataObject



98
99
100
101
102
103
104
# File 'app/table/controllers/main_controller.rb', line 98

def ordered_data
  if params._sort_direction
    searched_data.order(params._sort_field => params._sort_direction.to_i)
  else
    searched_data
  end
end

#search_fieldsObject



88
89
90
91
92
93
94
95
96
# File 'app/table/controllers/main_controller.rb', line 88

def search_fields
  fields = {}
  page._table._columns.each do |col|
    unless col._search_field == nil
      fields[col._search_field] = col._field_name
    end
  end
  fields
end

#searched_dataObject



26
27
28
29
30
31
32
33
# File 'app/table/controllers/main_controller.rb', line 26

def searched_data
  if params._query && !params._query.empty?
    query = build_query
  else
    query = column_filters
  end
  attrs.source.where(query)
end

#start_offsetObject



22
23
24
# File 'app/table/controllers/main_controller.rb', line 22

def start_offset
  (((params._page || 1).to_i - 1) * params._per_page.to_i)
end

#table_sizeObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/table/controllers/main_controller.rb', line 121

def table_size
  # TODO: volt-mongo loads the entire collection into memory for counts as of 9-7-15
  # searched_data.count
  if params._query && !params._query.empty?
    query = build_query
  else
    query = column_filters
  end
  attrs.source.path.then do |path|
    CountTask.count(path[0], query)
  end
end

#td_click(item_id, col_index) ⇒ Object



16
17
18
19
20
# File 'app/table/controllers/main_controller.rb', line 16

def td_click(item_id, col_index)
  event = page._table._columns[col_index]._click_event
  event ||= page._table._default_click_event
  trigger(event, item_id, col_index)
end

#total_sizeObject



112
113
114
115
116
117
118
119
# File 'app/table/controllers/main_controller.rb', line 112

def total_size
  # TODO: volt-mongo loads the entire collection into memory for counts as of 9-7-15
  #attrs.total_size || 500 #attrs.source.count
  query = {}
  attrs.source.path.then do |path|
    CountTask.count(path[0], query)
  end
end

#trigger_row_click(item_id) ⇒ Object



10
11
12
13
14
# File 'app/table/controllers/main_controller.rb', line 10

def trigger_row_click(item_id)
  # event = page._table._default_e_click
  # event ||=
  # trigger('row_click', item_id)
end