From 44db244a7615f8a7f29d6f76d399e9505c6f6cdd Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 14 Sep 2008 14:37:24 -0400 Subject: [PATCH] Cleaning up the Rails session code. Extract scoped_html method in Scope --- lib/webrat/core/scope.rb | 20 +++++++++++--------- lib/webrat/core/session.rb | 6 +++++- lib/webrat/rails/session.rb | 10 +--------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 445c1e9..42df3d7 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -165,19 +165,21 @@ module Webrat alias_method :click_button, :clicks_button def dom # :nodoc: - return @dom if defined?(@dom) && @dom - @dom = Hpricot(@html) - - if @selector - html = (@dom / @selector).first.to_html - @dom = Hpricot(html) - end - - return @dom + @dom ||= Hpricot(scoped_html) end protected + def scoped_html + @scoped_html ||= begin + if @selector + (Hpricot(@html) / @selector).first.to_html + else + @html + end + end + end + def find_select_option(option_text, id_or_name_or_label) if id_or_name_or_label field = find_field(id_or_name_or_label, SelectField) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index ecf243c..7e9d9c6 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -109,6 +109,11 @@ module Webrat yield Scope.new(self, response_body, selector) end + # Issues a GET request for a page, follows any redirects, and verifies the final page + # load was successful. + # + # Example: + # visits "/" def visits(url = nil, http_method = :get, data = {}) request_page(url, http_method, data) end @@ -136,6 +141,5 @@ module Webrat def_delegators :current_scope, :click_post_link, :clicks_post_link def_delegators :current_scope, :click_put_link, :clicks_put_link def_delegators :current_scope, :click_button, :clicks_button - end end \ No newline at end of file diff --git a/lib/webrat/rails/session.rb b/lib/webrat/rails/session.rb index 01a186f..de89cfa 100644 --- a/lib/webrat/rails/session.rb +++ b/lib/webrat/rails/session.rb @@ -5,15 +5,6 @@ module ActionController unless instance_methods.include?("put_via_redirect") include Webrat::RedirectActions end - - # Issues a GET request for a page, follows any redirects, and verifies the final page - # load was successful. - # - # Example: - # visits "/" - def visits(*args) - webrat_session.visits(*args) - end def respond_to?(name) super || webrat_session.respond_to?(name) @@ -32,6 +23,7 @@ module ActionController def webrat_session @webrat_session ||= Webrat::RailsSession.new(self) end + end end end \ No newline at end of file