deface 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -0
- data/deface.gemspec +1 -1
- data/lib/deface/override.rb +47 -3
- data/lib/deface/template_helper.rb +3 -3
- data/spec/deface/override_spec.rb +32 -0
- data/spec/deface/template_spec.rb +18 -0
- metadata +4 -4
data/README.markdown
CHANGED
@@ -50,6 +50,13 @@ Optional
|
|
50
50
|
|
51
51
|
* <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.
|
52
52
|
|
53
|
+
* <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:
|
54
|
+
* <tt>:sequence => n</tt> - where n is a positive or negative integer (lower numbers get applied first, default 100).
|
55
|
+
* <tt>:sequence => {:before => "*override_name*"}</tt> - where "*override_name*" is the name of an override defined for the
|
56
|
+
same virutal_path, the current override will be appplied before
|
57
|
+
the named override passed.
|
58
|
+
* <tt>:sequence => {:after => "*override_name*")</tt> - the current override will be applied after the named override passed.
|
59
|
+
|
53
60
|
Examples
|
54
61
|
========
|
55
62
|
|
data/deface.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{deface}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.6"
|
9
9
|
|
10
10
|
s.authors = ["Brian Quinn"]
|
11
11
|
s.description = %q{Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view.}
|
data/lib/deface/override.rb
CHANGED
@@ -41,8 +41,15 @@ module Deface
|
|
41
41
|
# If supplied Deface will log when the original markup changes, which helps highlight overrides that need
|
42
42
|
# attention when upgrading versions of the source application. Only really warranted for :replace overrides.
|
43
43
|
# NB: All whitespace is stripped before comparsion.
|
44
|
-
|
45
|
-
|
44
|
+
# * <tt>:sequence</tt> - Used to order the application of an override for a specific virtual path, helpful when
|
45
|
+
# an override depends on another override being applied first.
|
46
|
+
# Supports:
|
47
|
+
# :sequence => n - where n is a positive or negative integer (lower numbers get applied first, default 100).
|
48
|
+
# :sequence => {:before => "override_name"} - where "override_name" is the name of an override defined for the
|
49
|
+
# same virutal_path, the current override will be appplied before
|
50
|
+
# the named override passed.
|
51
|
+
# :sequence => {:after => "override_name") - the current override will be applied after the named override passed.
|
52
|
+
#
|
46
53
|
def initialize(args)
|
47
54
|
@args = args
|
48
55
|
|
@@ -63,6 +70,43 @@ module Deface
|
|
63
70
|
@args[:name]
|
64
71
|
end
|
65
72
|
|
73
|
+
def sequence
|
74
|
+
return 100 unless @args.key?(:sequence)
|
75
|
+
if @args[:sequence].is_a? Hash
|
76
|
+
key = @args[:virtual_path].to_sym
|
77
|
+
|
78
|
+
if @args[:sequence].key? :before
|
79
|
+
ref_name = @args[:sequence][:before]
|
80
|
+
|
81
|
+
if @@all[key].key? ref_name.to_s
|
82
|
+
return @@all[key][ref_name.to_s].sequence - 1
|
83
|
+
else
|
84
|
+
return 100
|
85
|
+
end
|
86
|
+
elsif @args[:sequence].key? :after
|
87
|
+
ref_name = @args[:sequence][:after]
|
88
|
+
|
89
|
+
if @@all[key].key? ref_name.to_s
|
90
|
+
return @@all[key][ref_name.to_s].sequence + 1
|
91
|
+
else
|
92
|
+
return 100
|
93
|
+
end
|
94
|
+
else
|
95
|
+
#should never happen.. tut tut!
|
96
|
+
return 100
|
97
|
+
end
|
98
|
+
|
99
|
+
else
|
100
|
+
return @args[:sequence].to_i
|
101
|
+
end
|
102
|
+
rescue SystemStackError
|
103
|
+
if defined?(Rails)
|
104
|
+
Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Circular sequence dependency includes override named: '#{self.name}' on '#{@args[:virtual_path]}'."
|
105
|
+
end
|
106
|
+
|
107
|
+
return 100
|
108
|
+
end
|
109
|
+
|
66
110
|
def action
|
67
111
|
(@@actions & @args.keys).first
|
68
112
|
end
|
@@ -200,7 +244,7 @@ module Deface
|
|
200
244
|
result = []
|
201
245
|
result << @@all[virtual_path.to_sym].try(:values)
|
202
246
|
|
203
|
-
result.flatten.compact
|
247
|
+
result.flatten.compact.sort_by &:sequence
|
204
248
|
end
|
205
249
|
|
206
250
|
private
|
@@ -4,12 +4,12 @@ module Deface
|
|
4
4
|
# used to find source for a partial or template using virutal_path
|
5
5
|
def load_template_source(virtual_path, partial)
|
6
6
|
parts = virtual_path.split("/")
|
7
|
-
|
7
|
+
prefix = []
|
8
8
|
if parts.size == 2
|
9
|
-
prefix
|
9
|
+
prefix << ""
|
10
10
|
name = virtual_path
|
11
11
|
else
|
12
|
-
prefix
|
12
|
+
prefix << parts.shift
|
13
13
|
name = parts.join("/")
|
14
14
|
end
|
15
15
|
|
@@ -140,6 +140,38 @@ module Deface
|
|
140
140
|
|
141
141
|
end
|
142
142
|
|
143
|
+
describe "#sequence" do
|
144
|
+
it "should calculate correct after sequences" do
|
145
|
+
@third = Deface::Override.new(:virtual_path => "posts/index", :name => "third", :insert_after => "li:contains('second')", :text => "<li>third</li>", :sequence => {:after => "second"})
|
146
|
+
@second = Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "<li>second</li>", :sequence => {:after => "first"})
|
147
|
+
@first = Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "<li>first</li>")
|
148
|
+
|
149
|
+
@third.sequence.should == 102
|
150
|
+
@second.sequence.should == 101
|
151
|
+
@first.sequence.should == 100
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should calculate correct before sequences" do
|
155
|
+
@second = Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "<li>second</li>", :sequence => 99)
|
156
|
+
@first = Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "<li>first</li>", :sequence => {:before => "second"})
|
157
|
+
|
158
|
+
@second.sequence.should == 99
|
159
|
+
@first.sequence.should == 98
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
it "should calculate correct sequences with invalid hash" do
|
165
|
+
@second = Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "<li>second</li>", :sequence => {})
|
166
|
+
@first = Deface::Override.new(:virtual_path => "posts/show", :name => "first", :replace => "li", :text => "<li>first</li>", :sequence => {:before => "second"})
|
167
|
+
|
168
|
+
@second.sequence.should == 100
|
169
|
+
@first.sequence.should == 100
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
143
175
|
end
|
144
176
|
|
145
177
|
end
|
@@ -125,5 +125,23 @@ module ActionView
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
128
|
+
|
129
|
+
describe "with mulitple sequenced overrides defined" do
|
130
|
+
before(:all) do
|
131
|
+
Deface::Override.new(:virtual_path => "posts/index", :name => "third", :insert_after => "li:contains('second')", :text => "<li>third</li>", :sequence => {:after => "second"})
|
132
|
+
Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "<li>second</li>", :sequence => {:after => "first"})
|
133
|
+
Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "<li>first</li>")
|
134
|
+
|
135
|
+
@template = ActionView::Template.new("<ul><li>replaced</li></ul>",
|
136
|
+
"/path/to/file.erb",
|
137
|
+
ActionView::Template::Handlers::ERB,
|
138
|
+
{:virtual_path=>"posts/index", :format=>:html})
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should return modified source" do
|
142
|
+
@template.source.gsub("\n", "").should == "<ul><li>first</li><li>second</li><li>third</li></ul>"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
128
146
|
end
|
129
147
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 6
|
10
|
+
version: 0.5.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Quinn
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-04 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|