Skip to content

Commit

Permalink
correct comportement when ActiveStorage::Attached::Many is given as…
Browse files Browse the repository at this point in the history
… paramter
  • Loading branch information
madeindjs committed Dec 21, 2018
1 parent 33b762a commit 278f3f2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/active_storage/send_zip/version.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions lib/active_storage/send_zip_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/active_storage/send_zip_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 278f3f2

Please sign in to comment.