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
@@ -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.tapp + '/*').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
@@ -0,0 +1,27 @@
1
+ module AssetZipImportable
2
+ module Importable
3
+ module Paths
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 remote_zip_url
13
+ zip.file.try(:authenticated_url)
14
+ end
15
+
16
+ def extract_file_paths
17
+ file_dir = Dir.glob(zip_file_dir + '/*').select{ |path|
18
+ File.directory?(path) && !path.match(/__MACOSX/)
19
+ }.first
20
+
21
+ Dir.glob(file_dir + '/*.*')
22
+ end
23
+
24
+ def extract_files
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,47 @@
1
+ require 'fileutils'
2
+
3
+ module AssetZipImportable
4
+ module Importable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Completions
9
+ include Extractions
10
+ include FilePaths
11
+
12
+ validates :zip, presence: true
13
+
14
+ mount_uploader :zip, ZipUploader
15
+ end
16
+
17
+ module ClassMethods
18
+ def import_for(model_class, attr_name)
19
+ define_method :model_class do
20
+ model_class
21
+ end
22
+
23
+ define_method :attr_name do
24
+ attr_name
25
+ end
26
+ end
27
+ end
28
+
29
+ def import!(callbacks)
30
+ download
31
+ extract
32
+
33
+ model_class.transaction do
34
+ extract_files.each do |file|
35
+ @resource = model_class.new(attr_name => file)
36
+ if @resource.save!
37
+ increment_completed_number!
38
+ callbacks[:success].try(:call, @resource)
39
+ end
40
+ end
41
+ end
42
+ rescue => e
43
+ callbacks[:error].try(:call, @resource)
44
+ raise e
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ require 'fileutils'
2
+
3
+ module AssetZipImportable
4
+ module Importable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Completions
9
+ include Extractions
10
+ include FilePaths
11
+
12
+ validates :zip, presence: true
13
+
14
+ mount_uploader :zip, ZipUploader
15
+ end
16
+
17
+ module ClassMethods
18
+ def import_for(model_class, attr_name)
19
+ define_method :model_class do
20
+ model_class
21
+ end
22
+
23
+ define_method :attr_name do
24
+ attr_name
25
+ end
26
+ end
27
+ end
28
+
29
+ def import!(callbacks)
30
+ download
31
+ extract
32
+
33
+ model_class.transaction do
34
+ extract_files.each do |file|
35
+ @resource = model_class.new(attr_name => file)
36
+ if @resource.save!
37
+ @resource.increment_completed_number!
38
+ callbacks[:success].try(:call, @resource)
39
+ end
40
+ end
41
+ end
42
+ rescue => e
43
+ callbacks[:error].try(:call, @resource)
44
+ raise e
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ class ZipUploader < CarrierWave::Uploader::Base
2
+ include CarrierWave::MimeTypes
3
+
4
+ storage Rails.env.test? || Rails.env.development? ? :file : :fog
5
+
6
+ def store_dir
7
+ upload_folder = Rails.env.test? ? 'uploads-test' : 'uploads'
8
+ "#{ upload_folder }/#{ model.class.to_s.underscore }/#{ mounted_as }/#{ model.id }"
9
+ end
10
+
11
+ def extension_white_list
12
+ %w(zip)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class ZipUploader < ApplicationUploader
2
+ include CarrierWave::MimeTypes
3
+
4
+ storage Rails.env.test? || Rails.env.development? ? :file : :fog
5
+
6
+ def store_dir
7
+ upload_folder = Rails.env.test? ? 'uploads-test' : 'uploads'
8
+ "#{ upload_folder }/#{ model.class.to_s.underscore }/#{ mounted_as }/#{ model.id }"
9
+ end
10
+
11
+ def extension_white_list
12
+ %w(zip)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>AssetZipImportable</title>
5
+ <%= stylesheet_link_tag "asset_zip_importable/application", media: "all" %>
6
+ <%= javascript_include_tag "asset_zip_importable/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,23 @@
1
+ module AssetZipImportable
2
+ class ImportWorker
3
+ include Sidekiq::Worker
4
+
5
+ def perform(model_name, id)
6
+ model_name.constantize.find(id).import!(
7
+ success: -> (resource) {
8
+ log 'SAVED', resource.inspect
9
+ },
10
+ error: -> (resource) {
11
+ log 'FALED', "#{ resource.inspect }, #{ resource.errors.full_messages.inspect }"
12
+ }
13
+ )
14
+ rescue => e
15
+ log 'ERROR', e.inspect
16
+ end
17
+
18
+ private
19
+ def log(type, text)
20
+ Rails.logger.info "[#{ type }][#{ self.class.name }] #{ text }"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module AssetZipImportable
2
+ class ImportWorker < ApplicationWorker
3
+ include Sidekiq::Worker
4
+
5
+ def perform(model_name, id)
6
+ model_name.constantize.find(id).import!(
7
+ success: -> (resource) {
8
+ log 'SAVED', resource.inspect
9
+ },
10
+ error: -> (resource) {
11
+ log 'FALED', "#{ resource.inspect }, #{ resource.errors.full_messages.inspect }"
12
+ }
13
+ )
14
+ rescue => e
15
+ log 'ERROR', e.inspect
16
+ end
17
+
18
+ private
19
+ def log(type, text)
20
+ Rails.logger.info "[#{ type }][#{ self.class.name }] #{ text }"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ class ImportWorker < ApplicationWorker
2
+ include Sidekiq::Worker
3
+
4
+ def log(type, text)
5
+ Rails.logger.info "[#{ type }][#{ self.class.name }] #{ text }"
6
+ end
7
+ def perform(id)
8
+ @import = ImportImageAttachment.find(id)
9
+ @import.download
10
+ @import.extract
11
+ ImageAttachment.transaction do
12
+ @import.extract_files.each do |file|
13
+ image = ImageAttachment.new({ image: file })
14
+ if image.save
15
+ @import.increment_completed_number!
16
+ log 'SAVED', image.inspect
17
+ else
18
+ log 'FALED', "#{ image.inspect }, #{ image.errors.full_messages.inspect }"
19
+ end
20
+ end
21
+ end
22
+ rescue => e
23
+ log 'ERROR', e.inspect
24
+ end
25
+ end
File without changes
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ AssetZipImportable::Engine.routes.draw do
2
+ end
@@ -0,0 +1,5 @@
1
+ module AssetZipImportable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace AssetZipImportable
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module AssetZipImportable
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module AssetZipImportable
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module AssetZipImportable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'zip/zip'
2
+ require 'carrierwave'
3
+ require 'fog'
4
+ require 'unf'
5
+
6
+ require "asset_zip_importable/engine"
7
+
8
+ module AssetZipImportable
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'zip/zip'
2
+ require 'carrierwave'
3
+ require 'fog'
4
+
5
+ require "asset_zip_importable/engine"
6
+
7
+ module AssetZipImportable
8
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generate asset zip importar model
3
+
4
+ Example:
5
+ rails generate asset_zip_importar picture image
6
+
7
+ This will create:
8
+ app/models/import_picture.rb
9
+ db/migrate/YYYYMMDDXXXXX_import_pictures.rb
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate asset zip importar model
3
+
4
+ Example:
5
+ rails generate asset_zip_importar picture image
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,36 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record'
4
+
5
+ class AssetZipImportarGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ extend ActiveRecord::Generators::Migration
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ argument :model_name, type: :string
12
+ argument :asset_attribute_name, type: :string
13
+
14
+ def generate_asset_zip_importar
15
+ template "importar_model.rb.erb", "app/models/#{ importar_name }.rb"
16
+ migration_template "migration.rb.erb", "db/migrate/create_#{ importars_name }.rb"
17
+ end
18
+
19
+ def importar_model_name
20
+ importar_name.camelize
21
+ end
22
+
23
+ def importar_name
24
+ "import_#{ model_name }_#{ asset_attribute_name }"
25
+ end
26
+
27
+ def importars_name
28
+ importar_name.pluralize
29
+ end
30
+
31
+ class << self
32
+ def next_migration_number(path)
33
+ ActiveRecord::Generators::Base.next_migration_number(path)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record'
4
+
5
+ class AssetZipImportarGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ extend ActiveRecord::Generators::Migration
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ argument :model_name, type: :string
12
+ argument :asset_attribute_name, type: :string
13
+
14
+ def generate_asset_zip_importar
15
+ template "import_model.rb.erb", "app/models/#{ importar_name }.rb"
16
+ migration_template "migration.rb.erb", "db/migrate/create_#{ importars_name }.rb"
17
+ end
18
+
19
+ def importar_model_name
20
+ importar_name.camelize
21
+ end
22
+
23
+ def importar_name
24
+ "import_#{ model_name }_#{ asset_attribute_name }"
25
+ end
26
+
27
+ def importars_name
28
+ importar_name.pluralize
29
+ end
30
+
31
+ class << self
32
+ def next_migration_number(path)
33
+ ActiveRecord::Generators::Base.next_migration_number(path)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class AssetZipImportarMigrationGenerator < ActiveRecord::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def generate_asset_zip_importar_migration
7
+ migration_template "migration.rb.erb", "db/migrate/create_import_#{ model_name.pluralize }.rb"
8
+ end
9
+ end