Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow configuration of custom wrapper IDs in middleware.

To do this, we needed to make the App configuration available within the Skin,
so we've refactored the arguments passed to be a Hash rather than the list of
parameters.

We also changed the arguments of the BodyInserter to be IDs rather than
selectors; they are converted to selectors internally. We did this because it
seems more natural to configure using IDs than CSS selector paths.
  • Loading branch information...
commit 2a7dae70944a855aa86d1101e088e3e5a00604db 1 parent a901a0e
@lazyatom lazyatom authored
View
10 lib/slimmer/body_inserter.rb
@@ -1,13 +1,13 @@
module Slimmer
class BodyInserter
- def initialize(source_path='#wrapper', destination_path='#wrapper')
- @source_path = source_path
- @destination_path = destination_path
+ def initialize(source_id='wrapper', destination_id='wrapper')
+ @source_selector = '#' + source_id
+ @destination_selector = '#' + destination_id
end
def filter(src,dest)
- body = Nokogiri::HTML.fragment(src.at_css(@source_path).to_html)
- dest.at_css(@destination_path).replace(body)
+ body = Nokogiri::HTML.fragment(src.at_css(@source_selector).to_html)
+ dest.at_css(@destination_selector).replace(body)
end
end
end
View
6 lib/slimmer/skin.rb
@@ -18,9 +18,13 @@ class Skin
attr_accessor :strict
private :strict=, :strict
+ attr_accessor :options
+ private :options=, :options
+
# TODO: Extract the cache to something we can pass in instead of using
# true/false and an in-memory cache.
def initialize options = {}
+ self.options = options
self.asset_host = options[:asset_host]
self.templated_cache = {}
self.prefix = options[:prefix]
@@ -168,7 +172,7 @@ def success(source_request, request, body)
processors = [
TitleInserter.new(),
TagMover.new(),
- BodyInserter.new(),
+ BodyInserter.new(options[:wrapper_id] || 'wrapper'),
BodyClassCopier.new,
HeaderContextInserter.new(),
SectionInserter.new(),
View
2  test/processors/body_inserter_test.rb
@@ -36,7 +36,7 @@ def test_should_allow_replacement_of_arbitrary_segments_into_wrapper
<html><body><div id="some_other_id"><p>this should be moved</p></div></body></html>
}
- Slimmer::BodyInserter.new("#some_other_id").filter(source, template)
+ Slimmer::BodyInserter.new("some_other_id").filter(source, template)
assert_not_in template, "#wrapper"
assert_in template, "#some_other_id", %{<p>this should be moved</p>}
end
View
9 test/test_helper.rb
@@ -27,12 +27,12 @@ def assert_not_in(template, selector, message="didn't exist to find #{selector}"
class SlimmerIntegrationTest < MiniTest::Unit::TestCase
include Rack::Test::Methods
- def self.given_response(code, body, headers={})
+ def self.given_response(code, body, headers={}, app_options={})
define_method(:app) do
inner_app = proc { |env|
[code, headers.merge("Content-Type" => "text/html"), body]
}
- Slimmer::App.new inner_app, :asset_host => "http://template.local"
+ Slimmer::App.new inner_app, {asset_host: "http://template.local"}.merge(app_options)
end
define_method :teardown do
@@ -91,4 +91,9 @@ def assert_rendered_in_template(selector, content=nil, message=nil)
end
end
end
+
+ def assert_no_selector(selector, message=nil)
+ message ||= "Expected not to find #{selector.inspect}, but did"
+ assert_nil Nokogiri::HTML.parse(last_response.body).at_css(selector), message
+ end
end
View
15 test/typical_usage_test.rb
@@ -308,6 +308,21 @@ def setup
def test_should_return_503_if_an_API_call_times_out
assert_equal 503, last_response.status
end
+ end
+
+ class ArbitraryWrapperIdTest < SlimmerIntegrationTest
+
+ given_response 200, %{
+ <html>
+ <body>
+ <div id="custom_wrapper">The body of the page</div>
+ </body>
+ </html>
+ }, {}, {wrapper_id: "custom_wrapper"}
+ def test_should_replace_wrapper_with_custom_wrapper
+ assert_rendered_in_template "body .content #custom_wrapper", "The body of the page"
+ assert_no_selector "#wrapper"
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.