Class: Report

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

Constant Summary collapse

REPORT_STRUCTURE =
['stylesheets', 'javascripts', 'images', 'templates']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.iid(internal_identifier) ⇒ Object



22
23
24
# File 'app/models/report.rb', line 22

def iid(internal_identifier)
  find_by_internal_identifier(internal_identifier)
end

.import(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/report.rb', line 26

def import(file)
  ActiveRecord::Base.transaction do
    name_and_iid = file.original_filename.to_s.gsub(/(^.*(\\|\/))|(\.zip$)/, '')
    report_name = name_and_iid.split('[').first
    report_iid = name_and_iid.split('[').last.gsub(']', '')
    return false unless valid_report?(file)
    Report.skip_callback(:create, :after, :create_report_files!)
    report = Report.create(:name => report_name, :internal_identifier => report_iid)
    report.import(file)
    Report.set_callback(:create, :after, :create_report_files!)
  end
end

.make_tmp_dirObject



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

def make_tmp_dir
  Pathname.new(Rails.root.to_s + "/tmp/reports/tmp_#{Time.now.to_i.to_s}/").tap do |dir|
    FileUtils.mkdir_p(dir) unless dir.exist?
  end
end

.valid_report?(file) ⇒ Boolean



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/report.rb', line 45

def valid_report?(file)
  valid = false
  Zip::ZipFile.open(file.path) do |zip|
    zip.sort.each do |entry|
      entry.name.split('/').each do |file|
        valid = true if REPORT_STRUCTURE.include?(file)
      end
    end
  end
  valid
end

Instance Method Details

#base_dirObject



58
59
60
# File 'app/models/report.rb', line 58

def base_dir
  "#{Rails.root}/public/compass_ae_reports/#{self.internal_identifier}"
end

#create_report_files!Object



157
158
159
160
# File 'app/models/report.rb', line 157

def create_report_files!
  self.add_file(self.template, File.join(self.url, 'templates', "base.html.erb"))
  self.add_file(self.default_stylesheet,File.join(self.url, 'stylesheets', "#{self.internal_identifier}.css") )
end

#default_stylesheetObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/report.rb', line 191

def default_stylesheet
  "body{
padding: 5px; 
}

table{
width: 100%;
}

table tr{
page-break-inside: avoid !important;
}

table th{
border: 1px solid black;
color: white;
border-collapse:collapse;
font-size: 16px;
vertical-align:center;
text-align: center;
background-color: 537697;
padding-top: 2px;
padding-bottom: 2px;
padding-left: 15px;
padding-right: 15px;

}

table td{
border: 1px solid black;
border-collapse:collapse;
font-size: 14px;
vertical-align:top;
padding-top: 2px;
padding-left: 15px;
padding-right: 15px;
}

td.no-border {
  border: 0px;
  height: 20px;
}

.bold{
  font-weight: bold;
  height: 20px;
}

.left-padded{
  padding-left: 20px;
}"
end

#delete_report_files!Object



162
163
164
165
# File 'app/models/report.rb', line 162

def delete_report_files!
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  file_support.delete_file(File.join(file_support.root, self.url), :force => true)
end

#exportObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/report.rb', line 114

def export
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
  tmp_dir = Report.make_tmp_dir
  (tmp_dir + "#{name}[#{internal_identifier}].zip").tap do |file_name|
    file_name.unlink if file_name.exist?
    Zip::ZipFile.open(file_name.to_s, Zip::ZipFile::CREATE) do |zip|
      files.each do |file|
        contents = file_support.get_contents(File.join(file_support.root, file.directory, file.name))
        relative_path = file.directory.sub("#{url}", '')
        path = FileUtils.mkdir_p(File.join(tmp_dir, relative_path))
        full_path = File.join(path, file.name)
        File.open(full_path, 'wb+') { |f| f.puts(contents) }
        zip.add(File.join(relative_path[1..relative_path.length], file.name), full_path) if ::File.exists?(full_path)
      end
      ::File.open(tmp_dir + 'query.sql', 'wb+') { |f| f.puts(query) }
      zip.add('query.sql', tmp_dir + 'query.sql')
      ::File.open(tmp_dir + 'meta_data.yml', 'wb+') { |f| f.puts(.to_yaml) }
      zip.add('meta_data.yml', tmp_dir + 'meta_data.yml')

    end
  end
end

#image_path(source) ⇒ Object



142
143
144
145
# File 'app/models/report.rb', line 142

def image_path(source)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  File.join(file_support.root, self.url, 'images', source)
end

#import(file) ⇒ Object



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
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/report.rb', line 66

def import(file)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
  file = ActionController::UploadedTempfile.new("uploaded-report").tap do |f|
    f.puts file.read
    f.original_filename = file.original_filename
    f.read # no idea why we need this here, otherwise the zip can't be opened
  end unless file.path

  Zip::ZipFile.open(file.path) do |zip|
    zip.each do |entry|
      if entry.name.split('/').last == 'base.html.erb'
        name = entry.name.sub(/__MACOSX\//, '')
        data = ''
        entry.get_input_stream { |io| data = io.read }
        self.template = data
        self.save
        self.add_file(self.template, File.join(self.url, 'templates', "base.html.erb"))
      elsif entry.name.split('/').last == 'meta_data.yml'
        data = ''
        entry.get_input_stream { |io| data = io.read }
        data = StringIO.new(data) if data.present?
        self. = YAML.load(data)
        self.save
      elsif entry.name.split('/').last == 'query.sql'
        data = ''
        entry.get_input_stream { |io| data = io.read }
        self.query = data
        self.save
      else
        if entry.file?
          name = entry.name.sub(/__MACOSX\//, '')
          data = ''
          entry.get_input_stream { |io| data = io.read }
          data = StringIO.new(data) if data.present?
          report_file = self.files.where("name = ? and directory = ?", File.basename(name), File.join(self.url, File.dirname(name))).first
          unless report_file.nil?
            report_file.data = data
            report_file.save
          else
            self.add_file(data, File.join(file_support.root, self.url, name))
          end
        end
      end
    end
  end

end

#javascript_path(source) ⇒ Object



152
153
154
155
# File 'app/models/report.rb', line 152

def javascript_path(source)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  File.join(file_support.root, self.url, 'javascripts', source)
end

#set_default_templateObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/report.rb', line 167

def set_default_template
  self.template =
  "<%= bootstrap_load %>
<%= report_stylesheet_link_tag '#{self.internal_identifier}','#{self.internal_identifier}.css' %>
<h3> <%= title %> </h3>
<table>
<thead>
  <tr>
  <% columns.each do |column| %>
    <th> <%= column %> </th>
  <% end %>
  </tr>
</thead>
<% rows.each do |row| %>
  <tr>
    <% row.values.each do |value| %>
          <td> <%= value %> </td>
    <% end %>
  </tr>
<% end %>
</table>
<%= jquery_load %>"
end

#stylesheet_path(source) ⇒ Object



137
138
139
140
# File 'app/models/report.rb', line 137

def stylesheet_path(source)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  File.join(file_support.root, self.url, 'stylesheets', source)
end

#template_path(source) ⇒ Object



147
148
149
150
# File 'app/models/report.rb', line 147

def template_path(source)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  File.join(file_support.root, self.url, 'templates', source)
end

#urlObject



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

def url
  "/public/compass_ae_reports/#{self.internal_identifier}"
end