Class: MetaReports::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/meta_reports/base.rb

Direct Known Subclasses

Report

Constant Summary collapse

FORMATS =

Utility methods

%w[html pdf xlsx]
COLORS =
{
  even:                   'efefef',
  odd:                    'ffffff',
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color(klass, row = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/meta_reports/base.rb', line 17

def self.color(klass, row = 0)
  color = COLORS[klass.to_sym]
  return nil unless color
  if color.is_a? Array
    # the trailing split first is to drop any !important directive
    # color = color[row%color.length].to_s.split.first 
    # the trailing gsub is to drop any !important directive
    color = color[row%color.length].to_s.gsub(/\s.*$/,'')
  end
  if color.gsub!(/^\$/, '') # we have a variable
    color = COLORS[color.to_sym]
    if color.is_a? Array
      choice = color.gsub!(/Odd/,'') ? 1 : 0
      color = color[choice]
    end
  end
  color
end

.format_mask(format) ⇒ Object



36
37
38
# File 'app/models/meta_reports/base.rb', line 36

def self.format_mask(format)
  1 << (FORMATS.index(format.to_s) || -1)
end

.formats_mask(*formats) ⇒ Object



40
41
42
# File 'app/models/meta_reports/base.rb', line 40

def self.formats_mask(*formats)
  formats.inject(0) {|mask, format| mask | (1 << (FORMATS.index(format.to_s) || -1) )}
end

.test_report(params) ⇒ Object



7
8
9
# File 'app/models/meta_reports/base.rb', line 7

def self.test_report(params)
  {title: 'Test Report', subtitle: 'this is a test report', tables: {'Table 1' => [['One','Two','Three'],[1,2,3]]}}
end

Instance Method Details

#displays_all_formats?(*formats) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'app/models/meta_reports/base.rb', line 44

def displays_all_formats?(*formats)
  has_all = true
  ([*formats] & FORMATS).each do |format|
    i = FORMATS.index(format.to_s)
    has_any = false if formats_mask[i] != 1
  end
  has_all
end

#displays_any_format?(*formats) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'app/models/meta_reports/base.rb', line 53

def displays_any_format?(*formats)
  has_any = false
  ([*formats] & FORMATS).each do |format|
    i = FORMATS.index(format.to_s)
    has_any = true if formats_mask[i] == 1
  end
  has_any
end

#format?(format) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'app/models/meta_reports/base.rb', line 70

def format?(format)
  i = FORMATS.index(format.to_s)
  i ? (formats_mask[i] == 1) : nil
end

#formatsObject



66
67
68
# File 'app/models/meta_reports/base.rb', line 66

def formats
  FORMATS.reject { |r| (formats_mask || 0)[FORMATS.index(r)].zero? }
end

#formats=(formats) ⇒ Object



62
63
64
# File 'app/models/meta_reports/base.rb', line 62

def formats=(formats)
  self.formats_mask = ([*formats] & FORMATS).inject(0) { |sum,r| sum += 1 << FORMATS.index(r) }
end

#run(params) ⇒ Object



75
76
77
78
79
80
# File 'app/models/meta_reports/base.rb', line 75

def run(params)
  report = ::MetaReports::Report.send(name, params)
  report[:id] = "report_#{name}"
  report[:report] = self
  report
end

#viewObject



82
83
84
# File 'app/models/meta_reports/base.rb', line 82

def view
  ActiveRecord::Base.connection.execute("UPDATE meta_reports_reports SET views = views + 1 WHERE id = #{id}")
end