public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
update permalink-fu

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2919 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Thu Jun 21 08:04:51 -0700 2007
commit  1905ee737a15aca62649e9c5a1f05642d04c5a22
tree    02685bf1772925429bf5308d29061938c2f63836
parent  e6baf1c36f0aedc631e5608a781f78f18620350e
...
14
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
 
20
21
22
23
24
25
 
...
14
15
16
 
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
34
35
36
37
38
 
39
40
0
@@ -14,11 +14,26 @@ module PermalinkFu
0
     end
0
   end
0
   
0
- def has_permalink(attr_name, permalink_field = nil)
0
+ # Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field. This
0
+ # is done
0
+ #
0
+ # class Foo < ActiveRecord::Base
0
+ # # stores permalink form of #title to the #permalink attribute
0
+ # has_permalink :title
0
+ #
0
+ # # stores a permalink form of "#{category}-#{title}" to the #permalink attribute
0
+ #
0
+ # has_permalink [:category, :title]
0
+ #
0
+ # # stores permalink form of #title to the #category_permalink attribute
0
+ # has_permalink [:category, :title], :category_permalink
0
+ # end
0
+ #
0
+ def has_permalink(attr_names = [], permalink_field = nil)
0
     permalink_field ||= 'permalink'
0
- before_validation { |record| record.send("#{permalink_field}=", PermalinkFu.escape(record.send(attr_name).to_s)) if record.send(permalink_field).to_s.empty? }
0
+ before_validation { |record| record.send("#{permalink_field}=", Array(attr_names).collect { |attr_name| PermalinkFu.escape(record.send(attr_name).to_s) }.join('-')) if record.send(permalink_field).to_s.empty? }
0
   end
0
 end
0
 
0
 PermalinkFu.translation_to = 'ascii//ignore//translit'
0
-PermalinkFu.translation_from = 'utf-8'
0
\ No newline at end of file
0
+PermalinkFu.translation_from = 'utf-8'
...
6
7
8
9
 
10
11
12
...
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
23
...
25
26
27
 
 
28
29
30
...
38
39
40
41
42
 
 
 
 
 
 
 
 
 
 
 
...
6
7
8
 
9
10
11
12
...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
...
43
44
45
46
47
48
49
50
...
58
59
60
 
61
62
63
64
65
66
67
68
69
70
71
72
0
@@ -6,7 +6,7 @@ class MockModel
0
   attr_accessor :title
0
   attr_accessor :permalink
0
   
0
- def self.after_validation(&block)
0
+ def self.before_validation(&block)
0
     @@validation = block
0
   end
0
   
0
@@ -18,6 +18,24 @@ class MockModel
0
   has_permalink :title
0
 end
0
 
0
+class MockModelExtra
0
+ extend PermalinkFu
0
+ attr_accessor :title
0
+ attr_accessor :extra
0
+ attr_accessor :permalink
0
+
0
+ def self.before_validation(&block)
0
+ @@validation = block
0
+ end
0
+
0
+ def validate
0
+ @@validation.call self
0
+ permalink
0
+ end
0
+
0
+ has_permalink [:title, :extra]
0
+end
0
+
0
 class PermalinkFuTest < Test::Unit::TestCase
0
   @@samples = {
0
     'This IS a Tripped out title!!.!1 (well/ not really)' => 'this-is-a-tripped-out-title-1-well-not-really',
0
@@ -25,6 +43,8 @@ class PermalinkFuTest < Test::Unit::TestCase
0
     'āčēģīķļņū' => 'acegiklnu'
0
   }
0
 
0
+ @@extra = { 'some-)()()-ExtRa!/// .data==?> to \/\/test' => 'some-extra-data-to-test' }
0
+
0
   def test_should_escape_permalinks
0
     @@samples.each do |from, to|
0
       assert_equal to, PermalinkFu.escape(from)
0
@@ -38,4 +58,14 @@ class PermalinkFuTest < Test::Unit::TestCase
0
       assert_equal to, @m.validate
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+
0
+ def test_multiple_attribute_permalink
0
+ @m = MockModelExtra.new
0
+ @@samples.each do |from, to|
0
+ @@extra.each do |from_extra, to_extra|
0
+ @m.title = from; @m.extra = from_extra; @m.permalink = nil
0
+ assert_equal "#{to}-#{to_extra}", @m.validate
0
+ end
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.