census 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +5 -0
- data/Rakefile +70 -0
- data/VERSION +1 -0
- data/app/controllers/census/data_groups_controller.rb +44 -0
- data/app/helpers/census_helper.rb +15 -0
- data/app/models/answer.rb +43 -0
- data/app/models/choice.rb +9 -0
- data/app/models/data_group.rb +10 -0
- data/app/models/question.rb +54 -0
- data/app/views/census/_question_fields.html.erb +40 -0
- data/app/views/census/_user_answers.html.erb +10 -0
- data/app/views/census/_user_questions.html.erb +10 -0
- data/app/views/census/data_groups/_choice_fields.html.erb +4 -0
- data/app/views/census/data_groups/_form.html.erb +11 -0
- data/app/views/census/data_groups/_question_fields.html.erb +22 -0
- data/app/views/census/data_groups/edit.html.erb +7 -0
- data/app/views/census/data_groups/index.html.erb +10 -0
- data/app/views/census/data_groups/new.html.erb +7 -0
- data/config/routes.rb +8 -0
- data/generators/census/USAGE +1 -0
- data/generators/census/census_generator.rb +39 -0
- data/generators/census/lib/insert_commands.rb +33 -0
- data/generators/census/lib/rake_commands.rb +22 -0
- data/generators/census/templates/README +29 -0
- data/generators/census/templates/census.js +10 -0
- data/generators/census/templates/census.rb +14 -0
- data/generators/census/templates/factories.rb +24 -0
- data/generators/census/templates/migrations/with_users.rb +60 -0
- data/generators/census/templates/migrations/without_users.rb +54 -0
- data/generators/census/templates/user.rb +3 -0
- data/lib/census/data_type.rb +33 -0
- data/lib/census/user.rb +85 -0
- data/lib/census.rb +1 -0
- data/rails/init.rb +7 -0
- data/shoulda_macros/census.rb +35 -0
- data/test/controllers/data_groups_controller_test.rb +165 -0
- data/test/models/answer_test.rb +44 -0
- data/test/models/choice_test.rb +24 -0
- data/test/models/data_group_test.rb +12 -0
- data/test/models/question_test.rb +213 -0
- data/test/models/user_test.rb +9 -0
- data/test/rails_root/app/controllers/application_controller.rb +10 -0
- data/test/rails_root/app/helpers/application_helper.rb +3 -0
- data/test/rails_root/app/models/user.rb +3 -0
- data/test/rails_root/config/boot.rb +110 -0
- data/test/rails_root/config/environment.rb +8 -0
- data/test/rails_root/config/environments/development.rb +17 -0
- data/test/rails_root/config/environments/production.rb +28 -0
- data/test/rails_root/config/environments/test.rb +32 -0
- data/test/rails_root/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_root/config/initializers/census.rb +14 -0
- data/test/rails_root/config/initializers/inflections.rb +10 -0
- data/test/rails_root/config/initializers/mime_types.rb +5 -0
- data/test/rails_root/config/initializers/new_rails_defaults.rb +21 -0
- data/test/rails_root/config/initializers/session_store.rb +15 -0
- data/test/rails_root/config/routes.rb +43 -0
- data/test/rails_root/db/migrate/20100406160306_create_census_tables.rb +60 -0
- data/test/rails_root/db/schema.rb +58 -0
- data/test/rails_root/test/factories/census.rb +24 -0
- data/test/rails_root/test/performance/browsing_test.rb +9 -0
- data/test/rails_root/test/test_helper.rb +38 -0
- data/test/rails_root/vendor/gems/acts_as_list-0.1.2/lib/acts_as_list.rb +254 -0
- data/test/rails_root/vendor/gems/acts_as_list-0.1.2/test/list_test.rb +369 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/install.rb +1 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/lib/inverse_of.rb +293 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/rails/init.rb +1 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/cases/helper.rb +28 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/cases/inverse_associations_test.rb +567 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/club.rb +13 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/face.rb +7 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/interest.rb +5 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/man.rb +9 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/sponsor.rb +4 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/zine.rb +3 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/schema/schema.rb +32 -0
- data/test/rails_root/vendor/gems/inverse_of-0.0.1/uninstall.rb +1 -0
- data/test/test_helper.rb +19 -0
- metadata +190 -0
@@ -0,0 +1,254 @@
|
|
1
|
+
module ActsAsList
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
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
|
8
|
+
# the mapped database table.
|
9
|
+
#
|
10
|
+
# Todo list example:
|
11
|
+
#
|
12
|
+
# class TodoList < ActiveRecord::Base
|
13
|
+
# has_many :todo_items, :order => "position"
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# class TodoItem < ActiveRecord::Base
|
17
|
+
# belongs_to :todo_list
|
18
|
+
# acts_as_list :scope => :todo_list
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# todo_list.first.move_to_bottom
|
22
|
+
# todo_list.last.move_higher
|
23
|
+
module ClassMethods
|
24
|
+
# Configuration options are:
|
25
|
+
#
|
26
|
+
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
|
27
|
+
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
|
28
|
+
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
|
29
|
+
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
|
30
|
+
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
|
31
|
+
def acts_as_list(options = {})
|
32
|
+
configuration = { :column => "position", :scope => "1 = 1" }
|
33
|
+
configuration.update(options) if options.is_a?(Hash)
|
34
|
+
|
35
|
+
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
36
|
+
|
37
|
+
if configuration[:scope].is_a?(Symbol)
|
38
|
+
scope_condition_method = %(
|
39
|
+
def scope_condition
|
40
|
+
if #{configuration[:scope].to_s}.nil?
|
41
|
+
"#{configuration[:scope].to_s} IS NULL"
|
42
|
+
else
|
43
|
+
"#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
)
|
47
|
+
else
|
48
|
+
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
|
49
|
+
end
|
50
|
+
|
51
|
+
class_eval <<-EOV
|
52
|
+
include ActsAsList::InstanceMethods
|
53
|
+
|
54
|
+
def acts_as_list_class
|
55
|
+
::#{self.name}
|
56
|
+
end
|
57
|
+
|
58
|
+
def position_column
|
59
|
+
'#{configuration[:column]}'
|
60
|
+
end
|
61
|
+
|
62
|
+
#{scope_condition_method}
|
63
|
+
|
64
|
+
before_destroy :remove_from_list
|
65
|
+
before_create :add_to_list_bottom
|
66
|
+
EOV
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works
|
71
|
+
# by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter
|
72
|
+
# lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is
|
73
|
+
# the first in the list of all chapters.
|
74
|
+
module InstanceMethods
|
75
|
+
# Insert the item at the given position (defaults to the top position of 1).
|
76
|
+
def insert_at(position = 1)
|
77
|
+
insert_at_position(position)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Swap positions with the next lower item, if one exists.
|
81
|
+
def move_lower
|
82
|
+
lower = lower_item
|
83
|
+
return unless lower
|
84
|
+
acts_as_list_class.transaction do
|
85
|
+
self.update_attribute(position_column, lower.send(position_column))
|
86
|
+
lower.decrement_position
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Swap positions with the next higher item, if one exists.
|
91
|
+
def move_higher
|
92
|
+
higher = higher_item
|
93
|
+
return unless higher
|
94
|
+
acts_as_list_class.transaction do
|
95
|
+
self.update_attribute(position_column, higher.send(position_column))
|
96
|
+
higher.increment_position
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Move to the bottom of the list. If the item is already in the list, the items below it have their
|
101
|
+
# position adjusted accordingly.
|
102
|
+
def move_to_bottom
|
103
|
+
return unless in_list?
|
104
|
+
acts_as_list_class.transaction do
|
105
|
+
decrement_positions_on_lower_items
|
106
|
+
assume_bottom_position
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Move to the top of the list. If the item is already in the list, the items above it have their
|
111
|
+
# position adjusted accordingly.
|
112
|
+
def move_to_top
|
113
|
+
return unless in_list?
|
114
|
+
acts_as_list_class.transaction do
|
115
|
+
increment_positions_on_higher_items
|
116
|
+
assume_top_position
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Removes the item from the list.
|
121
|
+
def remove_from_list
|
122
|
+
if in_list?
|
123
|
+
decrement_positions_on_lower_items
|
124
|
+
update_attribute position_column, nil
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Increase the position of this item without adjusting the rest of the list.
|
129
|
+
def increment_position
|
130
|
+
return unless in_list?
|
131
|
+
update_attribute position_column, self.send(position_column).to_i + 1
|
132
|
+
end
|
133
|
+
|
134
|
+
# Decrease the position of this item without adjusting the rest of the list.
|
135
|
+
def decrement_position
|
136
|
+
return unless in_list?
|
137
|
+
update_attribute position_column, self.send(position_column).to_i - 1
|
138
|
+
end
|
139
|
+
|
140
|
+
# Return +true+ if this object is the first in the list.
|
141
|
+
def first?
|
142
|
+
return false unless in_list?
|
143
|
+
self.send(position_column) == 1
|
144
|
+
end
|
145
|
+
|
146
|
+
# Return +true+ if this object is the last in the list.
|
147
|
+
def last?
|
148
|
+
return false unless in_list?
|
149
|
+
self.send(position_column) == bottom_position_in_list
|
150
|
+
end
|
151
|
+
|
152
|
+
# Return the next higher item in the list.
|
153
|
+
def higher_item
|
154
|
+
return nil unless in_list?
|
155
|
+
acts_as_list_class.find(:first, :conditions =>
|
156
|
+
"#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC"
|
157
|
+
)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Return the next lower item in the list.
|
161
|
+
def lower_item
|
162
|
+
return nil unless in_list?
|
163
|
+
acts_as_list_class.find(:first, :conditions =>
|
164
|
+
"#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC"
|
165
|
+
)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Test if this record is in a list
|
169
|
+
def in_list?
|
170
|
+
!send(position_column).nil?
|
171
|
+
end
|
172
|
+
|
173
|
+
private
|
174
|
+
def add_to_list_top
|
175
|
+
increment_positions_on_all_items
|
176
|
+
end
|
177
|
+
|
178
|
+
def add_to_list_bottom
|
179
|
+
self[position_column] = bottom_position_in_list.to_i + 1
|
180
|
+
end
|
181
|
+
|
182
|
+
# Overwrite this method to define the scope of the list changes
|
183
|
+
def scope_condition() "1" end
|
184
|
+
|
185
|
+
# Returns the bottom position number in the list.
|
186
|
+
# bottom_position_in_list # => 2
|
187
|
+
def bottom_position_in_list(except = nil)
|
188
|
+
item = bottom_item(except)
|
189
|
+
item ? item.send(position_column) : 0
|
190
|
+
end
|
191
|
+
|
192
|
+
# Returns the bottom item
|
193
|
+
def bottom_item(except = nil)
|
194
|
+
conditions = scope_condition
|
195
|
+
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
|
196
|
+
acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
|
197
|
+
end
|
198
|
+
|
199
|
+
# Forces item to assume the bottom position in the list.
|
200
|
+
def assume_bottom_position
|
201
|
+
update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Forces item to assume the top position in the list.
|
205
|
+
def assume_top_position
|
206
|
+
update_attribute(position_column, 1)
|
207
|
+
end
|
208
|
+
|
209
|
+
# This has the effect of moving all the higher items up one.
|
210
|
+
def decrement_positions_on_higher_items(position)
|
211
|
+
acts_as_list_class.update_all(
|
212
|
+
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}"
|
213
|
+
)
|
214
|
+
end
|
215
|
+
|
216
|
+
# This has the effect of moving all the lower items up one.
|
217
|
+
def decrement_positions_on_lower_items
|
218
|
+
return unless in_list?
|
219
|
+
acts_as_list_class.update_all(
|
220
|
+
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
|
221
|
+
)
|
222
|
+
end
|
223
|
+
|
224
|
+
# This has the effect of moving all the higher items down one.
|
225
|
+
def increment_positions_on_higher_items
|
226
|
+
return unless in_list?
|
227
|
+
acts_as_list_class.update_all(
|
228
|
+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
|
229
|
+
)
|
230
|
+
end
|
231
|
+
|
232
|
+
# This has the effect of moving all the lower items down one.
|
233
|
+
def increment_positions_on_lower_items(position)
|
234
|
+
acts_as_list_class.update_all(
|
235
|
+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}"
|
236
|
+
)
|
237
|
+
end
|
238
|
+
|
239
|
+
# Increments position (<tt>position_column</tt>) of all items in the list.
|
240
|
+
def increment_positions_on_all_items
|
241
|
+
acts_as_list_class.update_all(
|
242
|
+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
|
243
|
+
)
|
244
|
+
end
|
245
|
+
|
246
|
+
def insert_at_position(position)
|
247
|
+
remove_from_list
|
248
|
+
increment_positions_on_lower_items(position)
|
249
|
+
self.update_attribute(position_column, position)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
ActiveRecord::Base.class_eval { include ActsAsList }
|
@@ -0,0 +1,369 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'activerecord', '>= 1.15.4.7794'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
require "#{File.dirname(__FILE__)}/../lib/acts_as_list"
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
10
|
+
|
11
|
+
def setup_db
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
13
|
+
create_table :mixins do |t|
|
14
|
+
t.column :pos, :integer
|
15
|
+
t.column :parent_id, :integer
|
16
|
+
t.column :created_at, :datetime
|
17
|
+
t.column :updated_at, :datetime
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_db
|
23
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
24
|
+
ActiveRecord::Base.connection.drop_table(table)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Mixin < ActiveRecord::Base
|
29
|
+
end
|
30
|
+
|
31
|
+
class ListMixin < Mixin
|
32
|
+
acts_as_list :column => "pos", :scope => :parent
|
33
|
+
|
34
|
+
def self.table_name() "mixins" end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ListMixinSub1 < ListMixin
|
38
|
+
end
|
39
|
+
|
40
|
+
class ListMixinSub2 < ListMixin
|
41
|
+
end
|
42
|
+
|
43
|
+
class ListWithStringScopeMixin < ActiveRecord::Base
|
44
|
+
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
|
45
|
+
|
46
|
+
def self.table_name() "mixins" end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
class ListTest < Test::Unit::TestCase
|
51
|
+
|
52
|
+
def setup
|
53
|
+
setup_db
|
54
|
+
(1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
|
55
|
+
end
|
56
|
+
|
57
|
+
def teardown
|
58
|
+
teardown_db
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_reordering
|
62
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
63
|
+
|
64
|
+
ListMixin.find(2).move_lower
|
65
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
66
|
+
|
67
|
+
ListMixin.find(2).move_higher
|
68
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
69
|
+
|
70
|
+
ListMixin.find(1).move_to_bottom
|
71
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
72
|
+
|
73
|
+
ListMixin.find(1).move_to_top
|
74
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
75
|
+
|
76
|
+
ListMixin.find(2).move_to_bottom
|
77
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
78
|
+
|
79
|
+
ListMixin.find(4).move_to_top
|
80
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_move_to_bottom_with_next_to_last_item
|
84
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
85
|
+
ListMixin.find(3).move_to_bottom
|
86
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_next_prev
|
90
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
91
|
+
assert_nil ListMixin.find(1).higher_item
|
92
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
93
|
+
assert_nil ListMixin.find(4).lower_item
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_injection
|
97
|
+
item = ListMixin.new(:parent_id => 1)
|
98
|
+
assert_equal "parent_id = 1", item.scope_condition
|
99
|
+
assert_equal "pos", item.position_column
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_insert
|
103
|
+
new = ListMixin.create(:parent_id => 20)
|
104
|
+
assert_equal 1, new.pos
|
105
|
+
assert new.first?
|
106
|
+
assert new.last?
|
107
|
+
|
108
|
+
new = ListMixin.create(:parent_id => 20)
|
109
|
+
assert_equal 2, new.pos
|
110
|
+
assert !new.first?
|
111
|
+
assert new.last?
|
112
|
+
|
113
|
+
new = ListMixin.create(:parent_id => 20)
|
114
|
+
assert_equal 3, new.pos
|
115
|
+
assert !new.first?
|
116
|
+
assert new.last?
|
117
|
+
|
118
|
+
new = ListMixin.create(:parent_id => 0)
|
119
|
+
assert_equal 1, new.pos
|
120
|
+
assert new.first?
|
121
|
+
assert new.last?
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_insert_at
|
125
|
+
new = ListMixin.create(:parent_id => 20)
|
126
|
+
assert_equal 1, new.pos
|
127
|
+
|
128
|
+
new = ListMixin.create(:parent_id => 20)
|
129
|
+
assert_equal 2, new.pos
|
130
|
+
|
131
|
+
new = ListMixin.create(:parent_id => 20)
|
132
|
+
assert_equal 3, new.pos
|
133
|
+
|
134
|
+
new4 = ListMixin.create(:parent_id => 20)
|
135
|
+
assert_equal 4, new4.pos
|
136
|
+
|
137
|
+
new4.insert_at(3)
|
138
|
+
assert_equal 3, new4.pos
|
139
|
+
|
140
|
+
new.reload
|
141
|
+
assert_equal 4, new.pos
|
142
|
+
|
143
|
+
new.insert_at(2)
|
144
|
+
assert_equal 2, new.pos
|
145
|
+
|
146
|
+
new4.reload
|
147
|
+
assert_equal 4, new4.pos
|
148
|
+
|
149
|
+
new5 = ListMixin.create(:parent_id => 20)
|
150
|
+
assert_equal 5, new5.pos
|
151
|
+
|
152
|
+
new5.insert_at(1)
|
153
|
+
assert_equal 1, new5.pos
|
154
|
+
|
155
|
+
new4.reload
|
156
|
+
assert_equal 5, new4.pos
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_delete_middle
|
160
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
161
|
+
|
162
|
+
ListMixin.find(2).destroy
|
163
|
+
|
164
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
165
|
+
|
166
|
+
assert_equal 1, ListMixin.find(1).pos
|
167
|
+
assert_equal 2, ListMixin.find(3).pos
|
168
|
+
assert_equal 3, ListMixin.find(4).pos
|
169
|
+
|
170
|
+
ListMixin.find(1).destroy
|
171
|
+
|
172
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
173
|
+
|
174
|
+
assert_equal 1, ListMixin.find(3).pos
|
175
|
+
assert_equal 2, ListMixin.find(4).pos
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_with_string_based_scope
|
179
|
+
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
180
|
+
assert_equal 1, new.pos
|
181
|
+
assert new.first?
|
182
|
+
assert new.last?
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_nil_scope
|
186
|
+
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
187
|
+
new2.move_higher
|
188
|
+
assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
def test_remove_from_list_should_then_fail_in_list?
|
193
|
+
assert_equal true, ListMixin.find(1).in_list?
|
194
|
+
ListMixin.find(1).remove_from_list
|
195
|
+
assert_equal false, ListMixin.find(1).in_list?
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_remove_from_list_should_set_position_to_nil
|
199
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
200
|
+
|
201
|
+
ListMixin.find(2).remove_from_list
|
202
|
+
|
203
|
+
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
204
|
+
|
205
|
+
assert_equal 1, ListMixin.find(1).pos
|
206
|
+
assert_equal nil, ListMixin.find(2).pos
|
207
|
+
assert_equal 2, ListMixin.find(3).pos
|
208
|
+
assert_equal 3, ListMixin.find(4).pos
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
212
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
213
|
+
|
214
|
+
ListMixin.find(2).remove_from_list
|
215
|
+
ListMixin.find(2).destroy
|
216
|
+
|
217
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
218
|
+
|
219
|
+
assert_equal 1, ListMixin.find(1).pos
|
220
|
+
assert_equal 2, ListMixin.find(3).pos
|
221
|
+
assert_equal 3, ListMixin.find(4).pos
|
222
|
+
end
|
223
|
+
|
224
|
+
# special thanks to openhood on github
|
225
|
+
def test_delete_middle_with_holes
|
226
|
+
# first we check everything is at expected place
|
227
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
228
|
+
|
229
|
+
# then we create a hole in the list, say you're working with existing data in which you already have holes
|
230
|
+
# or your scope is very complex
|
231
|
+
ListMixin.delete(2)
|
232
|
+
|
233
|
+
# we ensure the hole is really here
|
234
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
235
|
+
assert_equal 1, ListMixin.find(1).pos
|
236
|
+
assert_equal 3, ListMixin.find(3).pos
|
237
|
+
assert_equal 4, ListMixin.find(4).pos
|
238
|
+
|
239
|
+
# can we retrieve lower item despite the hole?
|
240
|
+
assert_equal 3, ListMixin.find(1).lower_item.id
|
241
|
+
|
242
|
+
# can we move an item lower jumping more than one position?
|
243
|
+
ListMixin.find(1).move_lower
|
244
|
+
assert_equal [3, 1, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
245
|
+
assert_equal 2, ListMixin.find(3).pos
|
246
|
+
assert_equal 3, ListMixin.find(1).pos
|
247
|
+
assert_equal 4, ListMixin.find(4).pos
|
248
|
+
|
249
|
+
# create another hole
|
250
|
+
ListMixin.delete(1)
|
251
|
+
|
252
|
+
# can we retrieve higher item despite the hole?
|
253
|
+
assert_equal 3, ListMixin.find(4).higher_item.id
|
254
|
+
|
255
|
+
# can we move an item higher jumping more than one position?
|
256
|
+
ListMixin.find(4).move_higher
|
257
|
+
assert_equal [4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
258
|
+
assert_equal 2, ListMixin.find(4).pos
|
259
|
+
assert_equal 3, ListMixin.find(3).pos
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
class ListSubTest < Test::Unit::TestCase
|
264
|
+
|
265
|
+
def setup
|
266
|
+
setup_db
|
267
|
+
(1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
|
268
|
+
end
|
269
|
+
|
270
|
+
def teardown
|
271
|
+
teardown_db
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_reordering
|
275
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
276
|
+
|
277
|
+
ListMixin.find(2).move_lower
|
278
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
279
|
+
|
280
|
+
ListMixin.find(2).move_higher
|
281
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
282
|
+
|
283
|
+
ListMixin.find(1).move_to_bottom
|
284
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
285
|
+
|
286
|
+
ListMixin.find(1).move_to_top
|
287
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
288
|
+
|
289
|
+
ListMixin.find(2).move_to_bottom
|
290
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
291
|
+
|
292
|
+
ListMixin.find(4).move_to_top
|
293
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_move_to_bottom_with_next_to_last_item
|
297
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
298
|
+
ListMixin.find(3).move_to_bottom
|
299
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_next_prev
|
303
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
304
|
+
assert_nil ListMixin.find(1).higher_item
|
305
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
306
|
+
assert_nil ListMixin.find(4).lower_item
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_injection
|
310
|
+
item = ListMixin.new("parent_id"=>1)
|
311
|
+
assert_equal "parent_id = 1", item.scope_condition
|
312
|
+
assert_equal "pos", item.position_column
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_insert_at
|
316
|
+
new = ListMixin.create("parent_id" => 20)
|
317
|
+
assert_equal 1, new.pos
|
318
|
+
|
319
|
+
new = ListMixinSub1.create("parent_id" => 20)
|
320
|
+
assert_equal 2, new.pos
|
321
|
+
|
322
|
+
new = ListMixinSub2.create("parent_id" => 20)
|
323
|
+
assert_equal 3, new.pos
|
324
|
+
|
325
|
+
new4 = ListMixin.create("parent_id" => 20)
|
326
|
+
assert_equal 4, new4.pos
|
327
|
+
|
328
|
+
new4.insert_at(3)
|
329
|
+
assert_equal 3, new4.pos
|
330
|
+
|
331
|
+
new.reload
|
332
|
+
assert_equal 4, new.pos
|
333
|
+
|
334
|
+
new.insert_at(2)
|
335
|
+
assert_equal 2, new.pos
|
336
|
+
|
337
|
+
new4.reload
|
338
|
+
assert_equal 4, new4.pos
|
339
|
+
|
340
|
+
new5 = ListMixinSub1.create("parent_id" => 20)
|
341
|
+
assert_equal 5, new5.pos
|
342
|
+
|
343
|
+
new5.insert_at(1)
|
344
|
+
assert_equal 1, new5.pos
|
345
|
+
|
346
|
+
new4.reload
|
347
|
+
assert_equal 5, new4.pos
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_delete_middle
|
351
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
352
|
+
|
353
|
+
ListMixin.find(2).destroy
|
354
|
+
|
355
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
356
|
+
|
357
|
+
assert_equal 1, ListMixin.find(1).pos
|
358
|
+
assert_equal 2, ListMixin.find(3).pos
|
359
|
+
assert_equal 3, ListMixin.find(4).pos
|
360
|
+
|
361
|
+
ListMixin.find(1).destroy
|
362
|
+
|
363
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
364
|
+
|
365
|
+
assert_equal 1, ListMixin.find(3).pos
|
366
|
+
assert_equal 2, ListMixin.find(4).pos
|
367
|
+
end
|
368
|
+
|
369
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|