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 982ddf6241a78a9e6547e16af29086627d9e72d2 1 parent 27060df
@radar radar authored
View
8 Gemfile
@@ -1,8 +1,8 @@
source 'http://rubygems.org'
-gem 'rails', '3.1.0.beta1'
+gem 'rails', :path => "~/Sites/rails/edge-rails"
# gem 'arel', :git => 'git://github.com/rails/arel.git'
-# gem 'rack', '1.3.0.beta'
+gem 'rack', :path => "~/Sites/gems/rack"
# gem 'sprockets', :git => "git://github.com/sstephenson/sprockets.git"
gem 'sqlite3'
@@ -17,6 +17,8 @@ gem 'cancan'
gem 'paperclip'
+gem 'jquery-rails'
+
group :test, :development do
gem 'rspec-rails', '~> 2.5'
end
@@ -24,7 +26,7 @@ end
group :test do
gem 'rack-test', :git => "git://github.com/radar/rack-test"
gem 'cucumber-rails'
- gem 'capybara'
+ gem 'capybara', :git => "git://github.com/radar/capybara"
gem 'database_cleaner'
gem 'factory_girl'
gem 'launchy'
View
74 Gemfile.lock
@@ -1,12 +1,29 @@
GIT
+ remote: git://github.com/radar/capybara
+ revision: 43ea95f338cb9371b7b27b2082e18349873ff042
+ specs:
+ capybara (1.0.0.beta1)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 0.2.0)
+ xpath (~> 0.1.4)
+
+GIT
remote: git://github.com/radar/rack-test
revision: a4b7ef0a736d95205bdb8d79c1abb182cb4a8a71
specs:
rack-test (0.6.0)
rack (>= 1.0)
-GEM
- remote: http://rubygems.org/
+PATH
+ remote: ~/Sites/gems/rack
+ specs:
+ rack (1.3.0.beta)
+
+PATH
+ remote: ~/Sites/rails/edge-rails
specs:
actionmailer (3.1.0.beta1)
actionpack (= 3.1.0.beta1)
@@ -38,20 +55,28 @@ GEM
activesupport (= 3.1.0.beta1)
activesupport (3.1.0.beta1)
multi_json (~> 1.0)
+ rails (3.1.0.beta1)
+ actionmailer (= 3.1.0.beta1)
+ actionpack (= 3.1.0.beta1)
+ activerecord (= 3.1.0.beta1)
+ activeresource (= 3.1.0.beta1)
+ activesupport (= 3.1.0.beta1)
+ bundler (~> 1.0)
+ railties (= 3.1.0.beta1)
+ railties (3.1.0.beta1)
+ actionpack (= 3.1.0.beta1)
+ activesupport (= 3.1.0.beta1)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ thor (~> 0.14.6)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
arel (2.1.0)
bcrypt-ruby (2.1.4)
builder (3.0.0)
cancan (1.6.4)
- capybara (0.4.1.2)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.3)
- celerity (0.8.9)
childprocess (0.1.8)
ffi (~> 1.0.6)
coffee-script (2.2.0)
@@ -69,7 +94,6 @@ GEM
cucumber (>= 0.10.1)
nokogiri (>= 1.4.4)
rack-test (>= 0.5.7)
- culerity (0.2.15)
database_cleaner (0.6.7)
devise (1.2.1)
bcrypt-ruby (~> 2.1.2)
@@ -89,6 +113,9 @@ GEM
json (>= 1.4.6)
hike (1.0.0)
i18n (0.6.0beta1)
+ jquery-rails (0.2.7)
+ rails (~> 3.0)
+ thor (~> 0.14.4)
json (1.5.1)
json_pure (1.5.1)
launchy (0.4.0)
@@ -106,27 +133,12 @@ GEM
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
polyglot (0.3.1)
- rack (1.3.0.beta)
rack-cache (1.0.1)
rack (>= 0.4)
rack-mount (0.7.2)
rack (>= 1.0.0)
rack-ssl (1.3.2)
rack
- rails (3.1.0.beta1)
- actionmailer (= 3.1.0.beta1)
- actionpack (= 3.1.0.beta1)
- activerecord (= 3.1.0.beta1)
- activeresource (= 3.1.0.beta1)
- activesupport (= 3.1.0.beta1)
- bundler (~> 1.0)
- railties (= 3.1.0.beta1)
- railties (3.1.0.beta1)
- actionpack (= 3.1.0.beta1)
- activesupport (= 3.1.0.beta1)
- rack-ssl (~> 1.3.2)
- rake (>= 0.8.7)
- thor (~> 0.14.6)
rake (0.8.7)
rspec (2.5.0)
rspec-core (~> 2.5.0)
@@ -169,7 +181,7 @@ PLATFORMS
DEPENDENCIES
cancan
- capybara
+ capybara!
coffee-script
cucumber-rails
database_cleaner
@@ -177,10 +189,12 @@ DEPENDENCIES
dynamic_form
email_spec
factory_girl
+ jquery-rails
launchy
paperclip
+ rack!
rack-test!
- rails (= 3.1.0.beta1)
+ rails!
rspec-rails (~> 2.5)
sass (~> 3.1.0.alpha)
sqlite3
View
10 app/assets/javascripts/tickets.js.coffee
@@ -1,3 +1,13 @@
# 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
2  app/assets/stylesheets/tickets.css.scss
@@ -3,3 +3,5 @@
They will automatically be included in application.css.
You can use Sass (SCSS) here: http://sass-lang.com/
*/
+
+$('a#add_another_file').click($ -> alert("whoops!"))
View
22 app/controllers/files_controller.rb
@@ -0,0 +1,22 @@
+class FilesController < ApplicationController
+ before_filter :authenticate_user!
+ def show
+ asset = Asset.find(params[:id])
+ if can?(:view, asset.ticket.project)
+ send_file asset.asset.path, :filename => asset.asset_file_name,
+ :content_type => asset.asset_content_type
+ else
+ flash[:alert] = "The asset you were looking for could not be found."
+ redirect_to root_path
+ end
+ end
+
+ def new
+ @ticket = Ticket.new
+ asset = @ticket.assets.build
+ render :partial => "files/form",
+ :locals => { :number => params[:number].to_i,
+ :asset => asset }
+ end
+
+end
View
2  app/controllers/tickets_controller.rb
@@ -11,7 +11,7 @@ class TicketsController < ApplicationController
def new
@ticket = @project.tickets.build
- 3.times { @ticket.assets.build }
+ @ticket.assets.build
end
def create
View
2  app/helpers/files_helper.rb
@@ -0,0 +1,2 @@
+module FilesHelper
+end
View
8 app/views/files/_form.html.erb
@@ -0,0 +1,8 @@
+<%= fields_for @ticket do |f| %>
+ <%= f.fields_for :assets, :child_index => number do |asset| %>
+ <p>
+ <%= asset.label :asset, "File ##{number += 1}" %>
+ <%= asset.file_field :asset %>
+ </p>
+ <% end %>
+<% 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'>
+ <%= f.fields_for :assets, :child_index => number do |asset| %>
+ <p>
+ <%= asset.label :asset, "File ##{number += 1}" %>
+ <%= asset.file_field :asset %>
+ </p>
+ <% end %>
+ </div>
+ <%= link_to "Add another file", 'javascript:', :id => "add_another_file" %>
<%= f.submit %>
<% end %>
View
2  config/routes.rb
@@ -10,7 +10,7 @@
resources :tickets
end
- resources :assets
+ resources :files
namespace :admin do
root :to => "base#index"
View
5 features/creating_tickets.feature
@@ -35,14 +35,15 @@ Feature: Creating Tickets
Then I should see "Ticket has not been created."
And I should see "Description is too short"
+ @javascript
Scenario: Creating a ticket with an attachment
When I fill in "Title" with "Add documentation for blink tag"
And I fill in "Description" with "The blink tag has an undocumented speed attribute"
And I attach the file "spec/fixtures/speed.txt" to "File #1"
+ And I follow "Add another file"
And I attach the file "spec/fixtures/spin.txt" to "File #2"
- And I attach the file "spec/fixtures/gradient.txt" to "File #3"
And I press "Create Ticket"
Then I should see "Ticket has been created."
And I should see "speed.txt" within "#ticket .assets"
And I should see "spin.txt" within "#ticket .assets"
- And I should see "gradient.txt" within "#ticket .assets"
+ When I follow "speed.txt"
View
1  files/2
@@ -0,0 +1 @@
+Spinning blink tags have a 200% higher click rate!
View
1  files/3
@@ -0,0 +1 @@
+Spinning blink tags have a 200% higher click rate!
View
1  files/4
@@ -0,0 +1 @@
+Spinning blink tags have a 200% higher click rate!
View
1  files/5
@@ -0,0 +1 @@
+The blink tag can blink faster if you use the speed="hyper" attribute.
View
1  files/6
@@ -0,0 +1 @@
+Spinning blink tags have a 200% higher click rate!
View
42 spec/controllers/files_controller_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe FilesController do
+ let(:project) { Factory(:project) }
+ let(:ticket) { Factory(:ticket, :project => project) }
+ let(:good_user) { create_user! }
+ let(:bad_user) { create_user! }
+
+ let(:path) { Rails.root + "spec/fixtures/speed.txt" }
+ let(:asset) do
+ ticket.assets.create(:asset => File.open(path))
+ end
+
+ before do
+ good_user.permissions.create!(:action => "view", :thing => project)
+ end
+
+ context "users with access" do
+
+ before do
+ sign_in(:user, good_user)
+ end
+
+ it "can access assets in a project" do
+ get 'show', :id => asset.id
+ response.body.should eql(File.read(path))
+ end
+ end
+
+ context "users without access" do
+ before do
+ sign_in(:user, bad_user)
+ end
+
+ it "cannot access assets in this project" do
+ get 'show', :id => asset.id
+ response.should redirect_to(root_path)
+ flash[:alert].should eql("The asset you were looking for could not be found.")
+ end
+ end
+
+end
View
15 spec/helpers/files_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the AssetsHelper. For example:
+#
+# describe AssetsHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe FilesHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.