Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Provide an 'Add another file' link that uses Javascript so that users…

… can upload more than one file
  • Loading branch information...
commit 30fb9d4a5e2861da1bd371be2a1d56b1e53a40e0 1 parent 68a6a7f
@radar radar authored
View
2  Gemfile
@@ -25,6 +25,6 @@ group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl', '2.6.4'
gem 'email_spec', '1.2.1'
- gem 'launchy'
+ gem 'database_cleaner'
end
View
6 Gemfile.lock
@@ -28,7 +28,6 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
- addressable (2.2.7)
arel (3.0.2)
bcrypt-ruby (3.0.1)
builder (3.0.0)
@@ -50,6 +49,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
+ database_cleaner (0.7.2)
devise (2.1.0.rc)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
@@ -73,8 +73,6 @@ GEM
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.6.6)
- launchy (2.1.0)
- addressable (~> 2.2.6)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -164,12 +162,12 @@ DEPENDENCIES
cancan (= 1.6.7)
capybara (= 1.1.2)
coffee-rails (~> 3.2.1)
+ database_cleaner
devise (= 2.1.0.rc)
dynamic_form (= 1.1.4)
email_spec (= 1.2.1)
factory_girl (= 2.6.4)
jquery-rails
- launchy
paperclip (= 2.7.0)
rails (= 3.2.3)
rspec-rails (~> 2.9)
View
9 app/assets/javascripts/tickets.js.coffee
@@ -1,3 +1,12 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
+
+$(->
+ $('a#add_another_file').click(->
+ url = "/files/new?number=" + $('#files input').length
+ $.get(url, (data)->
+ $('#files').append(data)
+ )
+ )
+)
View
10 app/controllers/files_controller.rb
@@ -1,5 +1,15 @@
class FilesController < ApplicationController
before_filter :authenticate_user!
+
+ def new
+ @ticket = Ticket.new
+ asset = @ticket.assets.build
+ render :partial => "files/form",
+ :locals => { :number => params[:number].to_i,
+ :asset => asset }
+
+ end
+
def show
asset = Asset.find(params[:id])
if can?(:view, asset.ticket.project)
View
2  app/controllers/tickets_controller.rb
@@ -8,7 +8,7 @@ class TicketsController < ApplicationController
def new
@ticket = @project.tickets.build
- 3.times { @ticket.assets.build }
+ @ticket.assets.build
end
def create
View
10 app/views/files/_form.html.erb
@@ -0,0 +1,10 @@
+<%= fields_for @ticket do |f| %>
+ <p>
+ <%= f.fields_for :assets, :child_index => number do |asset| %>
+ <p>
+ <%= asset.label :asset, "File ##{number += 1}" %>
+ <%= asset.file_field :asset %>
+ </p>
+ <% end %>
+ </p>
+<% end %>
View
15 app/views/tickets/_form.html.erb
@@ -9,11 +9,14 @@
<%= f.text_area :description %>
</p>
<% number = 0 %>
- <%= f.fields_for :assets do |asset| %>
- <p>
- <%= asset.label :asset, "File ##{number += 1}" %>
- <%= asset.file_field :asset %>
- </p>
- <% end %>
+ <div id='files'>
+ <%= render :partial => "files/form",
+ :locals => { :number => number } %>
+ </div>
+
+ <%= link_to "Add another file", 'javascript:',
+ :id => "add_another_file" %>
+
+
<%= f.submit %>
<% end %>
View
12 spec/integration/creating_tickets_spec.rb
@@ -6,6 +6,7 @@
user = Factory(:confirmed_user, :email => "ticketee@example.com")
define_permission!(user, "view", project)
define_permission!(user, "create tickets", project)
+ puts "Signing in as..."
sign_in_as!(user)
visit '/'
@@ -40,18 +41,17 @@
page.should have_content("Description is too short")
end
- scenario "Creating a ticket with an attachment" do
+ scenario "Creating a ticket with an attachment", :js => true do
fill_in "Title", :with => "Add documentation for blink tag"
- fill_in "Description", :with => "The blink tag has an undocumented speed attribute"
- attach_file "File #1", "spec/fixtures/speed.txt"
- attach_file "File #2", "spec/fixtures/spin.txt"
- attach_file "File #3", "spec/fixtures/gradient.txt"
+ fill_in "Description", :with => "Blink tag's undocumented speed attribute"
+ attach_file "File #1", File.expand_path("spec/fixtures/speed.txt")
+ click_link "Add another file"
+ attach_file "File #2", File.expand_path("spec/fixtures/spin.txt")
click_button "Create Ticket"
page.should have_content("Ticket has been created.")
within("#ticket .assets") do
page.should have_content("speed.txt")
page.should have_content("spin.txt")
- page.should have_content("gradient.txt")
end
end
end
View
2  spec/spec_helper.rb
@@ -29,7 +29,7 @@
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
- config.use_transactional_fixtures = true
+ config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
View
16 spec/support/database_cleaning.rb
@@ -0,0 +1,16 @@
+RSpec.configure do |config|
+
+ config.before(:suite) do
+ DatabaseCleaner.strategy = :truncation
+ DatabaseCleaner.clean_with(:truncation)
+ end
+
+ config.before(:each) do
+ DatabaseCleaner.start
+ end
+
+ config.after(:each) do
+ DatabaseCleaner.clean
+ end
+
+end

0 comments on commit 30fb9d4

Please sign in to comment.
Something went wrong with that request. Please try again.