Skip to content

Commit

Permalink
Revised fix and Shoulda tests for http://github.com/thoughtbot/cleara…
Browse files Browse the repository at this point in the history
…nce/issues/78

(SessionsController::new should pass optional return_to parameter on to SessionsController::create)
this time not using hidden form fields.
  • Loading branch information
Ron Newman committed Feb 26, 2010
1 parent 269148f commit a4d19ec
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 29 deletions.
5 changes: 1 addition & 4 deletions app/views/sessions/new.html.erb
@@ -1,6 +1,6 @@
<h2>Sign in</h2>

<% form_for :session, :url => session_path do |form| %>
<% form_for :session, :url => session_path(:return_to => params[:return_to]) do |form| %>
<div class="text_field">
<%= form.label :email %>
<%= form.text_field :email %>
Expand All @@ -9,9 +9,6 @@
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<% unless params[:return_to].blank? -%>
<%= hidden_field_tag :return_to, params[:return_to] %>
<% end -%>
<div class="submit_field">
<%= form.submit "Sign in", :disable_with => "Please wait..." %>
</div>
Expand Down
@@ -1,13 +1,10 @@
<h2>Sign in</h2>

<% semantic_form_for :session, :url => session_path do |form| %>
<% semantic_form_for :session, :url => session_path(:return_to => params[:return_to]) do |form| %>
<% form.inputs do %>
<%= form.input :email %>
<%= form.input :password, :as => :password %>
<% end %>
<% unless params[:return_to].blank? -%>
<%= hidden_field_tag :return_to, params[:return_to] %>
<% end -%>
<% form.buttons do %>
<%= form.commit_button "Sign in" %>
<% end %>
Expand Down
21 changes: 5 additions & 16 deletions shoulda_macros/clearance.rb
Expand Up @@ -206,24 +206,13 @@ def should_display_a_sign_up_form
end
end

def should_contain_hidden_return_to_field(&block)
should "include hidden return_to field in login form" do
return_url = instance_eval(&block)
assert_select "input[type=hidden][name=return_to]", 1
assert_select "input[type=hidden][name=return_to][value=?]", return_url, 1
end
end

def should_not_contain_hidden_return_to_field(&block)
should " NOT include hidden return_to field in login form" do
assert_select "input[type=hidden][name=return_to]", false
end
end

def should_display_a_sign_in_form
def should_display_a_sign_in_form(&block) # block produces either a return_to value or nil
warn "[DEPRECATION] should_display_a_sign_in_form: not meant to be public, no longer used internally"
should 'display a "sign in" form' do
assert_select "form[action=#{session_path}][method=post]",
return_to = instance_eval(&block)
submit_url = session_path
submit_url << "?return_to=" << ERB::Util::url_encode(return_to) if return_to
assert_select "form[action=?][method=post]", submit_url,
true, "There must be a form to sign in" do
assert_select "input[type=text][name=?]",
"session[email]", true, "There must be an email field"
Expand Down
8 changes: 3 additions & 5 deletions test/controllers/sessions_controller_test.rb
Expand Up @@ -6,14 +6,13 @@ class SessionsControllerTest < ActionController::TestCase

should_filter_params :password

context "on GET to /sessions/new" do
context "on GET to /sessions/new without a request return url" do
setup { get :new }

should_respond_with :success
should_render_template :new
should_not_set_the_flash
should_display_a_sign_in_form
should_not_contain_hidden_return_to_field
should_display_a_sign_in_form {nil} # no return_url
end

context "on GET to /sessions/new with a request return url" do
Expand All @@ -25,8 +24,7 @@ class SessionsControllerTest < ActionController::TestCase
should_respond_with :success
should_render_template :new
should_not_set_the_flash
should_display_a_sign_in_form
should_contain_hidden_return_to_field {@return_url}
should_display_a_sign_in_form {@return_url}
end

context "on POST to #create with unconfirmed credentials" do
Expand Down

0 comments on commit a4d19ec

Please sign in to comment.