Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

merged upstream commits #1

Merged
merged 4 commits into from

3 participants

Commits on Oct 1, 2011
  1. @BDQ
  2. @BDQ
Commits on Oct 5, 2011
  1. @BDQ

    Bumped version

    BDQ authored
Commits on Oct 11, 2011
  1. @milep
This page is out of date. Refresh to see the latest.
View
14 README.markdown
@@ -7,7 +7,7 @@
Deface
======
-Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view.
+Deface is a library that allows you to customize HTML ERB views in a Rails application without editing the underlying view.
It allows you to easily target html & erb elements as the hooks for customization using CSS selectors as supported by Nokogiri.
@@ -56,7 +56,9 @@ Optional
* <tt>:disabled</tt> - When set to true the override will not be applied.
-* <tt>:original</tt> - String containing original markup that is being overridden. If supplied Deface will log when the original markup changes, which helps highlight overrides that need attention when upgrading versions of the source application. Only really warranted for :replace overrides. NB: All whitespace is stripped before comparsion.
+* <tt>:original</tt> - String containing original markup that is being overridden. If supplied Deface will log when the original markup changes, which helps highlight overrides that need attention when upgrading versions of the source application. Only really warranted for :replace overrides. NB: All whitespace is stripped before comparison.
+
+* <tt>:closing_selector</tt> - A second css selector targeting an end element, allowing you to select a range of elements to apply an action against. The :closing_selector only supports the :replace, :remove and :replace_contents actions, and the end element must be a sibling of the first/starting element. Note the CSS general sibling selector (~) is used to match the first element after the opening selector (see below for an example).
* <tt>:sequence</tt> - Used to order the application of an override for a specific virtual path, helpful when an override depends on another override being applied first, supports:
* <tt>:sequence => n</tt> - where n is a positive or negative integer (lower numbers get applied first, default 100).
@@ -91,7 +93,7 @@ Inserts the contents of `shared/_comment.html.erb` after all instances of `div`
:insert_after => "div#comment_21",
:partial => "shared/comment")
-Removes any ERB block containing the string `helper_method` in the `posts/new.html.erb` template, will also log if markup being removed does not match `<%= helper_method %>`
+Removes any ERB block containing the string `helper_method` in the `posts/new.html.erb` template, will also log if markup being removed does not exactly match `<%= helper_method %>`
Deface::Override.new(:virtual_path => "posts/new",
:name => "example-4",
@@ -105,6 +107,12 @@ Sets (or adds if not present) the `class` and `title` attributes to all instance
:set_attributes => 'a#link',
:attributes => {:class => 'pretty', :title => 'This is a link'})
+Remove an entire ERB if statement (and all it's contents) in the 'admin/products/index.html.erb' template, using the :closing_selector.
+
+ Deface::Override.new(:virtual_path => 'admin/products/index',
+ :name => "remove_if_statement",
+ :remove => "code[:erb-silent]:contains('if @product.sold?')",
+ :closing_selector => "code[:erb-silent]:contains('end')"
Scope
=====
View
2  deface.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "deface"
- s.version = "0.6.1"
+ s.version = "0.7.0"
s.authors = ["Brian D Quinn"]
s.description = "Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view."
View
8 lib/deface/override.rb
@@ -46,6 +46,10 @@ class Override
# If supplied Deface will log when the original markup changes, which helps highlight overrides that need
# attention when upgrading versions of the source application. Only really warranted for :replace overrides.
# NB: All whitespace is stripped before comparsion.
+ # * <tt>:closing_selector</tt> - A second css selector targeting an end element, allowing you to select a range
+ # of elements to apply an action against. The :closing_selector only supports the :replace, :remove and
+ # :replace_contents actions, and the end element must be a sibling of the first/starting element. Note the CSS
+ # general sibling selector (~) is used to match the first element after the opening selector.
# * <tt>:sequence</tt> - Used to order the application of an override for a specific virtual path, helpful when
# an override depends on another override being applied first.
# Supports:
@@ -183,7 +187,8 @@ def disabled?
end
def end_selector
- @args[:closing_selector]
+ return nil if @args[:closing_selector].blank?
+ "#{self.selector} ~ #{@args[:closing_selector]}"
end
def attributes
@@ -271,6 +276,7 @@ def self.apply(source, details, log=true)
else
# targeting range of elements as end_selector is present
starting = doc.css(override.selector).first
+
if starting && starting.parent
ending = starting.parent.css(override.end_selector).first
else
View
22 spec/deface/override_spec.rb
@@ -214,6 +214,28 @@ module Deface
end
+
+ describe "#end_selector" do
+ it "should return nil when closing_selector is not defined" do
+ @override.end_selector.should be_nil
+ end
+
+ it "should return nil when closing_selector is an empty string" do
+ @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :closing_selector => "", :text => "<h1>Argh!</h1>")
+ @override.end_selector.should be_nil
+ end
+
+ it "should return nil when closing_selector is nil" do
+ @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :closing_selector => nil, :text => "<h1>Argh!</h1>")
+ @override.end_selector.should be_nil
+ end
+
+ it "should return combined sibling selector when closing_selector is present" do
+ @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :closing_selector => "h4", :text => "<h1>Argh!</h1>")
+ @override.end_selector.should == "h1 ~ h4"
+ end
+ end
+
end
end
View
4 spec/deface/template_spec.rb
@@ -51,11 +51,11 @@ module ActionView
describe "with a single remove override with closing_selector defined" do
before(:each) do
Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "h1", :closing_selector => "h2")
- @template = ActionView::Template.new("<span>Before!</span><h1>start</h1><p>some junk</p><div>more junk</div><h2>end</h2><span>After!</span>", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html})
+ @template = ActionView::Template.new("<h2>I should be safe</h2><span>Before!</span><h1>start</h1><p>some junk</p><div>more junk</div><h2>end</h2><span>After!</span>", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html})
end
it "should return modified source" do
- @template.source.should == "<span>Before!</span><span>After!</span>"
+ @template.source.should == "<h2>I should be safe</h2><span>Before!</span><span>After!</span>"
end
end
Something went wrong with that request. Please try again.