asset_zip_importable 0.0.1
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/app/assets/javascripts/asset_zip_importable/application.js +13 -0
- data/app/assets/stylesheets/asset_zip_importable/application.css +15 -0
- data/app/controllers/asset_zip_importable/application_controller.rb +4 -0
- data/app/controllers/concerns/asset_zip_importable/importar_operatable.rb +38 -0
- data/app/controllers/concerns/asset_zip_importable/importar_operatable.rb~ +38 -0
- data/app/helpers/asset_zip_importable/application_helper.rb +43 -0
- data/app/helpers/asset_zip_importable/application_helper.rb~ +43 -0
- data/app/models/concerns/asset_zip_importable/importable/completions.rb +29 -0
- data/app/models/concerns/asset_zip_importable/importable/completions.rb~ +29 -0
- data/app/models/concerns/asset_zip_importable/importable/extractions.rb +46 -0
- data/app/models/concerns/asset_zip_importable/importable/extractions.rb~ +46 -0
- data/app/models/concerns/asset_zip_importable/importable/file_paths.rb +36 -0
- data/app/models/concerns/asset_zip_importable/importable/file_paths.rb~ +36 -0
- data/app/models/concerns/asset_zip_importable/importable/paths_filenames.rb~ +27 -0
- data/app/models/concerns/asset_zip_importable/importable.rb +47 -0
- data/app/models/concerns/asset_zip_importable/importable.rb~ +47 -0
- data/app/uploaders/zip_uploader.rb +14 -0
- data/app/uploaders/zip_uploader.rb~ +14 -0
- data/app/views/layouts/asset_zip_importable/application.html.erb +14 -0
- data/app/workers/asset_zip_importable/import_worker.rb +23 -0
- data/app/workers/asset_zip_importable/import_worker.rb~ +23 -0
- data/app/workers/importar_worker.rb~ +25 -0
- data/config/application.rb~ +0 -0
- data/config/routes.rb +2 -0
- data/lib/asset_zip_importable/engine.rb +5 -0
- data/lib/asset_zip_importable/railtie.rb +4 -0
- data/lib/asset_zip_importable/railtie.rb~ +4 -0
- data/lib/asset_zip_importable/version.rb +3 -0
- data/lib/asset_zip_importable.rb +9 -0
- data/lib/asset_zip_importable.rb~ +8 -0
- data/lib/generators/asset_zip_importar/USAGE +9 -0
- data/lib/generators/asset_zip_importar/USAGE~ +8 -0
- data/lib/generators/asset_zip_importar/asset_zip_importar_generator.rb +36 -0
- data/lib/generators/asset_zip_importar/asset_zip_importar_generator.rb~ +36 -0
- data/lib/generators/asset_zip_importar/asset_zip_importar_migration_generator.rb~ +9 -0
- data/lib/generators/asset_zip_importar/migration_generator.rb~ +0 -0
- data/lib/generators/asset_zip_importar/templates/import_model.rb.erb~ +2 -0
- data/lib/generators/asset_zip_importar/templates/importar_model.rb.erb +4 -0
- data/lib/generators/asset_zip_importar/templates/importar_model.rb.erb~ +4 -0
- data/lib/generators/asset_zip_importar/templates/migration.rb.erb +12 -0
- data/lib/generators/asset_zip_importar/templates/migration.rb.erb~ +12 -0
- data/lib/tasks/asset_zip_importable_tasks.rake +4 -0
- data/test/asset_zip_importable_test.rb +7 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/lib/generators/asset_zip_importable/asset_zip_importar_generator_test.rb +16 -0
- data/test/test_helper.rb +17 -0
- 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,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
|