Skip to content

Commit

Permalink
Add basic cache support for multiple file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Apr 25, 2015
1 parent c99ca8e commit 171cfc8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
21 changes: 18 additions & 3 deletions lib/refile/attachment/active_record.rb
Expand Up @@ -35,6 +35,8 @@ def attachment(name, raise_errors: false, **options)
end
end

# @ignore
# rubocop:disable Metrics/MethodLength
def accepts_attachments_for(association_name, attachment: :file)
association = reflect_on_association(association_name)
name = "#{association_name}_#{attachment.to_s.pluralize}"
Expand All @@ -45,16 +47,29 @@ def accepts_attachments_for(association_name, attachment: :file)
end

define_method :"#{name}_data" do
send(association_name).map(&:"#{attachment}_data")
if send(association_name).all? { |record| record.send("#{attachment}_attacher").valid? }
send(association_name).map(&:"#{attachment}_data")
end
end

define_method :"#{name}" do
send(association_name).map(&attachment)
end

define_method :"#{name}=" do |files|
files.select(&:present?).each do |file|
send(association_name).build(attachment => file)
cache = begin
JSON.parse(files.first)
rescue JSON::ParserError
end
files = files.drop(1)
if files.empty? and cache.present?
cache.select(&:present?).each do |file|
send(association_name).build(attachment => file.to_json)
end
else
files.select(&:present?).each do |file|
send(association_name).build(attachment => file)
end
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/refile/rails/attachment_helper.rb
Expand Up @@ -78,9 +78,7 @@ def attachment_field(object_name, method, object:, **options)
options[:data].merge!(direct: true).merge!(definition.cache.presign.as_json)
end

hidden_field_name = if options[:multiple] then "#{method}[]" else method end

html = hidden_field(object_name, hidden_field_name, value: object.send("#{method}_data").to_json, object: object, id: nil)
html = hidden_field(object_name, method, multiple: options[:multiple], value: object.send("#{method}_data").to_json, object: object, id: nil)
html + file_field(object_name, method, options)
end
end
Expand Down
45 changes: 45 additions & 0 deletions spec/refile/features/multiple_upload_spec.rb
@@ -0,0 +1,45 @@
require "refile/test_app"

feature "Multiple file uploads", :js do
scenario "Upload multiple files" do
visit "/multiple/posts/new"
fill_in "Title", with: "A cool post"
attach_file "Documents", [path("hello.txt"), path("world.txt")]
click_button "Create"

expect(download_link("Document: hello.txt")).to eq("hello")
expect(download_link("Document: world.txt")).to eq("world")
end

scenario "Fail to upload a file that is too large" do
visit "/multiple/posts/new"
fill_in "Title", with: "A cool post"
attach_file "Documents", [path("hello.txt"), path("large.txt")]
click_button "Create"

expect(page).to have_content("Documents is invalid")
end

scenario "Fail to upload a file that has the wrong format then submit" do
visit "/multiple/posts/new"
fill_in "Title", with: "A cool post"
attach_file "Documents", [path("hello.txt"), path("image.jpg")]
click_button "Create"

expect(page).to have_content("Documents is invalid")
click_button "Create"
expect(page).to have_selector("h1", text: "A cool post")
expect(page).not_to have_link("Document")
end

scenario "Upload files via form redisplay", js: true do
visit "/multiple/posts/new"
attach_file "Documents", [path("hello.txt"), path("world.txt")]
click_button "Create"
fill_in "Title", with: "A cool post"
click_button "Create"

expect(download_link("Document: hello.txt")).to eq("hello")
expect(download_link("Document: world.txt")).to eq("world")
end
end
10 changes: 0 additions & 10 deletions spec/refile/features/normal_upload_spec.rb
Expand Up @@ -14,16 +14,6 @@
expect(download_link("Document")).to eq("hello")
end

scenario "Upload multiple files", :js do
visit "/multiple/posts/new"
fill_in "Title", with: "A cool post"
attach_file "Documents", [path("hello.txt"), path("world.txt")]
click_button "Create"

expect(download_link("Document: hello.txt")).to eq("hello")
expect(download_link("Document: world.txt")).to eq("world")
end

scenario "Fail to upload a file that is too large" do
visit "/normal/posts/new"
fill_in "Title", with: "A cool post"
Expand Down
2 changes: 1 addition & 1 deletion spec/refile/test_app/app/models/document.rb
@@ -1,4 +1,4 @@
class Document < ActiveRecord::Base
belongs_to :post
attachment :file
attachment :file, cache: :limited_cache
end

0 comments on commit 171cfc8

Please sign in to comment.