Skip to content

Commit

Permalink
Focused the quantity after adding an item
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Oct 19, 2008
1 parent da5e18d commit 89826cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions app/views/purchases/_form.html.erb
Expand Up @@ -23,4 +23,10 @@
</p>

<%= f.hidden_field :store_id, :value => @store && @store.id %>
<% if @store && request.xhr? -%>
<% javascript_tag do -%>
$('purchase_quantity').focus();
<% end -%>
<% end -%>
<% end -%>
29 changes: 23 additions & 6 deletions test/functional/purchases_controller_test.rb
Expand Up @@ -2,6 +2,12 @@

class PurchasesControllerTest < ActionController::TestCase

include ActionView::Helpers::JavaScriptHelper

def setup
@focus_quantity = /#{Regexp.escape("$('purchase_quantity').focus()")}/
end

should_route :get, '/purchases', :action => :index
should_route :post, '/purchases', :action => :create
should_route :delete, '/purchases/1', :action => :destroy, :id => '1'
Expand Down Expand Up @@ -85,6 +91,10 @@ class PurchasesControllerTest < ActionController::TestCase
should "fill in the name of the latest purchase's store" do
assert_select '#picked-store-name', @store.name
end

should "not focus the quantity" do
assert_no_match @focus_quantity, @response.body
end
end
end

Expand All @@ -100,14 +110,18 @@ class PurchasesControllerTest < ActionController::TestCase
should "have a placeholder for the store name" do
assert_select '#picked-store-name'
end

should "not focus the quantity" do
assert_no_match @focus_quantity, @response.body
end
end

context "on JS POST to create with valid params" do
setup do
@store = Factory(:store)
post :create,
:format => :js,
:purchase => Factory.attributes_for(:purchase,
xhr :post, :create,
:format => :js,
:purchase => Factory.attributes_for(:purchase,
:store_id => @store.id)
end

Expand All @@ -130,23 +144,26 @@ class PurchasesControllerTest < ActionController::TestCase
should "rerender the purchase form" do
assert_select_rjs :replace, 'new_purchase' do
assert_select '#purchase_store_id[value=?]', @store.id
assert_match @focus_quantity, @response.body
end
end
end

context "on JS POST to create with only a store ID" do
setup do
@store = Factory(:store)
post :create, :format => :js,
:purchase => { :store_id => @store.to_param }
xhr :post, :create, :format => :js,
:purchase => { :store_id => @store.to_param }
end

should_assign_to :purchase, :store
should_assign_to :new_purchase
should_not_change "@user.purchases.count"

should "rerender the purchase form" do
assert_select_rjs :replace, 'new_purchase'
assert_select_rjs :replace, 'new_purchase' do
assert_match @focus_quantity, @response.body
end
end

should "not create a new list element" do
Expand Down

0 comments on commit 89826cd

Please sign in to comment.