From f4fdb840d899ac60a5365f2f507d743f82e986c2 Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 12 Apr 2017 09:05:54 +0200 Subject: [PATCH 1/5] fixes the user_name field in csv export [fixes SCI-1189] --- app/models/zip_export.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index a1acefca0..f73719edc 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -62,7 +62,8 @@ class ZipExport < ActiveRecord::Base CSV.open(file, 'wb') do |csv| csv << attributes records.find_each do |entity| - csv << entity.audit_record.values_at(*attributes.map(&:to_sym)) + csv << entity.audit_record(formated: false) + .values_at(*attributes.map(&:to_sym)) end end end From b6daf2648bd1506057d133c57ab08f17bd813a63 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 13 Apr 2017 13:01:05 +0200 Subject: [PATCH 2/5] refactor zip_export --- app/models/zip_export.rb | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index f73719edc..06855eed1 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -1,6 +1,20 @@ require 'zip' require 'fileutils' +# To use ZipExport you have to define the generate_( type )_zip method! +# Example: +# def generate_(type)_zip(tmp_dir, data, options = {}) +# attributes = options.fetch(:attributes) { :attributes_missing } +# file = FileUtils.touch("#{tmp_dir}/export.csv").first +# records = data +# CSV.open(file, 'wb') do |csv| +# csv << attributes +# records.find_each do |entity| +# csv << entity.values_at(*attributes.map(&:to_sym)) +# end +# end +# end + class ZipExport < ActiveRecord::Base belongs_to :user has_attached_file :zip_file @@ -48,24 +62,16 @@ class ZipExport < ActiveRecord::Base handle_asynchronously :generate_exportable_zip + def method_missing(m, *args, &block) + puts 'Method is missing! To use this zip_export you have to ' \ + 'define a method: genreate_( type )_zip.' + object.send(m, *args, &block) + end + private def fill_content(dir, data, type, options = {}) - generate_papertrail_csv(dir, data, options) if type == :papertrail - end - - def generate_papertrail_csv(tmp_dir, data, options = {}) - attributes = options.fetch(:attributes) { :attributes_missing } - file = FileUtils.touch("#{tmp_dir}/export.csv").first - records = PaperTrail::Version.where(data) - .order(created_at: :desc) - CSV.open(file, 'wb') do |csv| - csv << attributes - records.find_each do |entity| - csv << entity.audit_record(formated: false) - .values_at(*attributes.map(&:to_sym)) - end - end + eval("generate_#{type}_zip(dir, data, options)") end def generate_notification(user) From 241af0620acdd3e71d936a10d2b6f64f92f1089e Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 13 Apr 2017 13:27:43 +0200 Subject: [PATCH 3/5] fixes hound alerts --- app/models/zip_export.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index 06855eed1..0b3f21d6b 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -62,13 +62,17 @@ class ZipExport < ActiveRecord::Base handle_asynchronously :generate_exportable_zip + private + def method_missing(m, *args, &block) puts 'Method is missing! To use this zip_export you have to ' \ - 'define a method: genreate_( type )_zip.' - object.send(m, *args, &block) + 'define a method: generate_( type )_zip.' + object.public_send(m, *args, &block) end - private + def respond_to_missing?(method_name, include_private = false) + method_name.to_s.start_with?(' generate_') || super + end def fill_content(dir, data, type, options = {}) eval("generate_#{type}_zip(dir, data, options)") From 0534bce1ed607d6255d2c1af470ce9213aeee186 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 13 Apr 2017 14:03:56 +0200 Subject: [PATCH 4/5] adds super in method_missing --- app/models/zip_export.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index 0b3f21d6b..8fa23e634 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -67,7 +67,7 @@ class ZipExport < ActiveRecord::Base def method_missing(m, *args, &block) puts 'Method is missing! To use this zip_export you have to ' \ 'define a method: generate_( type )_zip.' - object.public_send(m, *args, &block) + super end def respond_to_missing?(method_name, include_private = false) From a6183af63d3a3b41d2c38e0052cb62f8ff766c5a Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 13 Apr 2017 14:34:58 +0200 Subject: [PATCH 5/5] fix method_missing again --- app/models/zip_export.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index 8fa23e634..0b3f21d6b 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -67,7 +67,7 @@ class ZipExport < ActiveRecord::Base def method_missing(m, *args, &block) puts 'Method is missing! To use this zip_export you have to ' \ 'define a method: generate_( type )_zip.' - super + object.public_send(m, *args, &block) end def respond_to_missing?(method_name, include_private = false)