knitkit 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. data/app/models/document.rb +18 -1
  2. data/app/models/website.rb +6 -4
  3. data/app/widgets/contact_us/base.rb +1 -1
  4. data/app/widgets/search/base.rb +6 -6
  5. data/db/migrate/20120628165503_add_dynamic_attributes_to_documents.rb +5 -0
  6. data/lib/knitkit/engine.rb +0 -1
  7. data/lib/knitkit/version.rb +1 -1
  8. data/lib/knitkit.rb +1 -0
  9. data/lib/permalink_fu/README.markdown +64 -0
  10. data/lib/permalink_fu/init.rb +1 -0
  11. data/lib/permalink_fu/lib/data/x00.yml +257 -0
  12. data/lib/permalink_fu/lib/data/x01.yml +257 -0
  13. data/lib/permalink_fu/lib/data/x02.yml +256 -0
  14. data/lib/permalink_fu/lib/data/x03.yml +256 -0
  15. data/lib/permalink_fu/lib/data/x04.yml +256 -0
  16. data/lib/permalink_fu/lib/data/x05.yml +256 -0
  17. data/lib/permalink_fu/lib/data/x06.yml +256 -0
  18. data/lib/permalink_fu/lib/data/x07.yml +256 -0
  19. data/lib/permalink_fu/lib/data/x09.yml +256 -0
  20. data/lib/permalink_fu/lib/data/x0a.yml +256 -0
  21. data/lib/permalink_fu/lib/data/x0b.yml +256 -0
  22. data/lib/permalink_fu/lib/data/x0c.yml +256 -0
  23. data/lib/permalink_fu/lib/data/x0d.yml +256 -0
  24. data/lib/permalink_fu/lib/data/x0e.yml +256 -0
  25. data/lib/permalink_fu/lib/data/x0f.yml +256 -0
  26. data/lib/permalink_fu/lib/data/x10.yml +256 -0
  27. data/lib/permalink_fu/lib/data/x11.yml +256 -0
  28. data/lib/permalink_fu/lib/data/x12.yml +257 -0
  29. data/lib/permalink_fu/lib/data/x13.yml +256 -0
  30. data/lib/permalink_fu/lib/data/x14.yml +257 -0
  31. data/lib/permalink_fu/lib/data/x15.yml +257 -0
  32. data/lib/permalink_fu/lib/data/x16.yml +256 -0
  33. data/lib/permalink_fu/lib/data/x17.yml +256 -0
  34. data/lib/permalink_fu/lib/data/x18.yml +256 -0
  35. data/lib/permalink_fu/lib/data/x1e.yml +256 -0
  36. data/lib/permalink_fu/lib/data/x1f.yml +256 -0
  37. data/lib/permalink_fu/lib/data/x20.yml +256 -0
  38. data/lib/permalink_fu/lib/data/x21.yml +256 -0
  39. data/lib/permalink_fu/lib/data/x22.yml +256 -0
  40. data/lib/permalink_fu/lib/data/x23.yml +256 -0
  41. data/lib/permalink_fu/lib/data/x24.yml +256 -0
  42. data/lib/permalink_fu/lib/data/x25.yml +256 -0
  43. data/lib/permalink_fu/lib/data/x26.yml +256 -0
  44. data/lib/permalink_fu/lib/data/x27.yml +256 -0
  45. data/lib/permalink_fu/lib/data/x28.yml +257 -0
  46. data/lib/permalink_fu/lib/data/x2e.yml +256 -0
  47. data/lib/permalink_fu/lib/data/x2f.yml +256 -0
  48. data/lib/permalink_fu/lib/data/x30.yml +256 -0
  49. data/lib/permalink_fu/lib/data/x31.yml +256 -0
  50. data/lib/permalink_fu/lib/data/x32.yml +256 -0
  51. data/lib/permalink_fu/lib/data/x33.yml +256 -0
  52. data/lib/permalink_fu/lib/data/x4d.yml +256 -0
  53. data/lib/permalink_fu/lib/data/x4e.yml +257 -0
  54. data/lib/permalink_fu/lib/data/x4f.yml +257 -0
  55. data/lib/permalink_fu/lib/data/x50.yml +257 -0
  56. data/lib/permalink_fu/lib/data/x51.yml +257 -0
  57. data/lib/permalink_fu/lib/data/x52.yml +257 -0
  58. data/lib/permalink_fu/lib/data/x53.yml +257 -0
  59. data/lib/permalink_fu/lib/data/x54.yml +257 -0
  60. data/lib/permalink_fu/lib/data/x55.yml +257 -0
  61. data/lib/permalink_fu/lib/data/x56.yml +257 -0
  62. data/lib/permalink_fu/lib/data/x57.yml +257 -0
  63. data/lib/permalink_fu/lib/data/x58.yml +257 -0
  64. data/lib/permalink_fu/lib/data/x59.yml +257 -0
  65. data/lib/permalink_fu/lib/data/x5a.yml +257 -0
  66. data/lib/permalink_fu/lib/data/x5b.yml +257 -0
  67. data/lib/permalink_fu/lib/data/x5c.yml +257 -0
  68. data/lib/permalink_fu/lib/data/x5d.yml +257 -0
  69. data/lib/permalink_fu/lib/data/x5e.yml +257 -0
  70. data/lib/permalink_fu/lib/data/x5f.yml +257 -0
  71. data/lib/permalink_fu/lib/data/x60.yml +257 -0
  72. data/lib/permalink_fu/lib/data/x61.yml +257 -0
  73. data/lib/permalink_fu/lib/data/x62.yml +257 -0
  74. data/lib/permalink_fu/lib/data/x63.yml +257 -0
  75. data/lib/permalink_fu/lib/data/x64.yml +257 -0
  76. data/lib/permalink_fu/lib/data/x65.yml +257 -0
  77. data/lib/permalink_fu/lib/data/x66.yml +257 -0
  78. data/lib/permalink_fu/lib/data/x67.yml +257 -0
  79. data/lib/permalink_fu/lib/data/x68.yml +257 -0
  80. data/lib/permalink_fu/lib/data/x69.yml +257 -0
  81. data/lib/permalink_fu/lib/data/x6a.yml +257 -0
  82. data/lib/permalink_fu/lib/data/x6b.yml +257 -0
  83. data/lib/permalink_fu/lib/data/x6c.yml +257 -0
  84. data/lib/permalink_fu/lib/data/x6d.yml +257 -0
  85. data/lib/permalink_fu/lib/data/x6e.yml +257 -0
  86. data/lib/permalink_fu/lib/data/x6f.yml +257 -0
  87. data/lib/permalink_fu/lib/data/x70.yml +257 -0
  88. data/lib/permalink_fu/lib/data/x71.yml +257 -0
  89. data/lib/permalink_fu/lib/data/x72.yml +257 -0
  90. data/lib/permalink_fu/lib/data/x73.yml +257 -0
  91. data/lib/permalink_fu/lib/data/x74.yml +257 -0
  92. data/lib/permalink_fu/lib/data/x75.yml +257 -0
  93. data/lib/permalink_fu/lib/data/x76.yml +257 -0
  94. data/lib/permalink_fu/lib/data/x77.yml +257 -0
  95. data/lib/permalink_fu/lib/data/x78.yml +257 -0
  96. data/lib/permalink_fu/lib/data/x79.yml +257 -0
  97. data/lib/permalink_fu/lib/data/x7a.yml +257 -0
  98. data/lib/permalink_fu/lib/data/x7b.yml +257 -0
  99. data/lib/permalink_fu/lib/data/x7c.yml +257 -0
  100. data/lib/permalink_fu/lib/data/x7d.yml +257 -0
  101. data/lib/permalink_fu/lib/data/x7e.yml +257 -0
  102. data/lib/permalink_fu/lib/data/x7f.yml +257 -0
  103. data/lib/permalink_fu/lib/data/x80.yml +257 -0
  104. data/lib/permalink_fu/lib/data/x81.yml +257 -0
  105. data/lib/permalink_fu/lib/data/x82.yml +257 -0
  106. data/lib/permalink_fu/lib/data/x83.yml +257 -0
  107. data/lib/permalink_fu/lib/data/x84.yml +257 -0
  108. data/lib/permalink_fu/lib/data/x85.yml +257 -0
  109. data/lib/permalink_fu/lib/data/x86.yml +257 -0
  110. data/lib/permalink_fu/lib/data/x87.yml +257 -0
  111. data/lib/permalink_fu/lib/data/x88.yml +257 -0
  112. data/lib/permalink_fu/lib/data/x89.yml +257 -0
  113. data/lib/permalink_fu/lib/data/x8a.yml +257 -0
  114. data/lib/permalink_fu/lib/data/x8b.yml +257 -0
  115. data/lib/permalink_fu/lib/data/x8c.yml +257 -0
  116. data/lib/permalink_fu/lib/data/x8d.yml +257 -0
  117. data/lib/permalink_fu/lib/data/x8e.yml +257 -0
  118. data/lib/permalink_fu/lib/data/x8f.yml +257 -0
  119. data/lib/permalink_fu/lib/data/x90.yml +257 -0
  120. data/lib/permalink_fu/lib/data/x91.yml +257 -0
  121. data/lib/permalink_fu/lib/data/x92.yml +257 -0
  122. data/lib/permalink_fu/lib/data/x93.yml +257 -0
  123. data/lib/permalink_fu/lib/data/x94.yml +257 -0
  124. data/lib/permalink_fu/lib/data/x95.yml +257 -0
  125. data/lib/permalink_fu/lib/data/x96.yml +257 -0
  126. data/lib/permalink_fu/lib/data/x97.yml +257 -0
  127. data/lib/permalink_fu/lib/data/x98.yml +257 -0
  128. data/lib/permalink_fu/lib/data/x99.yml +257 -0
  129. data/lib/permalink_fu/lib/data/x9a.yml +257 -0
  130. data/lib/permalink_fu/lib/data/x9b.yml +257 -0
  131. data/lib/permalink_fu/lib/data/x9c.yml +257 -0
  132. data/lib/permalink_fu/lib/data/x9d.yml +257 -0
  133. data/lib/permalink_fu/lib/data/x9e.yml +257 -0
  134. data/lib/permalink_fu/lib/data/x9f.yml +256 -0
  135. data/lib/permalink_fu/lib/data/xa0.yml +257 -0
  136. data/lib/permalink_fu/lib/data/xa1.yml +257 -0
  137. data/lib/permalink_fu/lib/data/xa2.yml +257 -0
  138. data/lib/permalink_fu/lib/data/xa3.yml +257 -0
  139. data/lib/permalink_fu/lib/data/xa4.yml +256 -0
  140. data/lib/permalink_fu/lib/data/xac.yml +257 -0
  141. data/lib/permalink_fu/lib/data/xad.yml +257 -0
  142. data/lib/permalink_fu/lib/data/xae.yml +257 -0
  143. data/lib/permalink_fu/lib/data/xaf.yml +257 -0
  144. data/lib/permalink_fu/lib/data/xb0.yml +257 -0
  145. data/lib/permalink_fu/lib/data/xb1.yml +257 -0
  146. data/lib/permalink_fu/lib/data/xb2.yml +257 -0
  147. data/lib/permalink_fu/lib/data/xb3.yml +257 -0
  148. data/lib/permalink_fu/lib/data/xb4.yml +257 -0
  149. data/lib/permalink_fu/lib/data/xb5.yml +257 -0
  150. data/lib/permalink_fu/lib/data/xb6.yml +257 -0
  151. data/lib/permalink_fu/lib/data/xb7.yml +257 -0
  152. data/lib/permalink_fu/lib/data/xb8.yml +257 -0
  153. data/lib/permalink_fu/lib/data/xb9.yml +257 -0
  154. data/lib/permalink_fu/lib/data/xba.yml +257 -0
  155. data/lib/permalink_fu/lib/data/xbb.yml +257 -0
  156. data/lib/permalink_fu/lib/data/xbc.yml +257 -0
  157. data/lib/permalink_fu/lib/data/xbd.yml +257 -0
  158. data/lib/permalink_fu/lib/data/xbe.yml +257 -0
  159. data/lib/permalink_fu/lib/data/xbf.yml +257 -0
  160. data/lib/permalink_fu/lib/data/xc0.yml +257 -0
  161. data/lib/permalink_fu/lib/data/xc1.yml +257 -0
  162. data/lib/permalink_fu/lib/data/xc2.yml +257 -0
  163. data/lib/permalink_fu/lib/data/xc3.yml +257 -0
  164. data/lib/permalink_fu/lib/data/xc4.yml +257 -0
  165. data/lib/permalink_fu/lib/data/xc5.yml +257 -0
  166. data/lib/permalink_fu/lib/data/xc6.yml +257 -0
  167. data/lib/permalink_fu/lib/data/xc7.yml +257 -0
  168. data/lib/permalink_fu/lib/data/xc8.yml +257 -0
  169. data/lib/permalink_fu/lib/data/xc9.yml +257 -0
  170. data/lib/permalink_fu/lib/data/xca.yml +257 -0
  171. data/lib/permalink_fu/lib/data/xcb.yml +257 -0
  172. data/lib/permalink_fu/lib/data/xcc.yml +257 -0
  173. data/lib/permalink_fu/lib/data/xcd.yml +257 -0
  174. data/lib/permalink_fu/lib/data/xce.yml +257 -0
  175. data/lib/permalink_fu/lib/data/xcf.yml +257 -0
  176. data/lib/permalink_fu/lib/data/xd0.yml +257 -0
  177. data/lib/permalink_fu/lib/data/xd1.yml +257 -0
  178. data/lib/permalink_fu/lib/data/xd2.yml +257 -0
  179. data/lib/permalink_fu/lib/data/xd3.yml +257 -0
  180. data/lib/permalink_fu/lib/data/xd4.yml +257 -0
  181. data/lib/permalink_fu/lib/data/xd5.yml +257 -0
  182. data/lib/permalink_fu/lib/data/xd6.yml +257 -0
  183. data/lib/permalink_fu/lib/data/xd7.yml +256 -0
  184. data/lib/permalink_fu/lib/data/xf9.yml +257 -0
  185. data/lib/permalink_fu/lib/data/xfa.yml +256 -0
  186. data/lib/permalink_fu/lib/data/xfb.yml +257 -0
  187. data/lib/permalink_fu/lib/data/xfc.yml +257 -0
  188. data/lib/permalink_fu/lib/data/xfd.yml +256 -0
  189. data/lib/permalink_fu/lib/data/xfe.yml +257 -0
  190. data/lib/permalink_fu/lib/data/xff.yml +257 -0
  191. data/lib/permalink_fu/lib/permalink_fu.rb +176 -0
  192. data/lib/permalink_fu/test/permalink_fu_test.rb +476 -0
  193. data/lib/permalink_fu/test/test_helper.rb +13 -0
  194. data/public/images/check.png +0 -0
  195. data/public/images/credit_card.png +0 -0
  196. metadata +214 -39
@@ -0,0 +1,476 @@
1
+ # encoding: UTF-8
2
+
3
+ # Load the plugin's test_helper (Rails 2.x needs the path)
4
+ begin
5
+ require File.dirname(__FILE__) + '/test_helper.rb'
6
+ rescue LoadError
7
+ require 'test_helper'
8
+ end
9
+
10
+ class PermalinkFuTest < Test::Unit::TestCase
11
+ @@samples = {
12
+ 'This IS a Tripped out title!!.!1 (well/ not really)' => 'this-is-a-tripped-out-title1-well-not-really',
13
+ '////// meph1sto r0x ! \\\\\\' => 'meph1sto-r0x',
14
+ 'āčēģīķļņū' => 'acegiklnu',
15
+ '中文測試 chinese text' => 'zhong-wen-ce-shi-chinese-text',
16
+ 'fööbär' => 'foobar'
17
+ }
18
+
19
+ @@extra = { 'some-)()()-ExtRa!/// .data==?> to \/\/test' => 'some-extra-data-to-test' }
20
+
21
+ def test_basemodel
22
+ @m = BaseModel.new
23
+ assert @m.valid?
24
+ assert_equal @m.id, nil
25
+ assert_equal @m.title, nil
26
+ assert_equal @m.permalink, nil
27
+ assert_equal @m.extra, nil
28
+ assert_equal @m.foo, nil
29
+ end
30
+
31
+ def test_set_new_permalink_attributes_on_sub_class
32
+ @m = ClassModel.new
33
+ @m.title = 'foo'
34
+ @m.extra = 'bar'
35
+ assert @m.valid?
36
+ assert_equal @m.permalink, 'foo'
37
+
38
+ @m = SubClassHasPermalinkModel.new
39
+ @m.title = 'foo'
40
+ @m.extra = 'bar'
41
+ assert @m.valid?
42
+ assert_equal @m.permalink, 'foo-bar'
43
+ end
44
+
45
+ def test_should_not_inherit_permalink_attributes
46
+ @m = SubClassNoPermalinkModel.new
47
+ @m.title = 'foo'
48
+ assert @m.valid?
49
+ assert_equal @m.permalink, nil
50
+ end
51
+
52
+ def test_should_escape_permalinks
53
+ @@samples.each do |from, to|
54
+ assert_equal to, PermalinkFu.escape(from)
55
+ end
56
+ end
57
+
58
+ def test_should_escape_activerecord_model
59
+ @m = MockModel.new
60
+ @@samples.each do |from, to|
61
+ @m.title = from; @m.permalink = nil
62
+ assert @m.valid?
63
+ assert_equal to, @m.permalink
64
+ end
65
+ end
66
+
67
+ def test_should_escape_activerecord_model_with_existing_permalink
68
+ @m = MockModel.new
69
+ @@samples.each do |from, to|
70
+ @m.title = 'whatever'; @m.permalink = from
71
+ assert @m.valid?
72
+ assert_equal to, @m.permalink
73
+ end
74
+ end
75
+
76
+ def test_multiple_attribute_permalink
77
+ @m = MockModelExtra.new
78
+ @@samples.each do |from, to|
79
+ @@extra.each do |from_extra, to_extra|
80
+ @m.title = from; @m.extra = from_extra; @m.permalink = nil
81
+ assert @m.valid?
82
+ assert_equal "#{to}-#{to_extra}", @m.permalink
83
+ end
84
+ end
85
+ end
86
+
87
+ def test_should_create_unique_permalink
88
+ @m = MockModel.new
89
+ @m.title = 'foo'
90
+ assert @m.valid?
91
+ assert_equal 'foo-2', @m.permalink
92
+
93
+ @m.title = 'bar'
94
+ @m.permalink = nil
95
+ assert @m.valid?
96
+ assert_equal 'bar-3', @m.permalink
97
+ end
98
+
99
+ def test_should_create_unique_permalink_when_assigned_directly
100
+ @m = MockModel.new
101
+ @m.permalink = 'foo'
102
+ assert @m.valid?
103
+ assert_equal 'foo-2', @m.permalink
104
+
105
+ # should always check itself for uniqueness when not respond_to?(:permalink_changed?)
106
+ @m.permalink = 'bar'
107
+ assert @m.valid?
108
+ assert_equal 'bar-3', @m.permalink
109
+ end
110
+
111
+ def test_should_common_permalink_if_unique_is_false
112
+ @m = CommonMockModel.new
113
+ @m.permalink = 'foo'
114
+ assert @m.valid?
115
+ assert_equal 'foo', @m.permalink
116
+ end
117
+
118
+ def test_should_not_check_itself_for_unique_permalink_if_unchanged
119
+ @m = MockModel.new
120
+ @m.id = 2
121
+ @m.permalink = 'bar-2'
122
+ @m.instance_eval do
123
+ @changed_attributes = {}
124
+ end
125
+ assert @m.valid?
126
+ assert_equal 'bar-2', @m.permalink
127
+ end
128
+
129
+ def test_should_check_itself_for_unique_permalink_if_permalink_field_changed
130
+ @m = PermalinkChangeableMockModel.new
131
+ @m.permalink_will_change!
132
+ @m.permalink = 'foo'
133
+ assert @m.valid?
134
+ assert_equal 'foo-2', @m.permalink
135
+ end
136
+
137
+ def test_should_not_check_itself_for_unique_permalink_if_permalink_field_not_changed
138
+ @m = PermalinkChangeableMockModel.new
139
+ @m.permalink = 'foo'
140
+ assert @m.valid?
141
+ assert_equal 'foo', @m.permalink
142
+ end
143
+
144
+ def test_should_create_unique_scoped_permalink
145
+ @m = ScopedModel.new
146
+ @m.permalink = 'foo'
147
+ assert @m.valid?
148
+ assert_equal 'foo-2', @m.permalink
149
+
150
+ @m.foo = 5
151
+ @m.permalink = 'foo'
152
+ assert @m.valid?
153
+ assert_equal 'foo', @m.permalink
154
+ end
155
+
156
+ def test_should_limit_permalink
157
+ @old = MockModel.columns_hash['permalink'].instance_variable_get(:@limit)
158
+ MockModel.columns_hash['permalink'].instance_variable_set(:@limit, 2)
159
+ @m = MockModel.new
160
+ @m.title = 'BOO'
161
+ assert @m.valid?
162
+ assert_equal 'bo', @m.permalink
163
+ ensure
164
+ MockModel.columns_hash['permalink'].instance_variable_set(:@limit, @old)
165
+ end
166
+
167
+ def test_should_limit_unique_permalink
168
+ @old = MockModel.columns_hash['permalink'].instance_variable_get(:@limit)
169
+ MockModel.columns_hash['permalink'].instance_variable_set(:@limit, 3)
170
+ @m = MockModel.new
171
+ @m.title = 'foo'
172
+ assert @m.valid?
173
+ assert_equal 'f-2', @m.permalink
174
+ ensure
175
+ MockModel.columns_hash['permalink'].instance_variable_set(:@limit, @old)
176
+ end
177
+
178
+ def test_should_abide_by_if_proc_condition
179
+ @m = IfProcConditionModel.new
180
+ @m.title = 'dont make me a permalink'
181
+ assert @m.valid?
182
+ assert_nil @m.permalink
183
+ end
184
+
185
+ def test_should_abide_by_if_method_condition
186
+ @m = IfMethodConditionModel.new
187
+ @m.title = 'dont make me a permalink'
188
+ assert @m.valid?
189
+ assert_nil @m.permalink
190
+ end
191
+
192
+ def test_should_abide_by_if_string_condition
193
+ @m = IfStringConditionModel.new
194
+ @m.title = 'dont make me a permalink'
195
+ assert @m.valid?
196
+ assert_nil @m.permalink
197
+ end
198
+
199
+ def test_should_abide_by_unless_proc_condition
200
+ @m = UnlessProcConditionModel.new
201
+ @m.title = 'make me a permalink'
202
+ assert @m.valid?
203
+ assert_not_nil @m.permalink
204
+ end
205
+
206
+ def test_should_abide_by_unless_method_condition
207
+ @m = UnlessMethodConditionModel.new
208
+ @m.title = 'make me a permalink'
209
+ assert @m.valid?
210
+ assert_not_nil @m.permalink
211
+ end
212
+
213
+ def test_should_abide_by_unless_string_condition
214
+ @m = UnlessStringConditionModel.new
215
+ @m.title = 'make me a permalink'
216
+ assert @m.valid?
217
+ assert_not_nil @m.permalink
218
+ end
219
+
220
+ def test_should_allow_override_of_permalink_method
221
+ @m = OverrideModel.new
222
+ @m[:permalink] = 'the permalink'
223
+ assert_not_equal @m.permalink, @m[:permalink]
224
+ end
225
+
226
+ def test_should_create_permalink_from_attribute_not_attribute_accessor
227
+ @m = OverrideModel.new
228
+ @m.title = 'the permalink'
229
+ assert @m.valid?
230
+ assert_equal 'the-permalink', @m[:permalink]
231
+ end
232
+
233
+ def test_should_not_update_permalink_unless_field_changed
234
+ @m = NoChangeModel.new
235
+ @m.title = 'the permalink'
236
+ @m.permalink = 'unchanged'
237
+ assert @m.valid?
238
+ assert_equal 'unchanged', @m[:permalink]
239
+ end
240
+
241
+ def test_should_not_update_permalink_without_update_set_even_if_field_changed
242
+ @m = ChangedWithoutUpdateModel.new
243
+ @m.title = 'the permalink'
244
+ @m.permalink = 'unchanged'
245
+ assert @m.valid?
246
+ assert_equal 'unchanged', @m[:permalink]
247
+ end
248
+
249
+ def test_should_update_permalink_if_changed_method_does_not_exist
250
+ @m = OverrideModel.new
251
+ @m.title = 'the permalink'
252
+ assert @m.valid?
253
+ assert_equal 'the-permalink', @m[:permalink]
254
+ end
255
+
256
+ def test_should_update_permalink_if_the_existing_permalink_is_nil
257
+ @m = NoChangeModel.new
258
+ @m.title = 'the permalink'
259
+ @m.permalink = nil
260
+ assert @m.valid?
261
+ assert_equal 'the-permalink', @m[:permalink]
262
+ end
263
+
264
+ def test_should_update_permalink_if_the_existing_permalink_is_blank
265
+ @m = NoChangeModel.new
266
+ @m.title = 'the permalink'
267
+ @m.permalink = ''
268
+ assert @m.valid?
269
+ assert_equal 'the-permalink', @m[:permalink]
270
+ end
271
+
272
+ def test_should_assign_a_random_permalink_if_the_title_is_nil
273
+ @m = NoChangeModel.new
274
+ @m.title = nil
275
+ assert @m.valid?
276
+ assert_not_nil @m[:permalink]
277
+ assert @m[:permalink].size > 0
278
+ end
279
+
280
+ def test_should_assign_a_random_permalink_if_the_title_has_no_permalinkable_characters
281
+ @m = NoChangeModel.new
282
+ @m.title = '////'
283
+ assert @m.valid?
284
+ assert_not_nil @m[:permalink]
285
+ assert @m[:permalink].size > 0
286
+ end
287
+
288
+ def test_should_update_permalink_the_first_time_the_title_is_set
289
+ @m = ChangedWithoutUpdateModel.new
290
+ @m.title = "old title"
291
+ assert @m.valid?
292
+ assert_equal "old-title", @m[:permalink]
293
+ @m.title = "new title"
294
+ assert @m.valid?
295
+ assert_equal "old-title", @m[:permalink]
296
+ end
297
+
298
+ def test_should_not_update_permalink_if_already_set_even_if_title_changed
299
+ @m = ChangedWithoutUpdateModel.new
300
+ @m.permalink = "old permalink"
301
+ @m.title = "new title"
302
+ assert @m.valid?
303
+ assert_equal "old-permalink", @m[:permalink]
304
+ end
305
+
306
+ def test_should_update_permalink_every_time_the_title_is_changed
307
+ @m = ChangedWithUpdateModel.new
308
+ @m.title = "old title"
309
+ assert @m.valid?
310
+ assert_equal "old-title", @m[:permalink]
311
+ @m.title = "new title"
312
+ assert @m.valid?
313
+ assert_equal "new-title", @m[:permalink]
314
+ end
315
+
316
+ def test_should_work_correctly_for_scoped_fields_with_nil_value
317
+ s1 = ScopedModelForNilScope.new
318
+ s1.title = 'ack'
319
+ s1.foo = 3
320
+ assert s1.valid?
321
+ assert_equal 'ack', s1.permalink
322
+
323
+ s2 = ScopedModelForNilScope.new
324
+ s2.title = 'ack'
325
+ s2.foo = nil
326
+ assert s2.valid?
327
+ assert_equal 'ack-2', s2.permalink
328
+ end
329
+ end
330
+
331
+ class BaseModel < ActiveRecord::Base
332
+ cattr_accessor :columns
333
+ @@columns ||= []
334
+
335
+ def self.column(name, sql_type = nil, default = nil, null = true)
336
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
337
+ end
338
+
339
+ def self.exists?(*args)
340
+ false
341
+ end
342
+
343
+ column :id, 'int(11)'
344
+ column :title, 'varchar(100)'
345
+ column :permalink, 'varchar(100)'
346
+ column :extra, 'varchar(100)'
347
+ column :foo, 'varchar(100)'
348
+
349
+ end
350
+
351
+ class ClassModel < BaseModel
352
+ has_permalink :title
353
+ end
354
+
355
+ class SubClassHasPermalinkModel < ClassModel
356
+ has_permalink [:title, :extra]
357
+ end
358
+
359
+ class SubClassNoPermalinkModel < ClassModel
360
+ end
361
+
362
+ class MockModel < BaseModel
363
+ def self.exists?(conditions)
364
+ if conditions[1] == 'foo' || conditions[1] == 'bar' ||
365
+ (conditions[1] == 'bar-2' && conditions[2] != 2)
366
+ true
367
+ else
368
+ false
369
+ end
370
+ end
371
+
372
+ has_permalink :title
373
+ end
374
+
375
+ class PermalinkChangeableMockModel < BaseModel
376
+ def self.exists?(conditions)
377
+ if conditions[1] == 'foo'
378
+ true
379
+ else
380
+ false
381
+ end
382
+ end
383
+
384
+ has_permalink :title
385
+
386
+ def permalink_changed?
387
+ @permalink_changed
388
+ end
389
+
390
+ def permalink_will_change!
391
+ @permalink_changed = true
392
+ end
393
+ end
394
+
395
+ class CommonMockModel < BaseModel
396
+ def self.exists?(conditions)
397
+ false # oh noes
398
+ end
399
+
400
+ has_permalink :title, :unique => false
401
+ end
402
+
403
+ class ScopedModel < BaseModel
404
+ def self.exists?(conditions)
405
+ if conditions[1] == 'foo' && conditions[2] != 5
406
+ true
407
+ else
408
+ false
409
+ end
410
+ end
411
+
412
+ has_permalink :title, :scope => :foo
413
+ end
414
+
415
+ class ScopedModelForNilScope < BaseModel
416
+ def self.exists?(conditions)
417
+ (conditions[0] == 'permalink = ? and foo IS NULL') ? (conditions[1] == 'ack') : false
418
+ end
419
+
420
+ has_permalink :title, :scope => :foo
421
+ end
422
+
423
+ class OverrideModel < BaseModel
424
+ has_permalink :title
425
+
426
+ def permalink
427
+ 'not the permalink'
428
+ end
429
+ end
430
+
431
+ class ChangedWithoutUpdateModel < BaseModel
432
+ has_permalink :title
433
+ def title_changed?; true; end
434
+ end
435
+
436
+ class ChangedWithUpdateModel < BaseModel
437
+ has_permalink :title, :update => true
438
+ def title_changed?; true; end
439
+ end
440
+
441
+ class NoChangeModel < BaseModel
442
+ has_permalink :title, :update => true
443
+ def title_changed?; false; end
444
+ end
445
+
446
+ class IfProcConditionModel < BaseModel
447
+ has_permalink :title, :if => Proc.new { |obj| false }
448
+ end
449
+
450
+ class IfMethodConditionModel < BaseModel
451
+ has_permalink :title, :if => :false_method
452
+
453
+ def false_method; false; end
454
+ end
455
+
456
+ class IfStringConditionModel < BaseModel
457
+ has_permalink :title, :if => 'false'
458
+ end
459
+
460
+ class UnlessProcConditionModel < BaseModel
461
+ has_permalink :title, :unless => Proc.new { |obj| false }
462
+ end
463
+
464
+ class UnlessMethodConditionModel < BaseModel
465
+ has_permalink :title, :unless => :false_method
466
+
467
+ def false_method; false; end
468
+ end
469
+
470
+ class UnlessStringConditionModel < BaseModel
471
+ has_permalink :title, :unless => 'false'
472
+ end
473
+
474
+ class MockModelExtra < BaseModel
475
+ has_permalink [:title, :extra]
476
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+
5
+ begin
6
+ require 'ruby-debug'
7
+ Debugger.start
8
+ rescue LoadError
9
+ # no ruby debugger
10
+ end
11
+
12
+ $:.unshift "#{File.dirname(__FILE__)}/../lib/"
13
+ require 'permalink_fu'
Binary file
Binary file