Skip to content

Commit

Permalink
Fix support for passing in an ActiveRecord model to within (which use…
Browse files Browse the repository at this point in the history
…s dom_id) LB/BH
  • Loading branch information
brynary committed Sep 18, 2009
1 parent 8624714 commit b51ba02
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 39 deletions.
15 changes: 0 additions & 15 deletions lib/webrat/adapters/rails.rb
Expand Up @@ -11,21 +11,6 @@ def initialize(session)
@integration_session = session
end

# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
# Example:
# within User.last do
# click_link "Delete"
# end
def within(selector_or_object, &block)
if selector_or_object.is_a?(String)
super
else
super('#' + dom_id(selector_or_object), &block)
end
end

def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
Expand Down
16 changes: 16 additions & 0 deletions lib/webrat/integrations/rails.rb
Expand Up @@ -5,5 +5,21 @@ module ActionController #:nodoc:
IntegrationTest.class_eval do
include Webrat::Methods
include Webrat::Matchers

# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
# Example:
# within User.last do
# click_link "Delete"
# end
def within(selector_or_object, &block)
if selector_or_object.is_a?(String)
super
else
super('#' + RecordIdentifier.dom_id(selector_or_object), &block)
end
end

end
end
3 changes: 3 additions & 0 deletions spec/integration/rails/app/controllers/webrat_controller.rb
Expand Up @@ -40,4 +40,7 @@ def show_params
render :text => params.to_json
end

def within
end

end
3 changes: 3 additions & 0 deletions spec/integration/rails/app/views/webrat/within.html.erb
@@ -0,0 +1,3 @@
<div id="new_object">
<a href="/">Edit Object</a>
</div>
1 change: 1 addition & 0 deletions spec/integration/rails/config/routes.rb
Expand Up @@ -12,6 +12,7 @@
webrat.before_redirect_form "/before_redirect_form", :action => "before_redirect_form"
webrat.redirect_to_show_params "/redirect_to_show_params", :action => "redirect_to_show_params"
webrat.show_params "/show_params", :action => "show_params"
webrat.within "/within", :action => "within"

webrat.root :action => "form"
end
Expand Down
17 changes: 17 additions & 0 deletions spec/integration/rails/test/integration/webrat_test.rb
Expand Up @@ -89,6 +89,23 @@ class WebratTest < ActionController::IntegrationTest
assert_have_selector "h1"
end

test "should accept an Object argument to #within and translate using dom_id" do
webrat.simulate do
visit within_path

object = Object.new
def object.id
nil
end

within(object) do
click_link "Edit Object"
end

assert_contain "Webrat Form"
end
end

# Firefox detects and prevents infinite redirects under Selenium
unless ENV['WEBRAT_INTEGRATION_MODE'] == 'selenium'
test "should detect infinite redirects" do
Expand Down
24 changes: 0 additions & 24 deletions spec/private/rails/rails_adapter_spec.rb
Expand Up @@ -83,28 +83,4 @@
it "should provide a doc_root" do
Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root)
end

it "should accept an ActiveRecord argument to #within and translate to a selector using dom_id" do
pending "Move this to spec/public/within_spec.rb"

body = <<-HTML
<a href="/page1">Edit</a>
<div id="new_object">
<a href="/page2">Edit</a>
</div>
HTML

response = mock("response", :body => body, :headers => {}, :code => 200)
@integration_session.stub!(:response => response)
@integration_session.should_receive(:get).with("/page2", {}, nil)

rails_session = Webrat::RailsAdapter.new(@integration_session)

object = Object.new
object.stub!(:id => nil)

rails_session.within(object) do
rails_session.click_link 'Edit'
end
end
end

0 comments on commit b51ba02

Please sign in to comment.