beam 0.0.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +6 -0
  6. data/Gemfile.lock +165 -1
  7. data/README.md +34 -4
  8. data/Rakefile +35 -0
  9. data/beam.gemspec +13 -0
  10. data/lib/beam.rb +20 -0
  11. data/lib/beam/extensions/exception.rb +6 -6
  12. data/lib/beam/upload.rb +10 -15
  13. data/lib/beam/upload_controller_methods.rb +112 -0
  14. data/lib/beam/version.rb +1 -1
  15. data/lib/generators/beam/config/initializers/beam.rb +5 -0
  16. data/lib/generators/beam/install_generator.rb +1 -1
  17. data/spec/controllers/users_controller_spec.rb +134 -0
  18. data/spec/dummy/README.rdoc +28 -0
  19. data/spec/dummy/Rakefile +6 -0
  20. data/spec/dummy/app/assets/images/.keep +0 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/javascripts/users.js +2 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  25. data/spec/dummy/app/assets/stylesheets/users.css +4 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  27. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  28. data/spec/dummy/app/controllers/users_controller.rb +58 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/helpers/users_helper.rb +2 -0
  31. data/spec/dummy/app/mailers/.keep +0 -0
  32. data/spec/dummy/app/models/.keep +0 -0
  33. data/spec/dummy/app/models/concerns/.keep +0 -0
  34. data/spec/dummy/app/models/user.rb +2 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/app/views/users/_form.html.erb +25 -0
  37. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  38. data/spec/dummy/app/views/users/index.html.erb +29 -0
  39. data/spec/dummy/app/views/users/new.html.erb +5 -0
  40. data/spec/dummy/app/views/users/show.html.erb +14 -0
  41. data/spec/dummy/bin/bundle +3 -0
  42. data/spec/dummy/bin/rails +4 -0
  43. data/spec/dummy/bin/rake +4 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +28 -0
  46. data/spec/dummy/config/boot.rb +5 -0
  47. data/spec/dummy/config/database.yml +25 -0
  48. data/spec/dummy/config/environment.rb +6 -0
  49. data/spec/dummy/config/environments/development.rb +29 -0
  50. data/spec/dummy/config/environments/production.rb +80 -0
  51. data/spec/dummy/config/environments/test.rb +36 -0
  52. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  57. data/spec/dummy/config/initializers/session_store.rb +3 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +23 -0
  60. data/spec/dummy/config/routes.rb +4 -0
  61. data/spec/dummy/db/migrate/20140101111420_create_users.rb +10 -0
  62. data/spec/dummy/db/schema.rb +23 -0
  63. data/spec/dummy/lib/assets/.keep +0 -0
  64. data/spec/dummy/public/404.html +58 -0
  65. data/spec/dummy/public/422.html +58 -0
  66. data/spec/dummy/public/500.html +57 -0
  67. data/spec/dummy/public/favicon.ico +0 -0
  68. data/spec/fixturesspec/.DS_Store +0 -0
  69. data/spec/fixturesspec/users.csv +5 -0
  70. data/spec/fixturesspec/users.csv.zip +0 -0
  71. data/spec/fixturesspec/users_with_errors.csv.zip +0 -0
  72. data/spec/lib/beam/beam_config_spec.rb +15 -0
  73. data/spec/lib/beam/extensions/exception_spec.rb +15 -0
  74. data/spec/lib/beam/upload_spec.rb +67 -0
  75. data/spec/spec_helper.rb +33 -0
  76. metadata +311 -4
  77. data/config/initializers/beam.rb +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58e6c2bf43f67b5f0f1b96c2220babda0120320d
4
- data.tar.gz: 8fe1ccd8320c5b03875c9e8ea7de2f2315b9d824
3
+ metadata.gz: 5c4e5a271a4c1f2ef66f9f168b3f4345eafde0ad
4
+ data.tar.gz: 437c2586fe8b5e86d0abab316919140edf088a62
5
5
  SHA512:
6
- metadata.gz: 373dc353a9f0f00002a4c4a1df2fde2d3aba08adbc8fc78f0b178618297d9afda5ade0bc7f1800f277d8eb6acc1b919dce7290013a73ffd8b524239d5d0306bc
7
- data.tar.gz: f3efa60222fd1d9686aeb3fd0f8fe00ad06389c063faf3fc6f7791af3d02e6fe32c83f4024b8fac5464a7f8e39fe4a4f2dbe19b2caf864d596d2eab620b21594
6
+ metadata.gz: 0090185c4e8d80a8fb0423e1745e1bd71e2e924523f24462b745093730d54de3703605fe185bc14c91a191f64f01db6582882ceacf9cd1dd1727079bc8574312
7
+ data.tar.gz: 4bec5391d891d18baded2a0d6dca88f916b01481f07e8f5c8bdf7b93f3f19d9b78c673f92fdf2072075d9e0962e7c223f3456d516c2dcca2157ea383b87b158b
@@ -0,0 +1,5 @@
1
+ /coverage
2
+ /spec/dummy/log/*
3
+ /tmp/*
4
+ /spec/dummy/db/*.sqlite3
5
+ Guardfile
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ - "1.9.3"
5
+ env:
6
+ - DB=sqlite
7
+ script:
8
+ - bundle exec rake db:migrate
9
+ - bundle exec rake spec
@@ -1,3 +1,9 @@
1
+ ### 1.0.0
2
+ - Added upload_controller_methods for easy controller integration
3
+ - Refactored configuration options
4
+ - View snippet for ease in upload file
5
+ - Complete test suite
6
+
1
7
  ### 0.0.3
2
8
  - Zipped CSV files are optional now
3
9
  - Various configuration options moved to initializers
@@ -1,17 +1,181 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- beam (0.0.3)
4
+ beam (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ actionmailer (4.0.0)
10
+ actionpack (= 4.0.0)
11
+ mail (~> 2.5.3)
12
+ actionpack (4.0.0)
13
+ activesupport (= 4.0.0)
14
+ builder (~> 3.1.0)
15
+ erubis (~> 2.7.0)
16
+ rack (~> 1.5.2)
17
+ rack-test (~> 0.6.2)
18
+ activemodel (4.0.0)
19
+ activesupport (= 4.0.0)
20
+ builder (~> 3.1.0)
21
+ activerecord (4.0.0)
22
+ activemodel (= 4.0.0)
23
+ activerecord-deprecated_finders (~> 1.0.2)
24
+ activesupport (= 4.0.0)
25
+ arel (~> 4.0.0)
26
+ activerecord-deprecated_finders (1.0.3)
27
+ activerecord-import (0.4.0)
28
+ activerecord (>= 3.0)
29
+ activesupport (4.0.0)
30
+ i18n (~> 0.6, >= 0.6.4)
31
+ minitest (~> 4.2)
32
+ multi_json (~> 1.3)
33
+ thread_safe (~> 0.1)
34
+ tzinfo (~> 0.3.37)
35
+ arel (4.0.1)
36
+ atomic (1.1.14)
37
+ builder (3.1.4)
38
+ childprocess (0.3.9)
39
+ ffi (~> 1.0, >= 1.0.11)
40
+ coderay (1.0.9)
41
+ columnize (0.3.6)
42
+ coveralls (0.7.0)
43
+ multi_json (~> 1.3)
44
+ rest-client
45
+ simplecov (>= 0.7)
46
+ term-ansicolor
47
+ thor
48
+ debugger (1.6.5)
49
+ columnize (>= 0.3.1)
50
+ debugger-linecache (~> 1.2.0)
51
+ debugger-ruby_core_source (~> 1.3.1)
52
+ debugger-linecache (1.2.0)
53
+ debugger-ruby_core_source (1.3.1)
54
+ diff-lcs (1.2.4)
55
+ erubis (2.7.0)
56
+ ffi (1.9.3)
57
+ formatador (0.2.4)
58
+ guard (1.8.3)
59
+ formatador (>= 0.2.4)
60
+ listen (~> 1.3)
61
+ lumberjack (>= 1.0.2)
62
+ pry (>= 0.9.10)
63
+ thor (>= 0.14.6)
64
+ guard-bundler (1.0.0)
65
+ bundler (~> 1.0)
66
+ guard (~> 1.1)
67
+ guard-rspec (3.0.3)
68
+ guard (>= 1.8)
69
+ rspec (~> 2.13)
70
+ guard-spork (1.5.1)
71
+ childprocess (>= 0.2.3)
72
+ guard (>= 1.1)
73
+ spork (>= 0.8.4)
74
+ hike (1.2.3)
75
+ i18n (0.6.9)
76
+ listen (1.3.1)
77
+ rb-fsevent (>= 0.9.3)
78
+ rb-inotify (>= 0.9)
79
+ rb-kqueue (>= 0.2)
80
+ lumberjack (1.0.4)
81
+ mail (2.5.4)
82
+ mime-types (~> 1.16)
83
+ treetop (~> 1.4.8)
84
+ method_source (0.8.2)
85
+ mime-types (1.25.1)
86
+ minitest (4.7.5)
87
+ multi_json (1.8.2)
88
+ polyglot (0.3.3)
89
+ pry (0.9.12.2)
90
+ coderay (~> 1.0.5)
91
+ method_source (~> 0.8)
92
+ slop (~> 3.4)
93
+ rack (1.5.2)
94
+ rack-test (0.6.2)
95
+ rack (>= 1.0)
96
+ rails (4.0.0)
97
+ actionmailer (= 4.0.0)
98
+ actionpack (= 4.0.0)
99
+ activerecord (= 4.0.0)
100
+ activesupport (= 4.0.0)
101
+ bundler (>= 1.3.0, < 2.0)
102
+ railties (= 4.0.0)
103
+ sprockets-rails (~> 2.0.0)
104
+ railties (4.0.0)
105
+ actionpack (= 4.0.0)
106
+ activesupport (= 4.0.0)
107
+ rake (>= 0.8.7)
108
+ thor (>= 0.18.1, < 2.0)
9
109
  rake (10.1.0)
110
+ rb-fsevent (0.9.3)
111
+ rb-inotify (0.9.2)
112
+ ffi (>= 0.5.0)
113
+ rb-kqueue (0.2.0)
114
+ ffi (>= 0.5.0)
115
+ rest-client (1.6.7)
116
+ mime-types (>= 1.16)
117
+ rspec (2.14.1)
118
+ rspec-core (~> 2.14.0)
119
+ rspec-expectations (~> 2.14.0)
120
+ rspec-mocks (~> 2.14.0)
121
+ rspec-core (2.14.7)
122
+ rspec-expectations (2.14.4)
123
+ diff-lcs (>= 1.1.3, < 2.0)
124
+ rspec-mocks (2.14.4)
125
+ rspec-rails (2.14.0)
126
+ actionpack (>= 3.0)
127
+ activesupport (>= 3.0)
128
+ railties (>= 3.0)
129
+ rspec-core (~> 2.14.0)
130
+ rspec-expectations (~> 2.14.0)
131
+ rspec-mocks (~> 2.14.0)
132
+ shoulda-matchers (2.4.0)
133
+ activesupport (>= 3.0.0)
134
+ simplecov (0.7.1)
135
+ multi_json (~> 1.0)
136
+ simplecov-html (~> 0.7.1)
137
+ simplecov-html (0.7.1)
138
+ slop (3.4.6)
139
+ spork (1.0.0rc4)
140
+ sprockets (2.10.1)
141
+ hike (~> 1.2)
142
+ multi_json (~> 1.0)
143
+ rack (~> 1.0)
144
+ tilt (~> 1.1, != 1.3.0)
145
+ sprockets-rails (2.0.1)
146
+ actionpack (>= 3.0)
147
+ activesupport (>= 3.0)
148
+ sprockets (~> 2.8)
149
+ sqlite3 (1.3.8)
150
+ term-ansicolor (1.2.2)
151
+ tins (~> 0.8)
152
+ thor (0.18.1)
153
+ thread_safe (0.1.3)
154
+ atomic
155
+ tilt (1.4.1)
156
+ tins (0.12.0)
157
+ treetop (1.4.15)
158
+ polyglot
159
+ polyglot (>= 0.3.1)
160
+ tzinfo (0.3.38)
10
161
 
11
162
  PLATFORMS
12
163
  ruby
13
164
 
14
165
  DEPENDENCIES
166
+ activerecord-import (= 0.4.0)
15
167
  beam!
16
168
  bundler (~> 1.3)
169
+ coveralls
170
+ debugger (~> 1.6.5)
171
+ guard-bundler
172
+ guard-rspec
173
+ guard-spork
174
+ rails
17
175
  rake
176
+ rb-fsevent
177
+ rspec
178
+ rspec-rails
179
+ shoulda-matchers
180
+ spork (~> 1.0rc)
181
+ sqlite3
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Beam
2
+ [![Gem Version](https://badge.fury.io/rb/beam.png)](http://badge.fury.io/rb/beam) [![Build Status](https://travis-ci.org/gouravtiwari/beam.png?branch=master)](https://travis-ci.org/gouravtiwari/beam) [![Coverage Status](https://coveralls.io/repos/gouravtiwari/beam/badge.png?branch=master)](https://coveralls.io/r/gouravtiwari/beam?branch=master)
2
3
 
3
4
  A rubygem to simplifiy repetitive csv upload process for ActiveRecord models in rails applications.
4
5
  Supports bulk upload with [activerecord-import](http://rubygems.org/gems/activerecord-import)
@@ -49,21 +50,50 @@ Supports bulk upload with [activerecord-import](http://rubygems.org/gems/activer
49
50
  # Test4,test4@test.com
50
51
  ```
51
52
 
52
- 4. Get the output as:
53
+ 4. Only if you would like to use upload_controller_methods (to help you upload files zipped-csv files, as fake.csv.zip) include below routes in config/routes.rb (for fake_controller):
54
+ ```ruby
55
+ post "fake/upload", to: "fake#upload"
56
+ get "fake/error_file", to: "fake#error_file"
57
+ ```
58
+ include these methods in the controller:
59
+ ```ruby
60
+ class FakeController < ApplicationController
61
+ include Beam::UploadControllerMethods
62
+ end
63
+ ...
64
+ ...
65
+ ```
66
+ and add view snippet to app/views/fake/upload_form.html.erb
67
+ ```ruby
68
+ <%= form_tag users_upload_path, :multipart => true do %>
69
+ <%= file_field_tag 'upload[upload_file]'%>
70
+ <%= submit_tag "Upload" %>
71
+ <% end %>
72
+ ```
73
+
74
+ 5. Get the output as:
53
75
 
54
76
  ```ruby
55
77
  # response hash, e.g.
56
78
  {:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]}
57
79
  # error file, e.g.
58
- for users.csv file, it creates errors_users.csv at the same path
80
+ # for users.csv file, it creates errors_users.csv at the same path specified in Beam.config (Rails.root+'/tmp')
59
81
  # see records being saved in batch(by default) of 1_000 with activerecord-import gem
60
82
  ```
61
83
 
62
- See beam/upload.rb for more details
84
+ ## Configuration options:
85
+
86
+ Default configurations, to change these, update config/initializers/beam.rb:
87
+ Beam.config[:error_file_needed] = true
88
+ Beam.config[:batch_process] = true
89
+ Beam.config[:batch_size] = 1_000
90
+ Beam.config[:zipped] = true
91
+ Beam.config[:data_upload_path] = "#{Rails.root}/tmp"
92
+
63
93
 
64
94
  ## TO DO
65
95
 
66
- SPECS!
96
+ SideKiq & DelayedJob options
67
97
 
68
98
  ## Contributing
69
99
 
data/Rakefile CHANGED
@@ -1 +1,36 @@
1
1
  require "bundler/gem_tasks"
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'AuditRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+
33
+ desc "Run all specs in spec directory (excluding plugin specs)"
34
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
35
+
36
+ task :default => :spec
@@ -20,5 +20,18 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rails"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "activerecord-import", '0.4.0'
23
26
 
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "rspec-rails"
29
+ spec.add_development_dependency "spork", '~>1.0rc'
30
+ spec.add_development_dependency "guard-spork"
31
+ spec.add_development_dependency "guard-rspec"
32
+ spec.add_development_dependency "guard-bundler"
33
+ spec.add_development_dependency "rb-fsevent"
34
+ spec.add_development_dependency "shoulda-matchers"
35
+ spec.add_development_dependency "debugger", "~> 1.6.5"
36
+ spec.add_development_dependency "coveralls"
24
37
  end
@@ -1,4 +1,24 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  require "beam/version"
3
3
  require 'beam/extensions/exception'
4
+
5
+ module Beam
6
+ def self.root
7
+ File.expand_path '../..', __FILE__
8
+ end
9
+
10
+ def self.lib
11
+ File.join root, 'lib'
12
+ end
13
+
14
+ def self.tmp
15
+ File.join root, 'tmp'
16
+ end
17
+
18
+ def self.spec
19
+ File.join root, 'spec'
20
+ end
21
+ end
22
+
4
23
  require 'beam/upload'
24
+ require 'beam/upload_controller_methods'
@@ -1,10 +1,10 @@
1
1
  class Exception
2
2
  def formatted_exception(error_message)
3
- <<-ERROR
4
- An error occurred while #{error_message}:
5
- Type: #{self.class.name}
6
- Message: #{self.message}
7
- Backtrace:\n#{self.backtrace.join("\n")}
8
- ERROR
3
+ <<-ERROR
4
+ An error occurred while #{error_message}:
5
+ Type: #{self.class.name}
6
+ Message: #{self.message}
7
+ Backtrace:\n#{self.backtrace.join("\n")}
8
+ ERROR
9
9
  end
10
10
  end
@@ -6,24 +6,16 @@ module Beam
6
6
  # for batch processing
7
7
  # batch_size: default is 1_000, change it to batch of records you want to upload
8
8
  # zipped: set it to true if uploaded file is zipped
9
- @config = {
9
+ # data_upload_path: should be set to Rails.root + '/tmp'
10
+ mattr_accessor :config
11
+ @@config = {
10
12
  error_file_needed: true,
11
13
  batch_process: true,
12
14
  batch_size: 1_000,
13
- zipped: true
15
+ zipped: true,
16
+ data_upload_path: Rails.root ? Rails.root + '/tmp' : Beam.tmp
14
17
  }
15
18
 
16
- @valid_config_keys = @config.keys
17
-
18
- # Configure through hash
19
- def self.configure(opts = {})
20
- opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
21
- end
22
-
23
- def self.config
24
- configure(@config)
25
- end
26
-
27
19
  module Upload
28
20
  def upload_config
29
21
  @error_file_needed = Beam.config[:error_file_needed]
@@ -35,7 +27,7 @@ module Beam
35
27
  # params:
36
28
  # file_name: zip file name, e.g. users.csv.zip
37
29
  # file_path: path/to/zip-file, e.g. Rails.root/tmp
38
- # callback_method: method which does the job to load records, default is parse method
30
+ # callback_method: method which does the job to parse CSV and load records, default is parse method
39
31
  def upload_file(file_name, file_path, callback_method='parse')
40
32
  status = {}
41
33
  upload_config
@@ -55,6 +47,8 @@ module Beam
55
47
  error_in_upload([[invalid_file_message]]) if @error_file_needed
56
48
  Rails.logger.error e.formatted_exception("Uploading file #{@file_name} failed!")
57
49
  status = { errors: 1, status: 500}
50
+ ensure
51
+ File.delete @original_zip_file if File.exists?(@original_zip_file)
58
52
  end
59
53
  status
60
54
  end
@@ -125,7 +119,8 @@ module Beam
125
119
  import_from_batch(batch)
126
120
  rescue Exception => e
127
121
  response[:errors] = 1
128
- log_and_return_error({}, e, '')
122
+ response[:error_rows] = log_and_return_error({}, e, '')
123
+ response[:status] = 500
129
124
  end
130
125
 
131
126
  if response[:error_rows].blank?
@@ -0,0 +1,112 @@
1
+ module Beam
2
+ module UploadControllerMethods
3
+ attr_accessor :model, :upload_method
4
+
5
+ def upload
6
+ @upload_status = file_upload_status(params)
7
+ @upload_status.merge!(additional_params_on_success) if @upload_status[:success_msg]
8
+
9
+ respond_to do |format|
10
+ format.json { render json: @upload_status }
11
+ format.html { redirect_to :back }
12
+ end
13
+ end
14
+
15
+ def error_file
16
+ send_file "#{Beam.config[:data_upload_path]}/errors_#{self.controller_name.downcase}.csv"
17
+ end
18
+
19
+ def self.included(base)
20
+ private
21
+
22
+ # hook for post successful upload, in case additional parameters need to be send
23
+ def additional_params_on_success
24
+ {}
25
+ end
26
+
27
+ def zip_file?(file)
28
+ ["application/zip", "application/x-zip-compressed", "application/octet-stream"].include?(file.content_type) && file.original_filename.include?(".zip")
29
+ end
30
+
31
+ def file_upload_status(params)
32
+ file_obj = params[:upload] ? params[:upload][:upload_file] : params[:files][0]
33
+
34
+ status = file_obj ? file_status(file_obj) : not_file_status
35
+ status[:message] = status[:num_errors] && status[:num_errors].zero? ? status[:success_msg] : status[:error_msg]
36
+ status[:status] = status[:message] == status[:success_msg] ? "success" : "failure"
37
+ status[:status_code] = status[:message] == status[:success_msg] ? 200 : 500
38
+ status
39
+ end
40
+
41
+ def file_status(file)
42
+ zip_file?(file) ? csv_file_status(file) : not_csv_file_status
43
+ end
44
+
45
+ def not_file_status
46
+ {error_msg: "No file detected, please upload a file"}
47
+ end
48
+
49
+ def csv_file_status(file)
50
+ response = upload_file(file)
51
+ upload_status_by_response(response)
52
+ end
53
+
54
+ def upload_file(file)
55
+ require 'fileutils'
56
+ @model ||= controller_name.classify.constantize
57
+ tmp = file.tempfile
58
+ file_path = File.join(Beam.config[:data_upload_path], file.original_filename)
59
+ FileUtils.cp_r tmp.path, file_path, remove_destination: true
60
+
61
+ @upload_method ?
62
+ @model.upload_file(file.original_filename, Beam.config[:data_upload_path], @upload_method) :
63
+ @model.upload_file(file.original_filename, Beam.config[:data_upload_path])
64
+ end
65
+
66
+ def not_csv_file_status
67
+ {error_msg: "Only zipped csv files are allowed"}
68
+ end
69
+
70
+ def upload_status_by_response(response)
71
+ if response[:status] == 500
72
+ failure_status
73
+ else
74
+ response[:errors].zero? ?
75
+ upload_without_queue_success_status :
76
+ upload_without_queue_failure_status(response[:errors])
77
+ end
78
+ end
79
+
80
+ def failure_status
81
+ {num_errors: 1, controller_name: self.controller_name, error_msg: "Please check the format of zipped file"}
82
+ end
83
+
84
+ def upload_through_queue_status(errors)
85
+ {
86
+ num_errors: nil,
87
+ controller_name: self.controller_name,
88
+ success_msg: "In Progress"
89
+ }
90
+ end
91
+
92
+ def upload_without_queue_success_status
93
+ {
94
+ num_errors: 0,
95
+ controller_name: self.controller_name,
96
+ success_msg: "File uploaded successfully"
97
+ }
98
+ end
99
+
100
+ def upload_without_queue_failure_status(errors)
101
+ {
102
+ num_errors: errors,
103
+ controller_name: self.controller_name,
104
+ error_msg: "There are #{errors} row(s) with errors while uploading config file." +
105
+ " Please check whether you have selected correct module or you have valid data in the file."
106
+ }
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+ end