Class: DailyReport

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SharedBehaviors
Defined in:
app/models/daily_report.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedBehaviors

included

Class Method Details

.formattersObject



24
25
26
# File 'app/models/daily_report.rb', line 24

def self.formatters
  Ruport::Controller::Table.formats.keys
end

Instance Method Details

#archiveObject



91
92
93
94
# File 'app/models/daily_report.rb', line 91

def archive
  self.archived = true
  save ? self : nil
end

#archive!Object



96
97
98
99
# File 'app/models/daily_report.rb', line 96

def archive!
  self.archived = true
  save!
end

#delete_file!Object



49
50
51
52
# File 'app/models/daily_report.rb', line 49

def delete_file!
  File.delete(localfile) if File.file?(localfile)
  true
end

#file_exists?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/daily_report.rb', line 45

def file_exists?
  File.file?(localfile)
end

#generate!Object



54
55
56
57
58
59
60
61
62
# File 'app/models/daily_report.rb', line 54

def generate!
  Dir.mkdir(localdir) unless File.directory?(localdir)
  touch(:generate_started_at)
  data = table.result
  data = apply_transform(data)
  val = save_as!(data, localfile)
  touch(:generate_ended_at)
  val
end

#localdirObject



32
33
34
# File 'app/models/daily_report.rb', line 32

def localdir
  "#{Rails.root}/#{parent}"
end

#localfileObject



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

def localfile
  "#{localdir}/#{filename}"
end

#next_jobObject



87
88
89
# File 'app/models/daily_report.rb', line 87

def next_job
  jobs.by_priority.first
end

#parentObject



28
29
30
# File 'app/models/daily_report.rb', line 28

def parent
  "files/#{table.guid}"
end

#queue_next!Object



72
73
74
75
76
77
78
# File 'app/models/daily_report.rb', line 72

def queue_next!
  job = GenerateReportJob.new(id, true)
  Delayed::Job.enqueue :payload_object => job, 
                       :priority => 10,
                       :run_at => calculate_next_gen_time,
                       :report_id => id
end

#queue_now!Object



80
81
82
83
84
85
# File 'app/models/daily_report.rb', line 80

def queue_now!
  job = GenerateReportJob.new(id, false)
  Delayed::Job.enqueue :payload_object => job, 
                       :priority => 0,
                       :report_id => id
end

#save_as!(data, path) ⇒ Object



64
65
66
67
68
69
70
# File 'app/models/daily_report.rb', line 64

def save_as!(data, path)
  tempfile = "#{Rails.root}/process/#{Time.now.to_i}_#{rand(9999999)}_#{rand(9999999)}.tmp"
  return false unless data.as(formatter.to_sym, (formatter_data || {}).merge(:file => tempfile))
  return false unless File.file?(tempfile)
  File.rename(tempfile, path)
  File.file?(path)
end

#unarchiveObject



101
102
103
104
# File 'app/models/daily_report.rb', line 101

def unarchive
  self.archived = false
  save ? self : nil
end

#url(root) ⇒ Object



40
41
42
43
# File 'app/models/daily_report.rb', line 40

def url(root)
  root += "/" unless root[-1, 1] == "/"
  "#{root}#{parent}/#{filename}"
end