knitkit 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/app/models/document.rb +18 -1
- data/app/models/website.rb +6 -4
- data/app/widgets/contact_us/base.rb +1 -1
- data/app/widgets/search/base.rb +6 -6
- data/db/migrate/20120628165503_add_dynamic_attributes_to_documents.rb +5 -0
- data/lib/knitkit/engine.rb +0 -1
- data/lib/knitkit/version.rb +1 -1
- data/lib/knitkit.rb +1 -0
- data/lib/permalink_fu/README.markdown +64 -0
- data/lib/permalink_fu/init.rb +1 -0
- data/lib/permalink_fu/lib/data/x00.yml +257 -0
- data/lib/permalink_fu/lib/data/x01.yml +257 -0
- data/lib/permalink_fu/lib/data/x02.yml +256 -0
- data/lib/permalink_fu/lib/data/x03.yml +256 -0
- data/lib/permalink_fu/lib/data/x04.yml +256 -0
- data/lib/permalink_fu/lib/data/x05.yml +256 -0
- data/lib/permalink_fu/lib/data/x06.yml +256 -0
- data/lib/permalink_fu/lib/data/x07.yml +256 -0
- data/lib/permalink_fu/lib/data/x09.yml +256 -0
- data/lib/permalink_fu/lib/data/x0a.yml +256 -0
- data/lib/permalink_fu/lib/data/x0b.yml +256 -0
- data/lib/permalink_fu/lib/data/x0c.yml +256 -0
- data/lib/permalink_fu/lib/data/x0d.yml +256 -0
- data/lib/permalink_fu/lib/data/x0e.yml +256 -0
- data/lib/permalink_fu/lib/data/x0f.yml +256 -0
- data/lib/permalink_fu/lib/data/x10.yml +256 -0
- data/lib/permalink_fu/lib/data/x11.yml +256 -0
- data/lib/permalink_fu/lib/data/x12.yml +257 -0
- data/lib/permalink_fu/lib/data/x13.yml +256 -0
- data/lib/permalink_fu/lib/data/x14.yml +257 -0
- data/lib/permalink_fu/lib/data/x15.yml +257 -0
- data/lib/permalink_fu/lib/data/x16.yml +256 -0
- data/lib/permalink_fu/lib/data/x17.yml +256 -0
- data/lib/permalink_fu/lib/data/x18.yml +256 -0
- data/lib/permalink_fu/lib/data/x1e.yml +256 -0
- data/lib/permalink_fu/lib/data/x1f.yml +256 -0
- data/lib/permalink_fu/lib/data/x20.yml +256 -0
- data/lib/permalink_fu/lib/data/x21.yml +256 -0
- data/lib/permalink_fu/lib/data/x22.yml +256 -0
- data/lib/permalink_fu/lib/data/x23.yml +256 -0
- data/lib/permalink_fu/lib/data/x24.yml +256 -0
- data/lib/permalink_fu/lib/data/x25.yml +256 -0
- data/lib/permalink_fu/lib/data/x26.yml +256 -0
- data/lib/permalink_fu/lib/data/x27.yml +256 -0
- data/lib/permalink_fu/lib/data/x28.yml +257 -0
- data/lib/permalink_fu/lib/data/x2e.yml +256 -0
- data/lib/permalink_fu/lib/data/x2f.yml +256 -0
- data/lib/permalink_fu/lib/data/x30.yml +256 -0
- data/lib/permalink_fu/lib/data/x31.yml +256 -0
- data/lib/permalink_fu/lib/data/x32.yml +256 -0
- data/lib/permalink_fu/lib/data/x33.yml +256 -0
- data/lib/permalink_fu/lib/data/x4d.yml +256 -0
- data/lib/permalink_fu/lib/data/x4e.yml +257 -0
- data/lib/permalink_fu/lib/data/x4f.yml +257 -0
- data/lib/permalink_fu/lib/data/x50.yml +257 -0
- data/lib/permalink_fu/lib/data/x51.yml +257 -0
- data/lib/permalink_fu/lib/data/x52.yml +257 -0
- data/lib/permalink_fu/lib/data/x53.yml +257 -0
- data/lib/permalink_fu/lib/data/x54.yml +257 -0
- data/lib/permalink_fu/lib/data/x55.yml +257 -0
- data/lib/permalink_fu/lib/data/x56.yml +257 -0
- data/lib/permalink_fu/lib/data/x57.yml +257 -0
- data/lib/permalink_fu/lib/data/x58.yml +257 -0
- data/lib/permalink_fu/lib/data/x59.yml +257 -0
- data/lib/permalink_fu/lib/data/x5a.yml +257 -0
- data/lib/permalink_fu/lib/data/x5b.yml +257 -0
- data/lib/permalink_fu/lib/data/x5c.yml +257 -0
- data/lib/permalink_fu/lib/data/x5d.yml +257 -0
- data/lib/permalink_fu/lib/data/x5e.yml +257 -0
- data/lib/permalink_fu/lib/data/x5f.yml +257 -0
- data/lib/permalink_fu/lib/data/x60.yml +257 -0
- data/lib/permalink_fu/lib/data/x61.yml +257 -0
- data/lib/permalink_fu/lib/data/x62.yml +257 -0
- data/lib/permalink_fu/lib/data/x63.yml +257 -0
- data/lib/permalink_fu/lib/data/x64.yml +257 -0
- data/lib/permalink_fu/lib/data/x65.yml +257 -0
- data/lib/permalink_fu/lib/data/x66.yml +257 -0
- data/lib/permalink_fu/lib/data/x67.yml +257 -0
- data/lib/permalink_fu/lib/data/x68.yml +257 -0
- data/lib/permalink_fu/lib/data/x69.yml +257 -0
- data/lib/permalink_fu/lib/data/x6a.yml +257 -0
- data/lib/permalink_fu/lib/data/x6b.yml +257 -0
- data/lib/permalink_fu/lib/data/x6c.yml +257 -0
- data/lib/permalink_fu/lib/data/x6d.yml +257 -0
- data/lib/permalink_fu/lib/data/x6e.yml +257 -0
- data/lib/permalink_fu/lib/data/x6f.yml +257 -0
- data/lib/permalink_fu/lib/data/x70.yml +257 -0
- data/lib/permalink_fu/lib/data/x71.yml +257 -0
- data/lib/permalink_fu/lib/data/x72.yml +257 -0
- data/lib/permalink_fu/lib/data/x73.yml +257 -0
- data/lib/permalink_fu/lib/data/x74.yml +257 -0
- data/lib/permalink_fu/lib/data/x75.yml +257 -0
- data/lib/permalink_fu/lib/data/x76.yml +257 -0
- data/lib/permalink_fu/lib/data/x77.yml +257 -0
- data/lib/permalink_fu/lib/data/x78.yml +257 -0
- data/lib/permalink_fu/lib/data/x79.yml +257 -0
- data/lib/permalink_fu/lib/data/x7a.yml +257 -0
- data/lib/permalink_fu/lib/data/x7b.yml +257 -0
- data/lib/permalink_fu/lib/data/x7c.yml +257 -0
- data/lib/permalink_fu/lib/data/x7d.yml +257 -0
- data/lib/permalink_fu/lib/data/x7e.yml +257 -0
- data/lib/permalink_fu/lib/data/x7f.yml +257 -0
- data/lib/permalink_fu/lib/data/x80.yml +257 -0
- data/lib/permalink_fu/lib/data/x81.yml +257 -0
- data/lib/permalink_fu/lib/data/x82.yml +257 -0
- data/lib/permalink_fu/lib/data/x83.yml +257 -0
- data/lib/permalink_fu/lib/data/x84.yml +257 -0
- data/lib/permalink_fu/lib/data/x85.yml +257 -0
- data/lib/permalink_fu/lib/data/x86.yml +257 -0
- data/lib/permalink_fu/lib/data/x87.yml +257 -0
- data/lib/permalink_fu/lib/data/x88.yml +257 -0
- data/lib/permalink_fu/lib/data/x89.yml +257 -0
- data/lib/permalink_fu/lib/data/x8a.yml +257 -0
- data/lib/permalink_fu/lib/data/x8b.yml +257 -0
- data/lib/permalink_fu/lib/data/x8c.yml +257 -0
- data/lib/permalink_fu/lib/data/x8d.yml +257 -0
- data/lib/permalink_fu/lib/data/x8e.yml +257 -0
- data/lib/permalink_fu/lib/data/x8f.yml +257 -0
- data/lib/permalink_fu/lib/data/x90.yml +257 -0
- data/lib/permalink_fu/lib/data/x91.yml +257 -0
- data/lib/permalink_fu/lib/data/x92.yml +257 -0
- data/lib/permalink_fu/lib/data/x93.yml +257 -0
- data/lib/permalink_fu/lib/data/x94.yml +257 -0
- data/lib/permalink_fu/lib/data/x95.yml +257 -0
- data/lib/permalink_fu/lib/data/x96.yml +257 -0
- data/lib/permalink_fu/lib/data/x97.yml +257 -0
- data/lib/permalink_fu/lib/data/x98.yml +257 -0
- data/lib/permalink_fu/lib/data/x99.yml +257 -0
- data/lib/permalink_fu/lib/data/x9a.yml +257 -0
- data/lib/permalink_fu/lib/data/x9b.yml +257 -0
- data/lib/permalink_fu/lib/data/x9c.yml +257 -0
- data/lib/permalink_fu/lib/data/x9d.yml +257 -0
- data/lib/permalink_fu/lib/data/x9e.yml +257 -0
- data/lib/permalink_fu/lib/data/x9f.yml +256 -0
- data/lib/permalink_fu/lib/data/xa0.yml +257 -0
- data/lib/permalink_fu/lib/data/xa1.yml +257 -0
- data/lib/permalink_fu/lib/data/xa2.yml +257 -0
- data/lib/permalink_fu/lib/data/xa3.yml +257 -0
- data/lib/permalink_fu/lib/data/xa4.yml +256 -0
- data/lib/permalink_fu/lib/data/xac.yml +257 -0
- data/lib/permalink_fu/lib/data/xad.yml +257 -0
- data/lib/permalink_fu/lib/data/xae.yml +257 -0
- data/lib/permalink_fu/lib/data/xaf.yml +257 -0
- data/lib/permalink_fu/lib/data/xb0.yml +257 -0
- data/lib/permalink_fu/lib/data/xb1.yml +257 -0
- data/lib/permalink_fu/lib/data/xb2.yml +257 -0
- data/lib/permalink_fu/lib/data/xb3.yml +257 -0
- data/lib/permalink_fu/lib/data/xb4.yml +257 -0
- data/lib/permalink_fu/lib/data/xb5.yml +257 -0
- data/lib/permalink_fu/lib/data/xb6.yml +257 -0
- data/lib/permalink_fu/lib/data/xb7.yml +257 -0
- data/lib/permalink_fu/lib/data/xb8.yml +257 -0
- data/lib/permalink_fu/lib/data/xb9.yml +257 -0
- data/lib/permalink_fu/lib/data/xba.yml +257 -0
- data/lib/permalink_fu/lib/data/xbb.yml +257 -0
- data/lib/permalink_fu/lib/data/xbc.yml +257 -0
- data/lib/permalink_fu/lib/data/xbd.yml +257 -0
- data/lib/permalink_fu/lib/data/xbe.yml +257 -0
- data/lib/permalink_fu/lib/data/xbf.yml +257 -0
- data/lib/permalink_fu/lib/data/xc0.yml +257 -0
- data/lib/permalink_fu/lib/data/xc1.yml +257 -0
- data/lib/permalink_fu/lib/data/xc2.yml +257 -0
- data/lib/permalink_fu/lib/data/xc3.yml +257 -0
- data/lib/permalink_fu/lib/data/xc4.yml +257 -0
- data/lib/permalink_fu/lib/data/xc5.yml +257 -0
- data/lib/permalink_fu/lib/data/xc6.yml +257 -0
- data/lib/permalink_fu/lib/data/xc7.yml +257 -0
- data/lib/permalink_fu/lib/data/xc8.yml +257 -0
- data/lib/permalink_fu/lib/data/xc9.yml +257 -0
- data/lib/permalink_fu/lib/data/xca.yml +257 -0
- data/lib/permalink_fu/lib/data/xcb.yml +257 -0
- data/lib/permalink_fu/lib/data/xcc.yml +257 -0
- data/lib/permalink_fu/lib/data/xcd.yml +257 -0
- data/lib/permalink_fu/lib/data/xce.yml +257 -0
- data/lib/permalink_fu/lib/data/xcf.yml +257 -0
- data/lib/permalink_fu/lib/data/xd0.yml +257 -0
- data/lib/permalink_fu/lib/data/xd1.yml +257 -0
- data/lib/permalink_fu/lib/data/xd2.yml +257 -0
- data/lib/permalink_fu/lib/data/xd3.yml +257 -0
- data/lib/permalink_fu/lib/data/xd4.yml +257 -0
- data/lib/permalink_fu/lib/data/xd5.yml +257 -0
- data/lib/permalink_fu/lib/data/xd6.yml +257 -0
- data/lib/permalink_fu/lib/data/xd7.yml +256 -0
- data/lib/permalink_fu/lib/data/xf9.yml +257 -0
- data/lib/permalink_fu/lib/data/xfa.yml +256 -0
- data/lib/permalink_fu/lib/data/xfb.yml +257 -0
- data/lib/permalink_fu/lib/data/xfc.yml +257 -0
- data/lib/permalink_fu/lib/data/xfd.yml +256 -0
- data/lib/permalink_fu/lib/data/xfe.yml +257 -0
- data/lib/permalink_fu/lib/data/xff.yml +257 -0
- data/lib/permalink_fu/lib/permalink_fu.rb +176 -0
- data/lib/permalink_fu/test/permalink_fu_test.rb +476 -0
- data/lib/permalink_fu/test/test_helper.rb +13 -0
- data/public/images/check.png +0 -0
- data/public/images/credit_card.png +0 -0
- metadata +214 -39
@@ -0,0 +1,257 @@
|
|
1
|
+
---
|
2
|
+
- '[?]'
|
3
|
+
- '!'
|
4
|
+
- '"'
|
5
|
+
- '#'
|
6
|
+
- $
|
7
|
+
- '%'
|
8
|
+
- '&'
|
9
|
+
- "'"
|
10
|
+
- (
|
11
|
+
- )
|
12
|
+
- '*'
|
13
|
+
- +
|
14
|
+
- ','
|
15
|
+
- -
|
16
|
+
- .
|
17
|
+
- /
|
18
|
+
- 0
|
19
|
+
- 1
|
20
|
+
- 2
|
21
|
+
- 3
|
22
|
+
- 4
|
23
|
+
- 5
|
24
|
+
- 6
|
25
|
+
- 7
|
26
|
+
- 8
|
27
|
+
- 9
|
28
|
+
- ':'
|
29
|
+
- ;
|
30
|
+
- <
|
31
|
+
- =
|
32
|
+
- '>'
|
33
|
+
- '?'
|
34
|
+
- '@'
|
35
|
+
- A
|
36
|
+
- B
|
37
|
+
- C
|
38
|
+
- D
|
39
|
+
- E
|
40
|
+
- F
|
41
|
+
- G
|
42
|
+
- H
|
43
|
+
- I
|
44
|
+
- J
|
45
|
+
- K
|
46
|
+
- L
|
47
|
+
- M
|
48
|
+
- N
|
49
|
+
- O
|
50
|
+
- P
|
51
|
+
- Q
|
52
|
+
- R
|
53
|
+
- S
|
54
|
+
- T
|
55
|
+
- U
|
56
|
+
- V
|
57
|
+
- W
|
58
|
+
- X
|
59
|
+
- Y
|
60
|
+
- Z
|
61
|
+
- '['
|
62
|
+
- \
|
63
|
+
- ']'
|
64
|
+
- '^'
|
65
|
+
- _
|
66
|
+
- '`'
|
67
|
+
- a
|
68
|
+
- b
|
69
|
+
- c
|
70
|
+
- d
|
71
|
+
- e
|
72
|
+
- f
|
73
|
+
- g
|
74
|
+
- h
|
75
|
+
- i
|
76
|
+
- j
|
77
|
+
- k
|
78
|
+
- l
|
79
|
+
- m
|
80
|
+
- n
|
81
|
+
- o
|
82
|
+
- p
|
83
|
+
- q
|
84
|
+
- r
|
85
|
+
- s
|
86
|
+
- t
|
87
|
+
- u
|
88
|
+
- v
|
89
|
+
- w
|
90
|
+
- x
|
91
|
+
- y
|
92
|
+
- z
|
93
|
+
- '{'
|
94
|
+
- '|'
|
95
|
+
- '}'
|
96
|
+
- '~'
|
97
|
+
- '[?]'
|
98
|
+
- '[?]'
|
99
|
+
- .
|
100
|
+
- '['
|
101
|
+
- ']'
|
102
|
+
- ','
|
103
|
+
- '*'
|
104
|
+
- wo
|
105
|
+
- a
|
106
|
+
- i
|
107
|
+
- u
|
108
|
+
- e
|
109
|
+
- o
|
110
|
+
- ya
|
111
|
+
- yu
|
112
|
+
- yo
|
113
|
+
- tu
|
114
|
+
- +
|
115
|
+
- a
|
116
|
+
- i
|
117
|
+
- u
|
118
|
+
- e
|
119
|
+
- o
|
120
|
+
- ka
|
121
|
+
- ki
|
122
|
+
- ku
|
123
|
+
- ke
|
124
|
+
- ko
|
125
|
+
- sa
|
126
|
+
- si
|
127
|
+
- su
|
128
|
+
- se
|
129
|
+
- so
|
130
|
+
- ta
|
131
|
+
- ti
|
132
|
+
- tu
|
133
|
+
- te
|
134
|
+
- to
|
135
|
+
- na
|
136
|
+
- ni
|
137
|
+
- nu
|
138
|
+
- ne
|
139
|
+
- no
|
140
|
+
- ha
|
141
|
+
- hi
|
142
|
+
- hu
|
143
|
+
- he
|
144
|
+
- ho
|
145
|
+
- ma
|
146
|
+
- mi
|
147
|
+
- mu
|
148
|
+
- me
|
149
|
+
- mo
|
150
|
+
- ya
|
151
|
+
- yu
|
152
|
+
- yo
|
153
|
+
- ra
|
154
|
+
- ri
|
155
|
+
- ru
|
156
|
+
- re
|
157
|
+
- ro
|
158
|
+
- wa
|
159
|
+
- n
|
160
|
+
- ':'
|
161
|
+
- ;
|
162
|
+
- ''
|
163
|
+
- g
|
164
|
+
- gg
|
165
|
+
- gs
|
166
|
+
- n
|
167
|
+
- nj
|
168
|
+
- nh
|
169
|
+
- d
|
170
|
+
- dd
|
171
|
+
- r
|
172
|
+
- lg
|
173
|
+
- lm
|
174
|
+
- lb
|
175
|
+
- ls
|
176
|
+
- lt
|
177
|
+
- lp
|
178
|
+
- rh
|
179
|
+
- m
|
180
|
+
- b
|
181
|
+
- bb
|
182
|
+
- bs
|
183
|
+
- s
|
184
|
+
- ss
|
185
|
+
- ''
|
186
|
+
- j
|
187
|
+
- jj
|
188
|
+
- c
|
189
|
+
- k
|
190
|
+
- t
|
191
|
+
- p
|
192
|
+
- h
|
193
|
+
- '[?]'
|
194
|
+
- '[?]'
|
195
|
+
- '[?]'
|
196
|
+
- a
|
197
|
+
- ae
|
198
|
+
- ya
|
199
|
+
- yae
|
200
|
+
- eo
|
201
|
+
- e
|
202
|
+
- '[?]'
|
203
|
+
- '[?]'
|
204
|
+
- yeo
|
205
|
+
- ye
|
206
|
+
- o
|
207
|
+
- wa
|
208
|
+
- wae
|
209
|
+
- oe
|
210
|
+
- '[?]'
|
211
|
+
- '[?]'
|
212
|
+
- yo
|
213
|
+
- u
|
214
|
+
- weo
|
215
|
+
- we
|
216
|
+
- wi
|
217
|
+
- yu
|
218
|
+
- '[?]'
|
219
|
+
- '[?]'
|
220
|
+
- eu
|
221
|
+
- yi
|
222
|
+
- i
|
223
|
+
- '[?]'
|
224
|
+
- '[?]'
|
225
|
+
- '[?]'
|
226
|
+
- /C
|
227
|
+
- PS
|
228
|
+
- '!'
|
229
|
+
- -
|
230
|
+
- '|'
|
231
|
+
- Y=
|
232
|
+
- W=
|
233
|
+
- '[?]'
|
234
|
+
- '|'
|
235
|
+
- -
|
236
|
+
- '|'
|
237
|
+
- -
|
238
|
+
- '|'
|
239
|
+
- '#'
|
240
|
+
- O
|
241
|
+
- '[?]'
|
242
|
+
- '[?]'
|
243
|
+
- '[?]'
|
244
|
+
- '[?]'
|
245
|
+
- '[?]'
|
246
|
+
- '[?]'
|
247
|
+
- '[?]'
|
248
|
+
- '[?]'
|
249
|
+
- '[?]'
|
250
|
+
- '[?]'
|
251
|
+
- '{'
|
252
|
+
- '|'
|
253
|
+
- '}'
|
254
|
+
- ''
|
255
|
+
- ''
|
256
|
+
- ''
|
257
|
+
- ''
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'digest/sha1'
|
3
|
+
|
4
|
+
module PermalinkFu
|
5
|
+
def has_permalink(attr_names = [], permalink_field = nil, options = {})
|
6
|
+
if permalink_field.is_a?(Hash)
|
7
|
+
options = permalink_field
|
8
|
+
permalink_field = nil
|
9
|
+
end
|
10
|
+
ClassMethods.setup_permalink_fu_on self do
|
11
|
+
self.permalink_attributes = Array(attr_names)
|
12
|
+
self.permalink_field = (permalink_field || 'permalink').to_s
|
13
|
+
self.permalink_options = {:unique => true}.update(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
include InstanceMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
# This method does the actual permalink escaping.
|
21
|
+
def escape(str)
|
22
|
+
s = ClassMethods.decode(str)#.force_encoding("UTF-8")
|
23
|
+
s.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
|
24
|
+
s.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
|
25
|
+
s.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
|
26
|
+
s.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
|
27
|
+
s.downcase!
|
28
|
+
s.size == 0 ? ClassMethods.random_permalink(str) : s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Contains class methods for ActiveRecord models that have permalinks
|
33
|
+
module ClassMethods
|
34
|
+
# Contains Unicode codepoints, loading as needed from YAML files
|
35
|
+
CODEPOINTS = Hash.new { |h, k|
|
36
|
+
h[k] = YAML::load_file(File.join(File.dirname(__FILE__), "data", "#{k}.yml"))
|
37
|
+
}
|
38
|
+
|
39
|
+
class << self
|
40
|
+
def decode(string)
|
41
|
+
string.gsub(/[^\x00-\x7f]/u) do |codepoint|
|
42
|
+
begin
|
43
|
+
CODEPOINTS["x%02x" % (codepoint.unpack("U")[0] >> 8)][codepoint.unpack("U")[0] & 255]
|
44
|
+
rescue
|
45
|
+
"_"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def random_permalink(seed = nil)
|
51
|
+
Digest::SHA1.hexdigest("#{seed}#{Time.now.to_s.split(//).sort_by {rand}}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.setup_permalink_fu_on(base)
|
56
|
+
base.extend self
|
57
|
+
class << base
|
58
|
+
attr_accessor :permalink_options
|
59
|
+
attr_accessor :permalink_attributes
|
60
|
+
attr_accessor :permalink_field
|
61
|
+
end
|
62
|
+
|
63
|
+
yield
|
64
|
+
|
65
|
+
if base.permalink_options[:unique]
|
66
|
+
base.before_validation :create_unique_permalink
|
67
|
+
else
|
68
|
+
base.before_validation :create_common_permalink
|
69
|
+
end
|
70
|
+
class << base
|
71
|
+
alias_method :define_attribute_methods_without_permalinks, :define_attribute_methods
|
72
|
+
alias_method :define_attribute_methods, :define_attribute_methods_with_permalinks
|
73
|
+
end unless base.respond_to?(:define_attribute_methods_without_permalinks)
|
74
|
+
end
|
75
|
+
|
76
|
+
def define_attribute_methods_with_permalinks
|
77
|
+
if (value = define_attribute_methods_without_permalinks) && self.permalink_field
|
78
|
+
class_eval <<-EOV
|
79
|
+
def #{self.permalink_field}=(new_value);
|
80
|
+
write_attribute(:#{self.permalink_field}, new_value.blank? ? '' : PermalinkFu.escape(new_value));
|
81
|
+
end
|
82
|
+
EOV
|
83
|
+
end
|
84
|
+
value
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# This contains instance methods for ActiveRecord models that have permalinks.
|
89
|
+
module InstanceMethods
|
90
|
+
protected
|
91
|
+
def create_common_permalink
|
92
|
+
return unless should_create_permalink?
|
93
|
+
if read_attribute(self.class.permalink_field).blank? || permalink_fields_changed?
|
94
|
+
send("#{self.class.permalink_field}=", create_permalink_for(self.class.permalink_attributes))
|
95
|
+
end
|
96
|
+
|
97
|
+
# Quit now if we have the changed method available and nothing has changed
|
98
|
+
permalink_changed = "#{self.class.permalink_field}_changed?"
|
99
|
+
return if respond_to?(permalink_changed) && !send(permalink_changed)
|
100
|
+
|
101
|
+
# Otherwise find the limit and crop the permalink
|
102
|
+
limit = self.class.columns_hash[self.class.permalink_field].limit
|
103
|
+
base = send("#{self.class.permalink_field}=", read_attribute(self.class.permalink_field)[0..limit - 1])
|
104
|
+
[limit, base]
|
105
|
+
end
|
106
|
+
|
107
|
+
def create_unique_permalink
|
108
|
+
limit, base = create_common_permalink
|
109
|
+
return if limit.nil? # nil if the permalink has not changed or :if/:unless fail
|
110
|
+
counter = 1
|
111
|
+
# oh how i wish i could use a hash for conditions
|
112
|
+
conditions = ["#{self.class.permalink_field} = ?", base]
|
113
|
+
unless new_record?
|
114
|
+
conditions.first << " and id != ?"
|
115
|
+
conditions << id
|
116
|
+
end
|
117
|
+
if self.class.permalink_options[:scope]
|
118
|
+
[self.class.permalink_options[:scope]].flatten.each do |scope|
|
119
|
+
value = send(scope)
|
120
|
+
if value
|
121
|
+
conditions.first << " and #{scope} = ?"
|
122
|
+
conditions << send(scope)
|
123
|
+
else
|
124
|
+
conditions.first << " and #{scope} IS NULL"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
while self.class.exists?(conditions)
|
129
|
+
suffix = "-#{counter += 1}"
|
130
|
+
conditions[1] = "#{base[0..limit-suffix.size-1]}#{suffix}"
|
131
|
+
send("#{self.class.permalink_field}=", conditions[1])
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def create_permalink_for(attr_names)
|
136
|
+
str = attr_names.collect { |attr_name| send(attr_name).to_s } * " "
|
137
|
+
str.blank? ? PermalinkFu::ClassMethods.random_permalink : str
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
def should_create_permalink?
|
142
|
+
if self.class.permalink_field.blank?
|
143
|
+
false
|
144
|
+
elsif self.class.permalink_options[:if]
|
145
|
+
evaluate_method(self.class.permalink_options[:if])
|
146
|
+
elsif self.class.permalink_options[:unless]
|
147
|
+
!evaluate_method(self.class.permalink_options[:unless])
|
148
|
+
else
|
149
|
+
true
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Don't even check _changed? methods unless :update is set
|
154
|
+
def permalink_fields_changed?
|
155
|
+
return false unless self.class.permalink_options[:update]
|
156
|
+
self.class.permalink_attributes.any? do |attribute|
|
157
|
+
changed_method = "#{attribute}_changed?"
|
158
|
+
respond_to?(changed_method) ? send(changed_method) : true
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def evaluate_method(method)
|
163
|
+
case method
|
164
|
+
when Symbol
|
165
|
+
send(method)
|
166
|
+
when String
|
167
|
+
eval(method, instance_eval { binding })
|
168
|
+
when Proc, Method
|
169
|
+
method.call(self)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Extend ActiveRecord functionality
|
176
|
+
ActiveRecord::Base.extend PermalinkFu
|