public
Fork of halorgium/mephisto
Description: A refactored Mephisto that has multiple spam detection engines.
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/francois/mephisto.git
allow the various link_to filters to accept multiple args to specify more 
html attributes [rob-twf]

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@3060 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Fri Nov 23 21:19:49 -0800 2007
commit  12a136dd7837ef527727fa7d7ea7e76bb97be81b
tree    ea55ccc308e8ad3fc66aee159aa03e92501ccdad
parent  ec43d7432a4cd6d27765bced40067602a8df814a
...
1
2
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,5 +1,10 @@
0
 * SVN *
0
 
0
+* allow the various link_to filters to accept multiple args to specify more html attributes [rob-twf]
0
+
0
+ # use this order: :text, :title, :id, :class, :rel
0
+ link_to_article article, "click here", "this article", "article-1", "articles", "whatever-rel-means"
0
+
0
 * encode search/tag urls properly [rob-twf]
0
 
0
 * update article forms to explain new tag syntax (comma and space delimited tags) [xavier, rick]
...
4
5
6
7
8
 
 
 
 
9
10
11
12
 
 
 
 
13
14
15
16
 
 
 
 
17
18
19
20
 
 
 
 
21
22
23
...
25
26
27
28
 
29
30
 
31
32
 
33
34
35
...
63
64
65
66
67
 
 
 
 
68
69
70
71
 
 
 
 
72
73
74
...
128
129
130
131
132
133
 
 
 
 
134
135
136
137
138
139
 
 
 
 
 
 
 
 
 
 
140
141
142
...
146
147
148
149
150
 
...
4
5
6
 
 
7
8
9
10
11
12
 
 
13
14
15
16
17
18
 
 
19
20
21
22
23
24
 
 
25
26
27
28
29
30
31
...
33
34
35
 
36
37
 
38
39
 
40
41
42
43
...
71
72
73
 
 
74
75
76
77
78
79
 
 
80
81
82
83
84
85
86
...
140
141
142
 
 
 
143
144
145
146
147
148
149
 
 
 
150
151
152
153
154
155
156
157
158
159
160
161
162
...
166
167
168
 
169
170
0
@@ -4,20 +4,28 @@ module UrlFilters
0
   include ActionView::Helpers::TagHelper
0
   include ActionView::Helpers::AssetTagHelper
0
 
0
- def link_to_article(article, text = nil)
0
- content_tag :a, text || h(article['title']), :href => article['url']
0
+ def link_to_article(article, *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || h(article['title'])
0
+ content_tag :a, text, { :href => article['url'], :title => text }.merge(options)
0
   end
0
   
0
- def link_to_page(page, section = nil, text = nil)
0
- content_tag :a, text || h(page['title']), page_anchor_options(page, section)
0
+ def link_to_page(page, section = nil, *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || h(page['title'])
0
+ content_tag :a, text, page_anchor_options(page, section, { :title => text }.merge(options))
0
   end
0
 
0
- def link_to_comments(article, text = nil)
0
- content_tag :a, text || pluralize(article['comments_count'], 'comment'), :href => article['url']
0
+ def link_to_comments(article, *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || pluralize(article['comments_count'], 'comment')
0
+ content_tag :a, text, { :href => article['url'] + '#comments', :title => text }.merge(options)
0
   end
0
   
0
- def link_to_section(section, text = nil)
0
- content_tag :a, text || h(section['name']), section_anchor_options(section)
0
+ def link_to_section(section, *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || h(section['name'])
0
+ content_tag :a, text, section_anchor_options(section, { :title => text }.merge(options))
0
   end
0
 
0
   def img_tag(img, options = {})
0
@@ -25,11 +33,11 @@ module UrlFilters
0
   end
0
   
0
   # Special link that checks for current section. If it exists and it's a paged section, use link_to_page instead.
0
- def link_to_search_result(article, text = nil)
0
+ def link_to_search_result(article, *args)
0
     if current_page_section && current_page_section[:is_paged]
0
- link_to_page(article, current_page_section, text)
0
+ link_to_page(article, current_page_section, *args)
0
     else
0
- link_to_article(article, text)
0
+ link_to_article(article, *args)
0
     end
0
   end
0
   
0
@@ -63,12 +71,16 @@ module UrlFilters
0
     image_tag url, :class => 'gravatar', :size => "#{size}x#{size}", :alt => comment['author']
0
   end
0
 
0
- def link_to_tag(tag)
0
- content_tag :a, h(tag), :href => tag_url(tag), :rel => 'tag'
0
+ def link_to_tag(tag, *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || h(tag)
0
+ content_tag :a, text, { :href => tag_url(tag), :rel => 'tag', :title => text }.merge(options)
0
   end
0
 
0
- def link_to_month(section, date = nil, format = 'my')
0
- content_tag :a, format_date(date, format), :href => monthly_url(section, date)
0
+ def link_to_month(section, date = nil, format = 'my', *args)
0
+ options = link_args_to_options(args)
0
+ text = options.delete(:text) || h(format_date(date, format))
0
+ content_tag :a, text, { :href => monthly_url(section, date), :title => text }.merge(options)
0
   end
0
 
0
   def monthly_url(section, date = nil)
0
@@ -128,15 +140,23 @@ module UrlFilters
0
 
0
   private
0
     # marks a page as class=selected
0
- def page_anchor_options(page, section = nil)
0
- options = {:href => page_url(page, section)}
0
- current_page_article == page ? options.update(:class => 'selected') : options
0
+ def page_anchor_options(page, section = nil, options = {})
0
+ options.update(:href => page_url(page, section))
0
+ options[:class].nil? ? options.update(:class => 'selected') : options[:class] += ' selected' if current_page_article == page
0
+ options
0
     end
0
     
0
     # marks a section as class=selected
0
- def section_anchor_options(section)
0
- options = {:href => section['url'], :title => section['title']}
0
- (current_page_section && (current_page_section.url == section.url)) ? options.update(:class => 'selected') : options
0
+ def section_anchor_options(section, options = {})
0
+ options.update(:href => section['url'])
0
+ options[:class].nil? ? options.update(:class => 'selected') : options[:class] += ' selected' if current_page_section && (current_page_section.url == section.url)
0
+ options
0
+ end
0
+
0
+ def link_args_to_options(args)
0
+ options = {}
0
+ [:text, :title, :id, :class, :rel].zip(args) {|key, value| options[key] = h(value) unless value.blank?}
0
+ options
0
     end
0
     
0
     def current_page_section
0
@@ -146,4 +166,4 @@ module UrlFilters
0
     def current_page_article
0
       @current_page_article ||= @context['article']
0
     end
0
-end
0
\ No newline at end of file
0
+end
...
28
29
30
31
 
32
33
34
...
52
53
54
55
56
57
58
 
 
 
 
 
 
59
60
61
...
96
97
98
99
 
100
101
102
...
152
153
154
155
 
156
157
158
...
182
183
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
186
187
...
28
29
30
 
31
32
33
34
...
52
53
54
 
 
 
 
55
56
57
58
59
60
61
62
63
...
98
99
100
 
101
102
103
104
...
154
155
156
 
157
158
159
160
...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
0
@@ -28,7 +28,7 @@ context "Url Filters" do
0
   end
0
 
0
   specify "should generate monthly link" do
0
- assert_equal "<a href=\"/archives/2006/1\">January 2006</a>", link_to_month(sections(:home).to_liquid, '2006-1')
0
+ assert_equal "<a href=\"/archives/2006/1\" title=\"January 2006\">January 2006</a>", link_to_month(sections(:home).to_liquid, '2006-1')
0
   end
0
 
0
   specify "should generate paged url" do
0
@@ -52,10 +52,12 @@ context "Url Filters" do
0
     other_section = link_to_section(sections(:home).to_liquid)
0
     home_section = link_to_section(sections(:about).to_liquid)
0
     
0
- assert_match %r(href="/"), other_section
0
- assert_match %r(href="/about"), home_section
0
- assert_match %r(class="selected"), home_section
0
- assert_no_match %r(class="selected"), other_section
0
+ assert_match %r(href="/"), other_section
0
+ assert_match %r(href="/about"), home_section
0
+ assert_match %r(class="selected"), home_section
0
+ assert_no_match %r(class="selected"), other_section
0
+ assert_match %r(title="#{sections(:home).name}"), other_section
0
+ assert_match %r(title="#{sections(:about).name}"), home_section
0
   end
0
 
0
   specify "should generate paged url for home section" do
0
@@ -96,7 +98,7 @@ context "Url Filters" do
0
   end
0
   
0
   specify "should generate tag links" do
0
- assert_equal "<a href=\"/tags/foo\" rel=\"tag\">foo</a>", link_to_tag('foo')
0
+ assert_equal "<a href=\"/tags/foo\" rel=\"tag\" title=\"foo\">foo</a>", link_to_tag('foo')
0
   end
0
   
0
   specify "should generate search urls" do
0
@@ -152,7 +154,7 @@ context "Url Filters" do
0
     assert_match /href="\/feed\/about\/atom.xml"/, content
0
     assert_match /title="About Articles"/, content
0
   end
0
-
0
+
0
   specify "should html encode anchor text" do
0
     unencoded = 'Tom & Jerry'
0
     contents(:welcome).title = unencoded
0
@@ -182,6 +184,124 @@ context "Url Filters" do
0
   end
0
 end
0
 
0
+context "Link_to Url Filters" do
0
+ fixtures :sites, :sections, :contents
0
+ include CoreFilters, UrlFilters
0
+
0
+ def setup
0
+ @context = mock_context 'site' => sites(:first).to_liquid
0
+ @section = sections(:about).to_liquid
0
+ @article = contents(:welcome).to_liquid
0
+ @paged_article = contents(:about).to_liquid
0
+ @article.context = @paged_article.context = @context
0
+ end
0
+
0
+ specify "should generate links with custom text" do
0
+ pattern = %r(^<a href="[^"]+" (?:rel="tag" )?title="Custom text">Custom text</a>$)
0
+ args = 'Custom text'
0
+ assert_match pattern, link_to_article(@article, args)
0
+ assert_match pattern, link_to_page(@article, @section, args)
0
+ assert_match pattern, link_to_section(@section, args)
0
+ assert_match pattern, link_to_comments(@article, args)
0
+ assert_match pattern, link_to_tag('foo', args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', args)
0
+ assert_match pattern, link_to_search_result(@article, args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, args)
0
+ end
0
+
0
+ specify "should generate links with custom title attribute" do
0
+ pattern = %r(^<a href="[^"]+" (?:rel="tag" )?title="Custom title">)
0
+ args = [nil, 'Custom title']
0
+ assert_match pattern, link_to_article(@article, *args)
0
+ assert_match pattern, link_to_page(@article, @section, *args)
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_match pattern, link_to_comments(@article, *args)
0
+ assert_match pattern, link_to_tag('foo', *args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', *args)
0
+ assert_match pattern, link_to_search_result(@article, *args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, *args)
0
+ end
0
+
0
+ specify "should generate links with custom id attribute" do
0
+ pattern = %r(^<a href="[^"]+" id="custom-id" (?:rel="tag" )?title="[^"]+">)
0
+ args = [nil, nil, 'custom-id']
0
+ assert_match pattern, link_to_article(@article, *args)
0
+ assert_match pattern, link_to_page(@article, @section, *args)
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_match pattern, link_to_comments(@article, *args)
0
+ assert_match pattern, link_to_tag('foo', *args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', *args)
0
+ assert_match pattern, link_to_search_result(@article, *args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, *args)
0
+ end
0
+
0
+ specify "should generate links with custom class attribute" do
0
+ pattern = %r(^<a class="custom-class" href="[^"]+" (?:rel="tag" )?title="[^"]+">)
0
+ args = [nil, nil, nil, 'custom-class']
0
+ assert_match pattern, link_to_article(@article, *args)
0
+ assert_match pattern, link_to_page(@article, @section, *args)
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_match pattern, link_to_comments(@article, *args)
0
+ assert_match pattern, link_to_tag('foo', *args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', *args)
0
+ assert_match pattern, link_to_search_result(@article, *args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, *args)
0
+ end
0
+
0
+ specify "should generate links with custom rel attribute" do
0
+ pattern = %r(^<a href="[^"]+" rel="custom-rel" title="[^"]+">)
0
+ args = [nil, nil, nil, nil, 'custom-rel']
0
+ assert_match pattern, link_to_article(@article, *args)
0
+ assert_match pattern, link_to_page(@article, @section, *args)
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_match pattern, link_to_comments(@article, *args)
0
+ assert_match pattern, link_to_tag('foo', *args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', *args)
0
+ assert_match pattern, link_to_search_result(@article, *args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, *args)
0
+ end
0
+
0
+ specify "should html encode custom attributes" do
0
+ pattern = %r(^<a class="custom&amp;class" href="[^"]+" id="custom&amp;id" rel="custom&amp;rel" title="Custom &amp; title">Custom &amp; text</a>$)
0
+ args = ['Custom & text', 'Custom & title', 'custom&id', 'custom&class', 'custom&rel']
0
+ assert_match pattern, link_to_article(@article, *args)
0
+ assert_match pattern, link_to_page(@article, @section, *args)
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_match pattern, link_to_comments(@article, *args)
0
+ assert_match pattern, link_to_tag('foo', *args)
0
+ assert_match pattern, link_to_month(@section, '2006-1', 'my', *args)
0
+ assert_match pattern, link_to_search_result(@article, *args)
0
+ @context['section'] = @section
0
+ assert_match pattern, link_to_search_result(@paged_article, *args)
0
+ end
0
+
0
+ specify "should generate page links with selected class appended to custom class attribute" do
0
+ pattern = %r(class="custom-class selected")
0
+ args = [nil, nil, nil, 'custom-class']
0
+
0
+ @context['section'] = @section
0
+ @context['article'] = @paged_article
0
+
0
+ assert_match pattern, link_to_page(@paged_article, @section, *args)
0
+ assert_no_match pattern, link_to_page(@article, @section, *args)
0
+ end
0
+
0
+ specify "should generate section links with selected class appended to custom class attribute" do
0
+ pattern = %r(class="custom-class selected")
0
+ args = [nil, nil, nil, 'custom-class']
0
+
0
+ @context['section'] = @section
0
+
0
+ assert_match pattern, link_to_section(@section, *args)
0
+ assert_no_match pattern, link_to_section(sections(:home).to_liquid, *args)
0
+ end
0
+end
0
+
0
 context "Article Url Filters" do
0
   fixtures :sites, :sections, :contents
0
   include CoreFilters, UrlFilters

Comments

    No one has commented yet.