Module: QueryReport::Record

Included in:
Report
Defined in:
lib/query_report/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queryObject

Returns the value of attribute query.



3
4
5
# File 'lib/query_report/record.rb', line 3

def query
  @query
end

Instance Method Details

#all_recordsObject



38
39
40
41
# File 'lib/query_report/record.rb', line 38

def all_records
  record_to_map = array_record? ? query : filtered_query
  @all_records ||= map_record(record_to_map, false)
end

#all_records_to_renderObject



61
62
63
# File 'lib/query_report/record.rb', line 61

def all_records_to_render
  @all_records_to_render ||= map_rowspan(all_records)
end

#applyObject



28
29
30
31
# File 'lib/query_report/record.rb', line 28

def apply
  @filtered_query ||= array_record? ? query : apply_filters(query.clone, @params)
  @paginated_query ||= array_record? ? query : apply_pagination(@filtered_query, @params)
end

#array_record?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/query_report/record.rb', line 9

def array_record?
  query.kind_of?(Array)
end

#content_from_element(content) ⇒ Object



102
103
104
# File 'lib/query_report/record.rb', line 102

def content_from_element(content)
  content.kind_of?(Hash) ? content[:content] : content
end

#filtered_queryObject



13
14
15
16
# File 'lib/query_report/record.rb', line 13

def filtered_query
  apply
  @filtered_query
end

#has_any_rowspan?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/query_report/record.rb', line 52

def has_any_rowspan?
  @has_any_rowspan = @columns.any?(&:rowspan?) if @has_any_rowspan.nil?
  @has_any_rowspan
end

#map_record(query, render_from_view) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/query_report/record.rb', line 43

def map_record(query, render_from_view)
  @columns = @columns.delete_if { |col| col.only_on_web? } unless render_from_view

  query.map do |record|
    array = @columns.collect { |column| [column.humanize, sanitize_value(column.value(record), render_from_view)] }
    Hash[*array.flatten]
  end
end

#map_rowspan(recs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/query_report/record.rb', line 65

def map_rowspan(recs)
  return recs unless has_any_rowspan?

  last_reset_index = @columns.select(&:rowspan?).inject({}) { |hash, column| hash[column.humanize] = 0; hash }
  rowspan_column_hash = @columns.select(&:rowspan?).inject({}) { |hash, column| hash[column.humanize] = column.rowspan_column_humanized; hash }

  prev_row = {}
  recs.each_with_index do |row, index|
    last_reset_index.each do |col, last_index|
      rowspan_col = rowspan_column_hash[col]

      rowspan_content = content_from_element(row[rowspan_col]) #picking the current content of the rowspan column
      prev_rowspan_content = content_from_element(prev_row[rowspan_col]) #picking the last rowspan content stored

      content = row[col]
      prev_content = content_from_element(prev_row[col])

      if index == 0 || rowspan_content != prev_rowspan_content || content != prev_content
        last_reset_index[col] = index
        #initialize
        row[col] = {content: content, rowspan: 1}
      elsif rowspan_content == prev_rowspan_content
        recs[last_index][col][:rowspan] += 1
      end
    end

    prev_row = row
  end

  #cleaning up the un needed row values
  recs.each do |row|
    last_reset_index.each do |col, last_index|
      row.delete col unless row[col].kind_of?(Hash)
    end
  end
end

#model_classObject



5
6
7
# File 'lib/query_report/record.rb', line 5

def model_class
  query.klass
end

#paginated_queryObject



18
19
20
21
# File 'lib/query_report/record.rb', line 18

def paginated_query
  apply
  @paginated_query
end

#recordsObject



33
34
35
36
# File 'lib/query_report/record.rb', line 33

def records
  record_to_map = array_record? ? query : paginated_query
  @records ||= map_record(record_to_map, true)
end

#records_to_renderObject



57
58
59
# File 'lib/query_report/record.rb', line 57

def records_to_render
  @records_to_render ||= map_rowspan(records)
end

#searchObject



23
24
25
26
# File 'lib/query_report/record.rb', line 23

def search
  apply
  @search
end