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 !
move more of the permalink concerns into the site

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2222 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Tue Sep 19 09:05:11 -0700 2006
commit  1601e47585fcb3f84b0f3184ecc23e19cd2cff0b
tree    ef6d1b63812d3d9154544cd6823f0fd07b9b4965
parent  98040c8c10c42a3d4ac17ab1aa111341d460802e
...
1
2
3
4
5
6
7
8
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
146
147
148
149
150
151
152
153
154
155
156
 
157
158
159
...
187
188
189
 
 
 
 
190
191
 
192
193
194
...
197
198
199
200
 
201
202
203
...
1
2
3
 
 
4
5
6
...
128
129
130
 
 
 
 
 
 
 
 
 
 
 
 
 
131
132
133
134
135
 
 
 
 
 
 
 
136
137
138
139
...
167
168
169
170
171
172
173
174
 
175
176
177
178
...
181
182
183
 
184
185
186
187
0
@@ -1,8 +1,6 @@
0
 class Site < ActiveRecord::Base
0
   @@theme_path = Pathname.new(RAILS_ROOT) + 'themes'
0
   cattr_reader :theme_path
0
- PERMALINK_OPTIONS = { 'year' => '\d{4}', 'month' => '\d{1,2}', 'day' => '\d{1,2}', 'permalink' => '[\w\-]+', 'id' => '\d+' }
0
- PERMALINK_VAR = /^:([a-z]+)$/
0
 
0
   cattr_accessor :multi_sites_enabled
0
 
0
@@ -130,30 +128,12 @@ class Site < ActiveRecord::Base
0
   [:attachments, :templates, :resources].each { |m| delegate m, :to => :theme }
0
 
0
   def permalink_regex(refresh = false)
0
- if refresh || @permalink_regex.nil?
0
- @permalink_variables = []
0
- r = permalink_style.split('/').inject [] do |s, piece|
0
- if piece =~ PERMALINK_VAR
0
- @permalink_variables << $1.to_sym
0
- s << "(#{PERMALINK_OPTIONS[$1]})"
0
- else
0
- s << piece
0
- end
0
- end
0
- @permalink_regex = Regexp.new("^#{r.join('\/')}(\/comments(\/(\\d+))?)?$")
0
- end
0
-
0
+ @permalink_regex, @permalink_variables = Mephisto::Dispatcher.build_permalink_regex_with(permalink_style) if refresh || @permalink_regex.nil?
0
     @permalink_regex
0
   end
0
 
0
   def permalink_for(article)
0
- old_published = article.published_at
0
- article.published_at ||= Time.now.utc
0
- permalink_style.split('/').inject [''] do |s, piece|
0
- s << (piece =~ PERMALINK_VAR && PERMALINK_OPTIONS.keys.include?($1) ? article.send($1).to_s : piece)
0
- end.join('/')
0
- ensure
0
- article.published_at = old_published
0
+ Mephisto::Dispatcher.build_permalink_with(permalink_style, article)
0
   end
0
 
0
   def search_url(query, page = nil)
0
@@ -187,8 +167,12 @@ class Site < ActiveRecord::Base
0
   end
0
 
0
   protected
0
+ def permalink_variable_format?(var)
0
+ Mephisto::Dispatcher.variable_format?(var)
0
+ end
0
+
0
     def permalink_variable?(var)
0
- var =~ PERMALINK_VAR && PERMALINK_OPTIONS.keys.include?(var)
0
+ Mephisto::Dispatcher.variable?(var)
0
     end
0
 
0
     def check_permalink_style
0
@@ -197,7 +181,7 @@ class Site < ActiveRecord::Base
0
       pieces = permalink_style.split('/')
0
       errors.add :permalink_style, 'cannot have blank paths' if pieces.any?(&:blank?)
0
       pieces.each do |p|
0
- errors.add :permalink_style, "cannot contain '#{$1}' variable" if p =~ PERMALINK_VAR && !PERMALINK_OPTIONS.keys.include?($1)
0
+ errors.add :permalink_style, "cannot contain '#{p}' variable" unless p.blank? || permalink_variable_format?(p).nil? || permalink_variable?(p)
0
       end
0
       unless pieces.include?(':id') || pieces.include?(':permalink')
0
         errors.add :permalink_style, "must contain either :permalink or :id"
...
1
2
3
4
 
 
5
6
7
...
70
71
72
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
 
75
76
77
...
1
2
 
 
3
4
5
6
7
...
70
71
72
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
0
@@ -1,7 +1,7 @@
0
 module Mephisto
0
   class Dispatcher
0
- @@year_regex = /^\d{4}$/
0
- @@month_regex = /^\d{1,2}$/
0
+ PERMALINK_OPTIONS = { :year => '\d{4}', :month => '\d{1,2}', :day => '\d{1,2}', :permalink => '[\w\-]+', :id => '\d+' }
0
+ PERMALINK_VAR = /^:([a-z]+)$/
0
 
0
     def self.run(site, path)
0
       if options = recognize_permalink(site, path)
0
@@ -70,8 +70,42 @@ module Mephisto
0
         end
0
       end
0
     end
0
-
0
+
0
+ def self.build_permalink_regex_with(permalink_style)
0
+ variables = []
0
+ regex = permalink_style.split('/').inject [] do |s, piece|
0
+ if name = variable_format?(piece)
0
+ variables << name.to_sym
0
+ s << "(#{PERMALINK_OPTIONS[variables.last]})"
0
+ else
0
+ s << piece
0
+ end
0
+ end
0
+
0
+ [Regexp.new("^#{regex.join('\/')}(\/comments(\/(\\d+))?)?$"), variables]
0
+ end
0
+
0
+ def self.variable_format?(var)
0
+ var =~ PERMALINK_VAR ? $1 : nil
0
+ end
0
+
0
+ def self.variable?(var)
0
+ (name = variable_format?(var)) && PERMALINK_OPTIONS.keys.include?(name.to_sym) rescue nil
0
+ end
0
+
0
+ def self.build_permalink_with(permalink_style, article)
0
+ old_published = article.published_at
0
+ article.published_at ||= Time.now.utc
0
+ permalink_style.split('/').inject [''] do |s, piece|
0
+ s << ((name = variable_format?(piece)) && PERMALINK_OPTIONS.keys.include?(name.to_sym) ? article.send(name).to_s : piece)
0
+ end.join('/')
0
+ ensure
0
+ article.published_at = old_published
0
+ end
0
+
0
     private
0
+ @@year_regex = %r{^#{PERMALINK_OPTIONS[:year]}$}
0
+ @@month_regex = %r{^#{PERMALINK_OPTIONS[:month]}$}
0
       def self.year?(n)
0
         n.nil? || n =~ @@year_regex
0
       end
...
40
41
42
43
 
44
45
46
...
40
41
42
 
43
44
45
46
0
@@ -40,7 +40,7 @@ context "Site Permalink Validations" do
0
 
0
     @site.permalink_style = ':year/:foo/:month/:day/:permalink'
0
     assert !@site.valid?
0
- assert_equal "cannot contain 'foo' variable", @site.errors.on(:permalink_style)
0
+ assert_equal "cannot contain ':foo' variable", @site.errors.on(:permalink_style)
0
   end
0
 end
0
 

Comments

    No one has commented yet.