acts_as_list_with_sti_support 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +23 -0
- data/README.rdoc +62 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/init.rb +5 -0
- data/test/acts_as_list_with_sti_support_test.rb +595 -0
- data/test/test_helper.rb +26 -0
- metadata +80 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2010 Coroutine LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
The plugin is a modestly modified version of the rails acts_as_list
|
23
|
+
plugin released under the MIT license by David Heinemeier Hansson.
|
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= acts_as_list_with_sti_support
|
2
|
+
|
3
|
+
|
4
|
+
== Description
|
5
|
+
|
6
|
+
This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list.
|
7
|
+
The class that has this specified needs to have a +position+ column defined as an integer on the mapped
|
8
|
+
database table.
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
If you just want a global list, simply add acts_as_list to the model.
|
15
|
+
|
16
|
+
class ListItem < ActiveRecord::Base
|
17
|
+
acts_as_list
|
18
|
+
end
|
19
|
+
|
20
|
+
ListItem.first.move_to_bottom
|
21
|
+
ListItem.last.move_higher
|
22
|
+
|
23
|
+
|
24
|
+
The plugin accepts two optional parameters.
|
25
|
+
|
26
|
+
+column+:: specifies the name of the column to use for the position (default: position)
|
27
|
+
+scope+:: specifies the scope within which lists are ordered (default: 1 = 1, i.e., a global list)
|
28
|
+
|
29
|
+
class FunkyList < ActiveRecord::Base
|
30
|
+
has_many :funky_list_items
|
31
|
+
end
|
32
|
+
|
33
|
+
class FunkyListItem < ActiveRecord::Base
|
34
|
+
belongs_to :funky_list
|
35
|
+
acts_as_list :column => :pos, :scope => :funky_list
|
36
|
+
end
|
37
|
+
|
38
|
+
The +scope+ option can be supplied as a symbol or as a lambda. The generated scope condition
|
39
|
+
will differ depending on the value's Type and the schema of the associated table.
|
40
|
+
|
41
|
+
+scope => type_symbol+:: yields "type_id = '99'", if type_id is defined on the table
|
42
|
+
+scope => type_symbol+:: yields "type = '99'", if type_id is not defined on the table
|
43
|
+
+scope => lambda { |instance| "type_id = '#{instance.type_id}'"}+:: yields "type_id = '99'"
|
44
|
+
|
45
|
+
If you use the plugin within a single table inheritance (STI) design, no extra configuration is needed
|
46
|
+
to cause the position values to be scoped by type.
|
47
|
+
|
48
|
+
class Label < ActiveRecord::Base
|
49
|
+
acts_as_list
|
50
|
+
end
|
51
|
+
|
52
|
+
class BillingFrequency < Label
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
== License
|
58
|
+
|
59
|
+
Copyright (c) 2010 Coroutine LLC, released under the MIT license
|
60
|
+
|
61
|
+
The plugin is a modestly modified version of the rails acts_as_list plugin released under the
|
62
|
+
MIT license by David Heinemeier Hansson.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'jeweler'
|
5
|
+
|
6
|
+
|
7
|
+
desc 'Default: run tests.'
|
8
|
+
task :default => [:test]
|
9
|
+
|
10
|
+
|
11
|
+
desc 'Test the plugin.'
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << 'lib'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = true
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
desc 'Generate documentation for the plugin.'
|
20
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
21
|
+
rdoc.rdoc_dir = 'rdoc'
|
22
|
+
rdoc.title = 'acts_as_list_with_sti_support'
|
23
|
+
rdoc.options << '--line-numbers --inline-source'
|
24
|
+
rdoc.rdoc_files.include('README')
|
25
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
begin
|
30
|
+
Jeweler::Tasks.new do |gemspec|
|
31
|
+
gemspec.name = "acts_as_list_with_sti_support"
|
32
|
+
gemspec.summary = "Gem version of acts_as_list_with_sti_support Rails plugin, a smarter version of acts_as_list."
|
33
|
+
gemspec.description = "This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a position column defined as an integer on the mapped database table."
|
34
|
+
gemspec.email = "jdugan@coroutine.com"
|
35
|
+
gemspec.homepage = "http://github.com/coroutine/acts_as_label_with_sti_support"
|
36
|
+
gemspec.authors = ["Coroutine", "John Dugan"]
|
37
|
+
gemspec.add_dependency "activesupport"
|
38
|
+
end
|
39
|
+
Jeweler::GemcutterTasks.new
|
40
|
+
rescue LoadError
|
41
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
42
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1,595 @@
|
|
1
|
+
#---------------------------------------------------------
|
2
|
+
# Requirements
|
3
|
+
#---------------------------------------------------------
|
4
|
+
|
5
|
+
# all generic requirements are in the helper
|
6
|
+
require "test/test_helper"
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
#---------------------------------------------------------
|
11
|
+
# Database config
|
12
|
+
#---------------------------------------------------------
|
13
|
+
|
14
|
+
# establish db connection
|
15
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
16
|
+
|
17
|
+
|
18
|
+
# define tables
|
19
|
+
def setup_db
|
20
|
+
ActiveRecord::Schema.define(:version => 1) do
|
21
|
+
create_table :contacts do |t|
|
22
|
+
t.string :name
|
23
|
+
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :emails do |t|
|
28
|
+
t.integer :contact_id
|
29
|
+
t.string :address
|
30
|
+
t.integer :pos
|
31
|
+
|
32
|
+
t.timestamps
|
33
|
+
end
|
34
|
+
|
35
|
+
create_table :phones do |t|
|
36
|
+
t.integer :contact_id
|
37
|
+
t.string :number
|
38
|
+
t.integer :position
|
39
|
+
|
40
|
+
t.timestamps
|
41
|
+
end
|
42
|
+
|
43
|
+
create_table :websites do |t|
|
44
|
+
t.integer :contact_id
|
45
|
+
t.string :address
|
46
|
+
t.integer :position
|
47
|
+
|
48
|
+
t.timestamps
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table :labels do |t|
|
52
|
+
t.string :type
|
53
|
+
t.string :label
|
54
|
+
t.integer :position
|
55
|
+
|
56
|
+
t.timestamps
|
57
|
+
end
|
58
|
+
|
59
|
+
Contact.create!({ :name => "John Dugan" })
|
60
|
+
Contact.create!({ :name => "Tim Lowrimore" })
|
61
|
+
|
62
|
+
Email.create!({ :contact_id => Contact.first.id, :address => "jdugan@coroutine.com" })
|
63
|
+
Email.create!({ :contact_id => Contact.first.id, :address => "jdugan@example.com" })
|
64
|
+
Email.create!({ :contact_id => Contact.last.id, :address => "tlowrimore@coroutine.com" })
|
65
|
+
Email.create!({ :contact_id => Contact.last.id, :address => "tlowrimore@example.com" })
|
66
|
+
|
67
|
+
Phone.create!({ :contact_id => Contact.first.id, :number => "901.555.1111" })
|
68
|
+
Phone.create!({ :contact_id => Contact.first.id, :number => "901.555.2222" })
|
69
|
+
Phone.create!({ :contact_id => Contact.first.id, :number => "901.555.3333" })
|
70
|
+
Phone.create!({ :contact_id => Contact.first.id, :number => "901.555.4444" })
|
71
|
+
Phone.create!({ :contact_id => Contact.last.id, :number => "901.555.5555" })
|
72
|
+
Phone.create!({ :contact_id => Contact.last.id, :number => "901.555.6666" })
|
73
|
+
|
74
|
+
Website.create!({ :contact_id => Contact.first.id, :address => "http://coroutine.com" })
|
75
|
+
Website.create!({ :contact_id => Contact.first.id, :address => "http://johndugan.me" })
|
76
|
+
Website.create!({ :contact_id => Contact.last.id, :address => "http://coroutine.com" })
|
77
|
+
Website.create!({ :contact_id => Contact.last.id, :address => "http://timlowrimore.me" })
|
78
|
+
|
79
|
+
BillingFrequency.create({ :label => "Weekly" })
|
80
|
+
BillingFrequency.create({ :label => "Monthly" })
|
81
|
+
BillingFrequency.create({ :label => "Quarterly" })
|
82
|
+
BillingFrequency.create({ :label => "Yearly" })
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
# drop all tables
|
88
|
+
def teardown_db
|
89
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
90
|
+
ActiveRecord::Base.connection.drop_table(table)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
#---------------------------------------------------------
|
97
|
+
# Model definitions
|
98
|
+
#---------------------------------------------------------
|
99
|
+
|
100
|
+
# Contacts
|
101
|
+
class Contact < ActiveRecord::Base
|
102
|
+
has_many :emails
|
103
|
+
has_many :phones
|
104
|
+
has_many :websites
|
105
|
+
|
106
|
+
def email_ids
|
107
|
+
self.emails(true).map(&:id)
|
108
|
+
end
|
109
|
+
def phone_ids
|
110
|
+
self.phones(true).map(&:id)
|
111
|
+
end
|
112
|
+
def website_ids
|
113
|
+
self.websites(true).map(&:id)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
# Emails
|
119
|
+
class Email < ActiveRecord::Base
|
120
|
+
belongs_to :contact
|
121
|
+
|
122
|
+
acts_as_list :column => :pos, :scope => :contact
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
# Phones
|
127
|
+
class Phone < ActiveRecord::Base
|
128
|
+
belongs_to :contact
|
129
|
+
|
130
|
+
acts_as_list :scope => :contact_id
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
# Websites
|
135
|
+
class Website < ActiveRecord::Base
|
136
|
+
belongs_to :contact
|
137
|
+
|
138
|
+
acts_as_list :scope => lambda { |instance| "contact_id = '#{instance.contact_id}'" }
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
# Labels (STI Base)
|
143
|
+
class Label < ActiveRecord::Base
|
144
|
+
acts_as_list
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
# Billing Frequency (STI extension)
|
149
|
+
class BillingFrequency < Label
|
150
|
+
def self.ids
|
151
|
+
BillingFrequency.find(:all, :order => :position).map(&:id)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# Tax Frequency (STI extension)
|
157
|
+
class TaxFrequency < Label
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
# Payment Frequency (STI extension)
|
162
|
+
class PaymentFrequency < Label
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
#---------------------------------------------------------
|
168
|
+
# Standard Tests
|
169
|
+
#---------------------------------------------------------
|
170
|
+
|
171
|
+
class ActsAsListTest < ActiveSupport::TestCase
|
172
|
+
|
173
|
+
|
174
|
+
#---------------------------------------------
|
175
|
+
# setup and teardown delegations
|
176
|
+
#---------------------------------------------
|
177
|
+
|
178
|
+
def setup
|
179
|
+
setup_db
|
180
|
+
end
|
181
|
+
def teardown
|
182
|
+
teardown_db
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
#---------------------------------------------
|
187
|
+
# test validations
|
188
|
+
#---------------------------------------------
|
189
|
+
|
190
|
+
def test_validations
|
191
|
+
|
192
|
+
# get valid record
|
193
|
+
record = BillingFrequency.first
|
194
|
+
assert record.valid?
|
195
|
+
|
196
|
+
# position can be null
|
197
|
+
record.position = nil
|
198
|
+
assert record.valid?
|
199
|
+
|
200
|
+
# position cannot be string
|
201
|
+
record.position = "one"
|
202
|
+
assert !record.valid?
|
203
|
+
|
204
|
+
# position cannot be decimal
|
205
|
+
record.position = 2.25
|
206
|
+
assert !record.valid?
|
207
|
+
|
208
|
+
# position cannot be less than 1
|
209
|
+
record.position = -2
|
210
|
+
assert !record.valid?
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
#---------------------------------------------
|
216
|
+
# test class references
|
217
|
+
#---------------------------------------------
|
218
|
+
|
219
|
+
def test_acts_as_list_class
|
220
|
+
|
221
|
+
# standard class
|
222
|
+
record = Email.first
|
223
|
+
assert_equal record.acts_as_list_class, Email
|
224
|
+
|
225
|
+
# sti class
|
226
|
+
record = BillingFrequency.first
|
227
|
+
assert_equal record.acts_as_list_class, BillingFrequency
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
#---------------------------------------------
|
233
|
+
# test position columns
|
234
|
+
#---------------------------------------------
|
235
|
+
|
236
|
+
def test_position_columns
|
237
|
+
|
238
|
+
# override name
|
239
|
+
record = Email.first
|
240
|
+
assert_equal record.position_column, :pos
|
241
|
+
|
242
|
+
# standard name
|
243
|
+
record = Phone.first
|
244
|
+
assert_equal record.position_column, :position
|
245
|
+
|
246
|
+
# standard name (sti)
|
247
|
+
record = BillingFrequency.first
|
248
|
+
assert_equal record.position_column, :position
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
|
253
|
+
#---------------------------------------------
|
254
|
+
# test scope conditions
|
255
|
+
#---------------------------------------------
|
256
|
+
|
257
|
+
def test_scope_conditions
|
258
|
+
|
259
|
+
# implicit _id
|
260
|
+
record = Email.first
|
261
|
+
assert_equal record.scope_condition, "contact_id = '#{record.contact_id}'"
|
262
|
+
|
263
|
+
# explicit _id
|
264
|
+
record = Phone.first
|
265
|
+
assert_equal record.scope_condition, "contact_id = '#{record.contact_id}'"
|
266
|
+
|
267
|
+
# explicit string
|
268
|
+
record = Website.first
|
269
|
+
assert_equal record.scope_condition, "contact_id = '#{record.contact_id}'"
|
270
|
+
|
271
|
+
# sti
|
272
|
+
record = BillingFrequency.first
|
273
|
+
assert_equal record.scope_condition, "1 = 1"
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
#---------------------------------------------
|
280
|
+
# test ordering methods
|
281
|
+
#---------------------------------------------
|
282
|
+
|
283
|
+
def test_reordering
|
284
|
+
|
285
|
+
# standard model
|
286
|
+
contact = Contact.first
|
287
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
288
|
+
|
289
|
+
Phone.find(2).move_lower
|
290
|
+
assert_equal [1, 3, 2, 4], contact.phone_ids
|
291
|
+
|
292
|
+
Phone.find(2).move_higher
|
293
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
294
|
+
|
295
|
+
Phone.find(1).move_to_bottom
|
296
|
+
assert_equal [2, 3, 4, 1], contact.phone_ids
|
297
|
+
|
298
|
+
Phone.find(1).move_to_top
|
299
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
300
|
+
|
301
|
+
Phone.find(2).move_to_bottom
|
302
|
+
assert_equal [1, 3, 4, 2], contact.phone_ids
|
303
|
+
|
304
|
+
Phone.find(4).move_to_top
|
305
|
+
assert_equal [4, 1, 3, 2], contact.phone_ids
|
306
|
+
|
307
|
+
|
308
|
+
# sti model
|
309
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
310
|
+
|
311
|
+
BillingFrequency.find(2).move_lower
|
312
|
+
assert_equal [1, 3, 2, 4], BillingFrequency.ids
|
313
|
+
|
314
|
+
BillingFrequency.find(2).move_higher
|
315
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
316
|
+
|
317
|
+
BillingFrequency.find(1).move_to_bottom
|
318
|
+
assert_equal [2, 3, 4, 1], BillingFrequency.ids
|
319
|
+
|
320
|
+
BillingFrequency.find(1).move_to_top
|
321
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
322
|
+
|
323
|
+
BillingFrequency.find(2).move_to_bottom
|
324
|
+
assert_equal [1, 3, 4, 2], BillingFrequency.ids
|
325
|
+
|
326
|
+
BillingFrequency.find(4).move_to_top
|
327
|
+
assert_equal [4, 1, 3, 2], BillingFrequency.ids
|
328
|
+
end
|
329
|
+
|
330
|
+
|
331
|
+
def test_move_to_bottom_with_next_to_last_item
|
332
|
+
|
333
|
+
# standard model
|
334
|
+
contact = Contact.first
|
335
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
336
|
+
|
337
|
+
Phone.find(3).move_to_bottom
|
338
|
+
assert_equal [1, 2, 4, 3], contact.phone_ids
|
339
|
+
|
340
|
+
|
341
|
+
# sti model
|
342
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
343
|
+
|
344
|
+
BillingFrequency.find(3).move_to_bottom
|
345
|
+
assert_equal [1, 2, 4, 3], BillingFrequency.ids
|
346
|
+
end
|
347
|
+
|
348
|
+
|
349
|
+
def test_next_prev
|
350
|
+
|
351
|
+
# standard model
|
352
|
+
assert_equal Phone.find(2), Phone.find(1).lower_item
|
353
|
+
assert_nil Phone.find(1).higher_item
|
354
|
+
assert_equal Phone.find(3), Phone.find(4).higher_item
|
355
|
+
assert_nil Phone.find(4).lower_item
|
356
|
+
|
357
|
+
# sti model
|
358
|
+
assert_equal BillingFrequency.find(2), BillingFrequency.find(1).lower_item
|
359
|
+
assert_nil BillingFrequency.find(1).higher_item
|
360
|
+
assert_equal BillingFrequency.find(3), BillingFrequency.find(4).higher_item
|
361
|
+
assert_nil BillingFrequency.find(4).lower_item
|
362
|
+
end
|
363
|
+
|
364
|
+
|
365
|
+
def test_insert
|
366
|
+
|
367
|
+
# standard model
|
368
|
+
new = Phone.create(:contact_id => 3, :number => "901.555.7777")
|
369
|
+
assert_equal 1, new.position
|
370
|
+
assert new.first?
|
371
|
+
assert new.last?
|
372
|
+
|
373
|
+
new = Phone.create(:contact_id => 3, :number => "901.555.8888")
|
374
|
+
assert_equal 2, new.position
|
375
|
+
assert !new.first?
|
376
|
+
assert new.last?
|
377
|
+
|
378
|
+
new = Phone.create(:contact_id => 3, :number => "901.555.9999")
|
379
|
+
assert_equal 3, new.position
|
380
|
+
assert !new.first?
|
381
|
+
assert new.last?
|
382
|
+
|
383
|
+
new = Phone.create(:contact_id => 4, :number => "901.555.0000")
|
384
|
+
assert_equal 1, new.position
|
385
|
+
assert new.first?
|
386
|
+
assert new.last?
|
387
|
+
|
388
|
+
|
389
|
+
# sti model
|
390
|
+
new = TaxFrequency.create(:label => "Monthly")
|
391
|
+
assert_equal 1, new.position
|
392
|
+
assert new.first?
|
393
|
+
assert new.last?
|
394
|
+
|
395
|
+
new = TaxFrequency.create(:label => "Quarterly")
|
396
|
+
assert_equal 2, new.position
|
397
|
+
assert !new.first?
|
398
|
+
assert new.last?
|
399
|
+
|
400
|
+
new = TaxFrequency.create(:label => "Yearly")
|
401
|
+
assert_equal 3, new.position
|
402
|
+
assert !new.first?
|
403
|
+
assert new.last?
|
404
|
+
|
405
|
+
new = PaymentFrequency.create(:label => "Monthly")
|
406
|
+
assert_equal 1, new.position
|
407
|
+
assert new.first?
|
408
|
+
assert new.last?
|
409
|
+
end
|
410
|
+
|
411
|
+
|
412
|
+
def test_insert_at
|
413
|
+
|
414
|
+
# stadard model
|
415
|
+
new = Phone.create(:contact_id => 3, :number => "615.555.1111")
|
416
|
+
assert_equal 1, new.position
|
417
|
+
|
418
|
+
new = Phone.create(:contact_id => 3, :number => "615.555.2222")
|
419
|
+
assert_equal 2, new.position
|
420
|
+
|
421
|
+
new = Phone.create(:contact_id => 3, :number => "615.555.3333")
|
422
|
+
assert_equal 3, new.position
|
423
|
+
|
424
|
+
new4 = Phone.create(:contact_id => 3, :number => "615.555.4444")
|
425
|
+
assert_equal 4, new4.position
|
426
|
+
|
427
|
+
new4.insert_at(3)
|
428
|
+
assert_equal 3, new4.position
|
429
|
+
|
430
|
+
new.reload
|
431
|
+
assert_equal 4, new.position
|
432
|
+
|
433
|
+
new.insert_at(2)
|
434
|
+
assert_equal 2, new.position
|
435
|
+
|
436
|
+
new4.reload
|
437
|
+
assert_equal 4, new4.position
|
438
|
+
|
439
|
+
new5 = Phone.create(:contact_id => 3, :number => "615.555.5555")
|
440
|
+
assert_equal 5, new5.position
|
441
|
+
|
442
|
+
new5.insert_at(1)
|
443
|
+
assert_equal 1, new5.position
|
444
|
+
|
445
|
+
new4.reload
|
446
|
+
assert_equal 5, new4.position
|
447
|
+
|
448
|
+
|
449
|
+
# sti model
|
450
|
+
new = TaxFrequency.create(:label => "Weekly")
|
451
|
+
assert_equal 1, new.position
|
452
|
+
|
453
|
+
new = TaxFrequency.create(:label => "Monthly")
|
454
|
+
assert_equal 2, new.position
|
455
|
+
|
456
|
+
new = TaxFrequency.create(:label => "Quarterly")
|
457
|
+
assert_equal 3, new.position
|
458
|
+
|
459
|
+
new4 = TaxFrequency.create(:label => "Yearly")
|
460
|
+
assert_equal 4, new4.position
|
461
|
+
|
462
|
+
new4.insert_at(3)
|
463
|
+
assert_equal 3, new4.position
|
464
|
+
|
465
|
+
new.reload
|
466
|
+
assert_equal 4, new.position
|
467
|
+
|
468
|
+
new.insert_at(2)
|
469
|
+
assert_equal 2, new.position
|
470
|
+
|
471
|
+
new4.reload
|
472
|
+
assert_equal 4, new4.position
|
473
|
+
|
474
|
+
new5 = TaxFrequency.create(:label => "Daily")
|
475
|
+
assert_equal 5, new5.position
|
476
|
+
|
477
|
+
new5.insert_at(1)
|
478
|
+
assert_equal 1, new5.position
|
479
|
+
|
480
|
+
new4.reload
|
481
|
+
assert_equal 5, new4.position
|
482
|
+
end
|
483
|
+
|
484
|
+
|
485
|
+
def test_delete_middle
|
486
|
+
|
487
|
+
# standard model
|
488
|
+
contact = Contact.first
|
489
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
490
|
+
|
491
|
+
Phone.find(2).destroy
|
492
|
+
assert_equal [1, 3, 4], contact.phone_ids
|
493
|
+
|
494
|
+
assert_equal 1, Phone.find(1).position
|
495
|
+
assert_equal 2, Phone.find(3).position
|
496
|
+
assert_equal 3, Phone.find(4).position
|
497
|
+
|
498
|
+
Phone.find(1).destroy
|
499
|
+
assert_equal [3, 4], contact.phone_ids
|
500
|
+
|
501
|
+
assert_equal 1, Phone.find(3).position
|
502
|
+
assert_equal 2, Phone.find(4).position
|
503
|
+
|
504
|
+
|
505
|
+
# sti model
|
506
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
507
|
+
|
508
|
+
BillingFrequency.find(2).destroy
|
509
|
+
assert_equal [1, 3, 4], BillingFrequency.ids
|
510
|
+
|
511
|
+
assert_equal 1, BillingFrequency.find(1).position
|
512
|
+
assert_equal 2, BillingFrequency.find(3).position
|
513
|
+
assert_equal 3, BillingFrequency.find(4).position
|
514
|
+
|
515
|
+
BillingFrequency.find(1).destroy
|
516
|
+
assert_equal [3, 4], BillingFrequency.ids
|
517
|
+
|
518
|
+
assert_equal 1, BillingFrequency.find(3).position
|
519
|
+
assert_equal 2, BillingFrequency.find(4).position
|
520
|
+
end
|
521
|
+
|
522
|
+
|
523
|
+
def test_remove_from_list_should_then_fail_in_list?
|
524
|
+
|
525
|
+
# standard model
|
526
|
+
assert_equal true, Phone.find(1).in_list?
|
527
|
+
|
528
|
+
Phone.find(1).remove_from_list
|
529
|
+
assert_equal false, Phone.find(1).in_list?
|
530
|
+
|
531
|
+
|
532
|
+
# sti model
|
533
|
+
assert_equal true, BillingFrequency.find(1).in_list?
|
534
|
+
|
535
|
+
BillingFrequency.find(1).remove_from_list
|
536
|
+
assert_equal false, BillingFrequency.find(1).in_list?
|
537
|
+
end
|
538
|
+
|
539
|
+
|
540
|
+
def test_remove_from_list_should_set_position_to_nil
|
541
|
+
|
542
|
+
# standard model
|
543
|
+
contact = Contact.first
|
544
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
545
|
+
|
546
|
+
Phone.find(2).remove_from_list
|
547
|
+
assert_equal [2, 1, 3, 4], contact.phone_ids
|
548
|
+
|
549
|
+
assert_equal 1, Phone.find(1).position
|
550
|
+
assert_equal nil, Phone.find(2).position
|
551
|
+
assert_equal 2, Phone.find(3).position
|
552
|
+
assert_equal 3, Phone.find(4).position
|
553
|
+
|
554
|
+
|
555
|
+
# sti model
|
556
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
557
|
+
|
558
|
+
BillingFrequency.find(2).remove_from_list
|
559
|
+
assert_equal [2, 1, 3, 4], BillingFrequency.ids
|
560
|
+
|
561
|
+
assert_equal 1, BillingFrequency.find(1).position
|
562
|
+
assert_equal nil, BillingFrequency.find(2).position
|
563
|
+
assert_equal 2, BillingFrequency.find(3).position
|
564
|
+
assert_equal 3, BillingFrequency.find(4).position
|
565
|
+
end
|
566
|
+
|
567
|
+
|
568
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
569
|
+
|
570
|
+
# standard model
|
571
|
+
contact = Contact.first
|
572
|
+
assert_equal [1, 2, 3, 4], contact.phone_ids
|
573
|
+
|
574
|
+
Phone.find(2).remove_from_list
|
575
|
+
Phone.find(2).destroy
|
576
|
+
assert_equal [1, 3, 4], contact.phone_ids
|
577
|
+
|
578
|
+
assert_equal 1, Phone.find(1).position
|
579
|
+
assert_equal 2, Phone.find(3).position
|
580
|
+
assert_equal 3, Phone.find(4).position
|
581
|
+
|
582
|
+
|
583
|
+
# sti model
|
584
|
+
assert_equal [1, 2, 3, 4], BillingFrequency.ids
|
585
|
+
|
586
|
+
BillingFrequency.find(2).remove_from_list
|
587
|
+
BillingFrequency.find(2).destroy
|
588
|
+
assert_equal [1, 3, 4], BillingFrequency.ids
|
589
|
+
|
590
|
+
assert_equal 1, BillingFrequency.find(1).position
|
591
|
+
assert_equal 2, BillingFrequency.find(3).position
|
592
|
+
assert_equal 3, BillingFrequency.find(4).position
|
593
|
+
end
|
594
|
+
|
595
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# require rails stuff
|
2
|
+
require "rubygems"
|
3
|
+
require "active_record"
|
4
|
+
require "active_support"
|
5
|
+
require "active_support/test_case"
|
6
|
+
require "test/unit"
|
7
|
+
|
8
|
+
# require plugin
|
9
|
+
require "#{File.dirname(__FILE__)}/../init"
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#----------------------------------------------------------
|
14
|
+
# Define global methods
|
15
|
+
#----------------------------------------------------------
|
16
|
+
|
17
|
+
class ActiveSupport::TestCase
|
18
|
+
|
19
|
+
# This method allows us to use a convenient notation for testing
|
20
|
+
# model validations.
|
21
|
+
def assert_not_valid(object, msg="Object is valid when it should be invalid")
|
22
|
+
assert(!object.valid?, msg)
|
23
|
+
end
|
24
|
+
alias :assert_invalid :assert_not_valid
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_list_with_sti_support
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Coroutine
|
13
|
+
- John Dugan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-03-10 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activesupport
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
description: This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a position column defined as an integer on the mapped database table.
|
34
|
+
email: jdugan@coroutine.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README.rdoc
|
41
|
+
files:
|
42
|
+
- MIT-LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
- Rakefile
|
45
|
+
- VERSION
|
46
|
+
- init.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/coroutine/acts_as_label_with_sti_support
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Gem version of acts_as_list_with_sti_support Rails plugin, a smarter version of acts_as_list.
|
78
|
+
test_files:
|
79
|
+
- test/acts_as_list_with_sti_support_test.rb
|
80
|
+
- test/test_helper.rb
|