diff --git a/CHANGELOG.yml b/CHANGELOG.yml index 0b6bb0a..0d65cf5 100644 --- a/CHANGELOG.yml +++ b/CHANGELOG.yml @@ -1,3 +1,5 @@ +0,3,1: + - correct comportement when `ActiveStorage::Attached::Many` is given as paramter 0.3.0: - correct subfolders creation (and break some methods signature) 0.2.0: diff --git a/Gemfile b/Gemfile index 1eb9c87..da43cfa 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in active_storage-send_zip.gemspec gemspec -gem "rails", "~> 5.2" +gem "rails", "> 5.2" gem "rubyzip", "~> 1.2" diff --git a/Gemfile.lock b/Gemfile.lock index cf20eb4..2fd1886 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - active_storage-send_zip (0.2.0) + active_storage-send_zip (0.3.1) GEM remote: https://rubygems.org/ @@ -122,9 +122,9 @@ DEPENDENCIES active_storage-send_zip! bundler (~> 1.17) minitest (~> 5.0) - rails (~> 5.2) + rails (> 5.2) rake (~> 10.0) rubyzip (~> 1.2) BUNDLED WITH - 1.17.1 + 1.17.2 diff --git a/lib/active_storage/send_zip/version.rb b/lib/active_storage/send_zip/version.rb index e522d55..2a2cb80 100644 --- a/lib/active_storage/send_zip/version.rb +++ b/lib/active_storage/send_zip/version.rb @@ -1,6 +1,6 @@ module ActiveStorage module SendZip # The version of this gem - VERSION = '0.3.0'.freeze + VERSION = '0.3.1'.freeze end end diff --git a/lib/active_storage/send_zip_helper.rb b/lib/active_storage/send_zip_helper.rb index c5702f6..37a6049 100644 --- a/lib/active_storage/send_zip_helper.rb +++ b/lib/active_storage/send_zip_helper.rb @@ -9,16 +9,14 @@ module ActiveStorage module SendZipHelper # Download active storage files on server in a temporary folder # - # @param files [ActiveStorage::Attached::Many] files to save + # @param files [ActiveStorage::Attached::One|ActiveStorage::Attached::Many|Array|Hash] file(s) to save # @return [String] folder path of saved files def self.save_files_on_server(files) require 'zip' # get a temporary folder and create it temp_folder = Dir.mktmpdir 'active_storage-send_zip' - if files.is_a? Array - files.each { |file| save_file_on_server(file, temp_folder) } - elsif files.is_a? Hash + if files.is_a? Hash filepaths = [] files.each do |subfolder, filesHash| @@ -27,6 +25,10 @@ def self.save_files_on_server(files) filepaths << save_file_on_server(f, temp_folder, subfolder: subfolder.to_s) end end + elsif files.respond_to? :each + files.each { |file| save_file_on_server(file, temp_folder) } + else + raise ArgumentError, '`files` must be an hash or an iterable object' end temp_folder diff --git a/test/active_storage/send_zip_helper_test.rb b/test/active_storage/send_zip_helper_test.rb index 2247fe7..d9b55e2 100644 --- a/test/active_storage/send_zip_helper_test.rb +++ b/test/active_storage/send_zip_helper_test.rb @@ -54,6 +54,12 @@ def test_it_should_save_files_in_differents_folders assert_produce_nested_files files, folder_count: 2, files_count: 3 end + def test_it_should_raise_an_exception + assert_raises ArgumentError do + ActiveStorage::SendZipHelper.save_files_on_server 'bad boi' + end + end + def test_it_should_save_files_in_differents_folders_with_root_files files = { 'folder A' => [