asset_zip_importable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/asset_zip_importable/application.js +13 -0
  6. data/app/assets/stylesheets/asset_zip_importable/application.css +15 -0
  7. data/app/controllers/asset_zip_importable/application_controller.rb +4 -0
  8. data/app/controllers/concerns/asset_zip_importable/importar_operatable.rb +38 -0
  9. data/app/controllers/concerns/asset_zip_importable/importar_operatable.rb~ +38 -0
  10. data/app/helpers/asset_zip_importable/application_helper.rb +43 -0
  11. data/app/helpers/asset_zip_importable/application_helper.rb~ +43 -0
  12. data/app/models/concerns/asset_zip_importable/importable/completions.rb +29 -0
  13. data/app/models/concerns/asset_zip_importable/importable/completions.rb~ +29 -0
  14. data/app/models/concerns/asset_zip_importable/importable/extractions.rb +46 -0
  15. data/app/models/concerns/asset_zip_importable/importable/extractions.rb~ +46 -0
  16. data/app/models/concerns/asset_zip_importable/importable/file_paths.rb +36 -0
  17. data/app/models/concerns/asset_zip_importable/importable/file_paths.rb~ +36 -0
  18. data/app/models/concerns/asset_zip_importable/importable/paths_filenames.rb~ +27 -0
  19. data/app/models/concerns/asset_zip_importable/importable.rb +47 -0
  20. data/app/models/concerns/asset_zip_importable/importable.rb~ +47 -0
  21. data/app/uploaders/zip_uploader.rb +14 -0
  22. data/app/uploaders/zip_uploader.rb~ +14 -0
  23. data/app/views/layouts/asset_zip_importable/application.html.erb +14 -0
  24. data/app/workers/asset_zip_importable/import_worker.rb +23 -0
  25. data/app/workers/asset_zip_importable/import_worker.rb~ +23 -0
  26. data/app/workers/importar_worker.rb~ +25 -0
  27. data/config/application.rb~ +0 -0
  28. data/config/routes.rb +2 -0
  29. data/lib/asset_zip_importable/engine.rb +5 -0
  30. data/lib/asset_zip_importable/railtie.rb +4 -0
  31. data/lib/asset_zip_importable/railtie.rb~ +4 -0
  32. data/lib/asset_zip_importable/version.rb +3 -0
  33. data/lib/asset_zip_importable.rb +9 -0
  34. data/lib/asset_zip_importable.rb~ +8 -0
  35. data/lib/generators/asset_zip_importar/USAGE +9 -0
  36. data/lib/generators/asset_zip_importar/USAGE~ +8 -0
  37. data/lib/generators/asset_zip_importar/asset_zip_importar_generator.rb +36 -0
  38. data/lib/generators/asset_zip_importar/asset_zip_importar_generator.rb~ +36 -0
  39. data/lib/generators/asset_zip_importar/asset_zip_importar_migration_generator.rb~ +9 -0
  40. data/lib/generators/asset_zip_importar/migration_generator.rb~ +0 -0
  41. data/lib/generators/asset_zip_importar/templates/import_model.rb.erb~ +2 -0
  42. data/lib/generators/asset_zip_importar/templates/importar_model.rb.erb +4 -0
  43. data/lib/generators/asset_zip_importar/templates/importar_model.rb.erb~ +4 -0
  44. data/lib/generators/asset_zip_importar/templates/migration.rb.erb +12 -0
  45. data/lib/generators/asset_zip_importar/templates/migration.rb.erb~ +12 -0
  46. data/lib/tasks/asset_zip_importable_tasks.rake +4 -0
  47. data/test/asset_zip_importable_test.rb +7 -0
  48. data/test/dummy/README.rdoc +28 -0
  49. data/test/dummy/Rakefile +6 -0
  50. data/test/dummy/app/assets/javascripts/application.js +13 -0
  51. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  52. data/test/dummy/app/controllers/application_controller.rb +5 -0
  53. data/test/dummy/app/helpers/application_helper.rb +2 -0
  54. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  55. data/test/dummy/bin/bundle +3 -0
  56. data/test/dummy/bin/rails +4 -0
  57. data/test/dummy/bin/rake +4 -0
  58. data/test/dummy/config/application.rb +23 -0
  59. data/test/dummy/config/boot.rb +5 -0
  60. data/test/dummy/config/database.yml +25 -0
  61. data/test/dummy/config/environment.rb +5 -0
  62. data/test/dummy/config/environments/development.rb +37 -0
  63. data/test/dummy/config/environments/production.rb +78 -0
  64. data/test/dummy/config/environments/test.rb +39 -0
  65. data/test/dummy/config/initializers/assets.rb +8 -0
  66. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  67. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  68. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  69. data/test/dummy/config/initializers/inflections.rb +16 -0
  70. data/test/dummy/config/initializers/mime_types.rb +4 -0
  71. data/test/dummy/config/initializers/session_store.rb +3 -0
  72. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  73. data/test/dummy/config/locales/en.yml +23 -0
  74. data/test/dummy/config/routes.rb +4 -0
  75. data/test/dummy/config/secrets.yml +22 -0
  76. data/test/dummy/config.ru +4 -0
  77. data/test/dummy/public/404.html +67 -0
  78. data/test/dummy/public/422.html +67 -0
  79. data/test/dummy/public/500.html +66 -0
  80. data/test/dummy/public/favicon.ico +0 -0
  81. data/test/integration/navigation_test.rb +10 -0
  82. data/test/lib/generators/asset_zip_importable/asset_zip_importar_generator_test.rb +16 -0
  83. data/test/test_helper.rb +17 -0
  84. metadata +353 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 769ec53b75c24efc352dd70a72b8870bc4b572b6
4
+ data.tar.gz: 0c5ae6fa680102fad51e4ef3bc6e240cc38fb563
5
+ SHA512:
6
+ metadata.gz: 367f5d23bd04018fa460a1baf61141a5f531c30c588bf9f55f4c7e84fb9b1c60e9a989152e47fd1f55c9cc6da8a2b240033614b1d7f94152ddc5640a0df0db4d
7
+ data.tar.gz: 78c8b859f02caf74b416648ff8bd80d6cad4e60666ac4195386e1b736d60c76c2765fa7fa76f70d0af411c08dd6f17b4eeae80f19d8bf61736b25cd3b8732dac
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = AssetZipImportable
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AssetZipImportable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module AssetZipImportable
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,38 @@
1
+ module AssetZipImportable
2
+ module ImportarOperatable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def importar(importar_class, options = {})
7
+ define_method :importar_class do
8
+ importar_class
9
+ end
10
+
11
+ define_method :after_create_redirect_path_method do
12
+ options[:after_create_redirect_path_method]
13
+ end
14
+ end
15
+ end
16
+
17
+ def index
18
+ @imports = importar_class.order('created_at DESC').page(params[:page]).per(20)
19
+ @new_import = importar_class.new
20
+ end
21
+
22
+ def create
23
+ @importar = importar_class.new(importar_params)
24
+ message = if @importar.save
25
+ ImportWorker.perform_async(@importar.class.name, @importar.id)
26
+ { notice: 'インポートを開始しました' }
27
+ else
28
+ { alert: 'インポートに失敗しました' }
29
+ end
30
+ redirect_to send(after_create_redirect_path_method), message
31
+ end
32
+
33
+ private
34
+ def importar_params
35
+ params.require(importar_class.name.underscore).permit(:zip)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ module AssetZipImportable
2
+ module ImportarOperatable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def importar(importar_class, options = {})
7
+ define_method :importar_class do
8
+ importar_class
9
+ end
10
+
11
+ define_method :after_create_redirect_path_method do
12
+ options[:after_create_redirect_path_method]
13
+ end
14
+ end
15
+ end
16
+
17
+ def index
18
+ @imports = importar_class.order('created_at DESC').page(params[:page]).per(20)
19
+ @new_import = importar_class.new
20
+ end
21
+
22
+ def create
23
+ @importar = importar_class.new(importar_params)
24
+ message = if @importar.save
25
+ ImportWorker.perform_async(@importar.model_class.name, @importar.id)
26
+ { notice: 'インポートを開始しました' }
27
+ else
28
+ { alert: 'インポートに失敗しました' }
29
+ end
30
+ redirect_to send(after_create_redirect_path_method), message
31
+ end
32
+
33
+ private
34
+ def importar_params
35
+ params.require(importar_class.name.underscore).permit(:zip)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ module AssetZipImportable
2
+ module ApplicationHelper
3
+ def import_form_for(new_import, url, html_options = {})
4
+ form_for(new_import, url: url, html: { class: 'form-inline' }.merge(html_options)) do |f|
5
+ f.file_field(:zip) +
6
+ f.submit('インポート', class: 'btn')
7
+ end
8
+ end
9
+
10
+ def imports_table(imports)
11
+ content_tag(:table, class: 'table') do
12
+ content_tag(:thead) do
13
+ content_tag(:tr) do
14
+ content_tag(:th, 'id') +
15
+ content_tag(:th, 'zip') +
16
+ content_tag(:th, 'アップロード日時') +
17
+ content_tag(:th, '処理済み件数') +
18
+ content_tag(:th, '全件数') +
19
+ content_tag(:th, '状態')
20
+ end
21
+ end +
22
+ content_tag(:tbody) do
23
+ imports.map do |import|
24
+ content_tag(:tr) do
25
+ content_tag(:td, import.id.to_s) +
26
+ content_tag(:td, link_to(import.read_attribute(:zip), import.zip_url)) +
27
+ content_tag(:td, import.created_at.to_s) +
28
+ content_tag(:td, import.completed_number.to_s) +
29
+ content_tag(:td, import.total_number.to_s) +
30
+ content_tag(:td) do
31
+ if import.completed?
32
+ content_tag(:span, '完了', class: 'label label-success')
33
+ else
34
+ content_tag(:span, '未完了', class: 'label label-default')
35
+ end
36
+ end
37
+ end
38
+ end.join.html_safe
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ module AssetZipImportable
2
+ module ApplicationHelper
3
+ def import_form_for(new_import, url, html_options = {})
4
+ form_for(new_import, url: url, html: { class: 'form-inline' }.merge(html_options)) do |f|
5
+ f.file_field(:zip) +
6
+ f.submit('インポート', class: 'btn')
7
+ end
8
+ end
9
+
10
+ def imports_table(imports)
11
+ content_tag(:table, class: 'table') do
12
+ content_tag(:thead) do
13
+ content_tag(:tr) do
14
+ content_tag(:th, 'id') +
15
+ content_tag(:th, 'zip') +
16
+ content_tag(:th, 'アップロード日時') +
17
+ content_tag(:th, '処理済み件数') +
18
+ content_tag(:th, '全件数') +
19
+ content_tag(:th, '状態')
20
+ end
21
+ end +
22
+ content_tag(:tbody) do
23
+ imports.map do |import|
24
+ content_tag(:tr) do
25
+ content_tag(:td, import.id.to_s) +
26
+ content_tag(:td, link_to(import.read_attribute(:zip), import.zip_url)) +
27
+ content_tag(:td, import.created_at.to_s) +
28
+ content_tag(:td, import.completed_number.to_s) +
29
+ content_tag(:td, import.total_number.to_s) +
30
+ content_tag(:td) do
31
+ if import.completed?
32
+ content_tag(:span, '完了', class: 'label label-success')
33
+ else
34
+ content_tag(:span, '未完了', class: 'label label-default')
35
+ end
36
+ end
37
+ end
38
+ end.join.html_safe
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module Completions
4
+ def complete!
5
+ self.completed_at = Time.now
6
+ save!
7
+ end
8
+
9
+ def completed?
10
+ !!completed_at
11
+ end
12
+
13
+ def increment_completed_number!
14
+ self.completed_number ||= 0
15
+ self.completed_number += 1
16
+ if completed_number == self.total_number
17
+ complete!
18
+ else
19
+ save!
20
+ end
21
+ end
22
+
23
+ def progress
24
+ return 0 if completed_number.nil?
25
+ (completed_number.to_f / total_number.to_f * 100).round
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module Completions
4
+ def complete!
5
+ self.completed_at = Time.now
6
+ save!
7
+ end
8
+
9
+ def completed?
10
+ !!completed_at
11
+ end
12
+
13
+ def increment_completed_number!
14
+ self.completed_number ||= 0
15
+ self.completed_number += 1
16
+ if completed_number == self.total_number
17
+ complete!
18
+ else
19
+ save!
20
+ end
21
+ end
22
+
23
+ def progress
24
+ return 0 if self.completed_number.nil?
25
+ (self.completed_number.to_f / self.total_number.to_f * 100).round
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,46 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module Extractions
4
+ ACCEPT_FILE_TYPES = %w(JPEG GIF PNG PDF)
5
+
6
+ def download
7
+ FileUtils.mkdir_p(zip_file_dir)
8
+ File.open(zip_file, 'wb') do |opened_zip_file|
9
+ opened_zip_file << uploaded_zip_file.read
10
+ end
11
+ end
12
+
13
+ def extract
14
+ return unless FileTest.exist?(zip_file)
15
+
16
+ Zip::ZipFile.open(zip_file) do |opened_zip_file|
17
+ opened_zip_file.each do |entry|
18
+ file_path = zip_file_dir + '/' + entry.to_s
19
+
20
+ FileUtils::mkdir_p(File.dirname(file_path))
21
+ opened_zip_file.extract(entry, file_path) { true }
22
+
23
+ if accept_file_type?(file_path)
24
+ self.total_number ||= 0
25
+ self.total_number += 1
26
+ else
27
+ unless FileTest.directory?(file_path)
28
+ File.delete(file_path)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ save!
34
+ end
35
+
36
+ def accept_file_type?(file_path)
37
+ ACCEPT_FILE_TYPES.include?(_file_type(file_path))
38
+ end
39
+
40
+ private
41
+ def _file_type(file_path)
42
+ `file -b #{ file_path }`.split(' ').first
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module Extractions
4
+ ACCEPT_FILE_TYPES = %w(JPEG GIF PNG PDF)
5
+
6
+ def download
7
+ FileUtils.mkdir_p(zip_file_dir)
8
+ File.open(zip_file, 'wb') do |opened_zip_file|
9
+ opened_zip_file << uploaded_zip_file.read
10
+ end
11
+ end
12
+
13
+ def extract
14
+ return unless FileTest.exist?(zip_file)
15
+
16
+ Zip::ZipFile.open(zip_file) do |opened_zip_file|
17
+ opened_zip_file.each do |entry|
18
+ file_path = zip_file_dir + '/' + entry.to_s
19
+
20
+ FileUtils::mkdir_p(File.dirname(file_path))
21
+ opended_zip_file.extract(entry, file_path) { true }
22
+
23
+ if accept_file_type?(file_path)
24
+ self.total_number ||= 0
25
+ self.total_number += 1
26
+ else
27
+ unless FileTest.directory?(file_path)
28
+ File.delete(file_path)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ save!
34
+ end
35
+
36
+ def accept_file_type?(file_path)
37
+ ACCEPT_FILE_TYPES.include?(_file_type(file_path))
38
+ end
39
+
40
+ private
41
+ def _file_type(file_path)
42
+ `file -b #{ file_path }`.split(' ').first
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module FilePaths
4
+ def zip_file
5
+ Rails.root.join('tmp', model_class.name.underscore.pluralize, id.to_s, read_attribute(:zip))
6
+ end
7
+
8
+ def zip_file_dir
9
+ File.dirname(zip_file)
10
+ end
11
+
12
+ def uploaded_zip_file
13
+ case zip.file
14
+ when CarrierWave::SanitizedFile
15
+ open(zip.file.path)
16
+ when CarrierWave::Storage::Fog::File
17
+ open(zip.file.try(:authenticated_url))
18
+ end
19
+ end
20
+
21
+ def extract_file_paths
22
+ file_dir = Dir.glob(zip_file_dir + '/*').detect{ |path|
23
+ File.directory?(path) && !path.match(/__MACOSX/)
24
+ }
25
+ Dir.glob(file_dir + '/*.*')
26
+ end
27
+
28
+ def extract_files
29
+ extract_file_paths.inject([]){ |result, path|
30
+ result << File.open(path)
31
+ result
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end