liszt 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f95718b23f9690f8c3e7ffb3e6f749a3fd028ce
4
+ data.tar.gz: d4ec327052ee0cd233c66b5c84735d45742178ce
5
+ SHA512:
6
+ metadata.gz: 1f535ac64af9552d73637902ccdca9a66cd4b216b3f208c3a3541455cfbeb2bf58e094bac2ddc7e302d20e4459e3ad73283a973b747a6d5192be9c3d7d73a96a
7
+ data.tar.gz: 0ef43c9600baf9996a59cb288f99e4d1da6a9d1fe9ad29f7da092df86a71dd44f28ce4c23ffe611d4b6a254450359e588a7c4085d7f1b01427b63187f02cdceb
@@ -0,0 +1,7 @@
1
+ module Liszt
2
+ class Railtie < Rails::Railtie
3
+ config.after_initialize do
4
+ ActiveRecord::Base.extend Liszt
5
+ end
6
+ end
7
+ end
data/lib/liszt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Liszt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/liszt.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "liszt/version"
2
2
  require "liszt/instantizeable"
3
3
  require "liszt/redis_list"
4
+ require "liszt/railtie" if defined?(Rails::Railtie)
4
5
 
5
6
  require "active_record"
6
7
 
@@ -106,8 +107,14 @@ module Liszt
106
107
  refresh_ordered_list(obj)
107
108
  else
108
109
  list_ids = ordered_list_ids(obj)
109
- records = where('id in (?)', list_ids).to_a
110
- records.sort_by { |obj| list_ids.index(obj.id) }
110
+ records = liszt_relation(obj).to_a
111
+ real_ids = records.map(&:id)
112
+
113
+ if real_ids.to_set == list_ids.to_set
114
+ records.sort_by { |obj| list_ids.index(obj.id) }
115
+ else
116
+ refresh_ordered_records obj, list_ids, records, real_ids
117
+ end
111
118
  end
112
119
  end
113
120
 
@@ -117,14 +124,8 @@ module Liszt
117
124
  list_ids = ordered_list_ids(obj)
118
125
  records = liszt_relation(obj).to_a
119
126
  real_ids = records.map(&:id)
120
- merged_ids = Liszt.merge_id_lists(
121
- real_ids, list_ids, @liszt_append_new_items)
122
127
 
123
- if merged_ids != list_ids
124
- ordered_list(obj).clear_and_populate!(merged_ids)
125
- end
126
-
127
- records.sort_by { |obj| merged_ids.index(obj.id) }
128
+ refresh_ordered_records obj, list_ids, records, real_ids
128
129
  end
129
130
 
130
131
  # Update the given object's list with the given ids. Returns the final list
@@ -145,6 +146,17 @@ module Liszt
145
146
 
146
147
  private
147
148
 
149
+ def refresh_ordered_records(obj, list_ids, records, real_ids)
150
+ merged_ids = Liszt.merge_id_lists(
151
+ real_ids, list_ids, @liszt_append_new_items)
152
+
153
+ if merged_ids != list_ids
154
+ ordered_list(obj).clear_and_populate!(merged_ids)
155
+ end
156
+
157
+ records.sort_by { |obj| merged_ids.index(obj.id) }
158
+ end
159
+
148
160
  # Return the key for the Redis list that includes the given object.
149
161
  def liszt_key(obj={})
150
162
  key = "liszt:#{table_name}"
@@ -237,5 +249,4 @@ module Liszt
237
249
  end
238
250
  end
239
251
 
240
- # @@ TODO: use an actual railtie
241
- ActiveRecord::Base.extend Liszt
252
+ ActiveRecord::Base.extend Liszt unless defined?(Rails::Railtie)
data/test/liszt_test.rb CHANGED
@@ -86,11 +86,11 @@ describe Liszt do
86
86
  @person = Person.new(:name => "John Smith", :group_id => 1, :is_male => true)
87
87
  end
88
88
 
89
- it "doesn't confirm the list when force_refresh is nil" do
89
+ it "confirms the list when force_refresh is nil but records and sort order don't match" do
90
90
  @person.save
91
91
  @person.remove_from_list
92
- assert !@person.ordered_list_items.include?(@person)
93
- assert !@person.ordered_list_ids.include?(@person.id)
92
+ assert @person.ordered_list_items.include?(@person)
93
+ assert @person.ordered_list_ids.include?(@person.id)
94
94
  end
95
95
 
96
96
  it "confirms the list when force_refresh is true" do
@@ -100,11 +100,17 @@ describe Liszt do
100
100
  assert @person.ordered_list_ids.include?(@person.id)
101
101
  end
102
102
 
103
- it "removes incorrect items from the list when force_refresh is true" do
103
+ it "removes incorrect items from the list when force_refresh is not true" do
104
104
  @person.save
105
105
  @person.ordered_list.push(12314231)
106
106
  assert @person.ordered_list_ids.include?(12314231)
107
107
  @person.ordered_list_items
108
+ refute @person.ordered_list_ids.include?(12314231)
109
+ end
110
+
111
+ it "removes incorrect items from the list when force_refresh is true" do
112
+ @person.save
113
+ @person.ordered_list.push(12314231)
108
114
  assert @person.ordered_list_ids.include?(12314231)
109
115
  @person.ordered_list_items(:force_refresh => true)
110
116
  refute @person.ordered_list_ids.include?(12314231)
@@ -173,6 +179,13 @@ describe Liszt do
173
179
  end
174
180
 
175
181
  describe ".update_ordered_list" do
182
+ # check both the retval and the actual list
183
+ def assert_update(input, desired_result)
184
+ actual_result = @nelson.update_ordered_list input
185
+ actual_result.must_equal desired_result
186
+ @nelson.ordered_list_ids.must_equal desired_result
187
+ end
188
+
176
189
  before do
177
190
  @nelson = people(:nelson)
178
191
  @nelson.initialize_list!
@@ -180,9 +193,7 @@ describe Liszt do
180
193
  end
181
194
 
182
195
  it "reorders the elements based on the given list" do
183
- retval = @nelson.update_ordered_list [@id4, @id3, @id2, @id1]
184
- retval.must_equal [@id4, @id3, @id2, @id1]
185
- @nelson.ordered_list_ids.must_equal [@id4, @id3, @id2, @id1]
196
+ assert_update [@id4, @id3, @id2, @id1], [@id4, @id3, @id2, @id1]
186
197
  end
187
198
 
188
199
  describe "when the existing list is missing elements from the db" do
@@ -193,16 +204,13 @@ describe Liszt do
193
204
 
194
205
  describe "and the user also didn't provide them" do
195
206
  it "prepends those elements to the list" do
196
- retval = @nelson.update_ordered_list [@id4, @id2, @id1]
197
- retval.must_equal [@id3, @id4, @id2, @id1]
198
- @nelson.ordered_list_ids.must_equal [@id3, @id4, @id2, @id1]
207
+ assert_update [@id4, @id2, @id1], [@id3, @id4, @id2, @id1]
199
208
  end
200
209
  end
201
210
 
202
211
  describe "and the user provided them as part of their ordering" do
203
212
  it "adds those elements where the user provided them" do
204
- @nelson.update_ordered_list [@id4, @id2, @id3, @id1]
205
- @nelson.ordered_list_ids.must_equal [@id4, @id2, @id3, @id1]
213
+ assert_update [@id4, @id2, @id3, @id1], [@id4, @id2, @id3, @id1]
206
214
  end
207
215
  end
208
216
 
@@ -212,8 +220,7 @@ describe Liszt do
212
220
  end
213
221
 
214
222
  it "prepends only the one that wasn't provided" do
215
- @nelson.update_ordered_list [@id4, @id2, @id1]
216
- @nelson.ordered_list_ids.must_equal [@id3, @id4, @id2, @id1]
223
+ assert_update [@id4, @id2, @id1], [@id3, @id4, @id2, @id1]
217
224
  end
218
225
  end
219
226
  end
@@ -224,27 +231,29 @@ describe Liszt do
224
231
  end
225
232
 
226
233
  it "removes those elements from the list" do
227
- @nelson.update_ordered_list [@id4, @id3, @id2, @id1]
228
- @nelson.ordered_list_ids.must_equal [@id4, @id3, @id2, @id1]
234
+ assert_update [@id4, @id3, @id2, @id1], [@id4, @id3, @id2, @id1]
229
235
  end
230
236
 
231
237
  it "ignores those elements if given by the user" do
232
- @nelson.update_ordered_list [@id4, @id3, 123, @id2, @id1]
233
- @nelson.ordered_list_ids.must_equal [@id4, @id3, @id2, @id1]
238
+ assert_update [@id4, @id3, 123, @id2, @id1], [@id4, @id3, @id2, @id1]
234
239
  end
235
240
  end
236
241
 
237
242
  describe "when the list given by the user omits needed elements" do
238
- it "prepends them to the list" do
239
- @nelson.update_ordered_list [@id4, @id3, @id1]
240
- @nelson.ordered_list_ids.must_equal [@id2, @id4, @id3, @id1]
243
+ it "prepends them to the list in their existing order" do
244
+ assert_update [@id3], [@id1, @id2, @id4, @id3]
245
+ end
246
+ end
247
+
248
+ describe "when the list given by the user is empty" do
249
+ it "preserves the existing order" do
250
+ assert_update [], [@id1, @id2, @id3, @id4]
241
251
  end
242
252
  end
243
253
 
244
254
  describe "when the list given by the user contains nonexistent elements" do
245
255
  it "ignores them and calls back" do
246
- @nelson.update_ordered_list [@id4, @id3, 123, @id2, @id1]
247
- @nelson.ordered_list_ids.must_equal [@id4, @id3, @id2, @id1]
256
+ assert_update [@id4, @id3, 123, @id2, @id1], [@id4, @id3, @id2, @id1]
248
257
  end
249
258
  end
250
259
  end
metadata CHANGED
@@ -1,144 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liszt
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ryan Fitzgerald
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-20 00:00:00.000000000 Z
11
+ date: 2013-10-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- prerelease: false
16
- version_requirements: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ! '>='
19
- - !ruby/object:Gem::Version
20
- version: 3.0.0
21
- none: false
22
- type: :runtime
23
14
  name: activerecord
24
15
  requirement: !ruby/object:Gem::Requirement
25
16
  requirements:
26
- - - ! '>='
17
+ - - '>='
27
18
  - !ruby/object:Gem::Version
28
19
  version: 3.0.0
29
- none: false
30
- - !ruby/object:Gem::Dependency
20
+ type: :runtime
31
21
  prerelease: false
32
22
  version_requirements: !ruby/object:Gem::Requirement
33
23
  requirements:
34
- - - ! '>='
24
+ - - '>='
35
25
  - !ruby/object:Gem::Version
36
- version: '0'
37
- none: false
38
- type: :runtime
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
39
28
  name: redis
40
29
  requirement: !ruby/object:Gem::Requirement
41
30
  requirements:
42
- - - ! '>='
31
+ - - '>='
43
32
  - !ruby/object:Gem::Version
44
33
  version: '0'
45
- none: false
46
- - !ruby/object:Gem::Dependency
34
+ type: :runtime
47
35
  prerelease: false
48
36
  version_requirements: !ruby/object:Gem::Requirement
49
37
  requirements:
50
- - - ! '>='
38
+ - - '>='
51
39
  - !ruby/object:Gem::Version
52
40
  version: '0'
53
- none: false
54
- type: :development
41
+ - !ruby/object:Gem::Dependency
55
42
  name: minitest
56
43
  requirement: !ruby/object:Gem::Requirement
57
44
  requirements:
58
- - - ! '>='
45
+ - - '>='
59
46
  - !ruby/object:Gem::Version
60
47
  version: '0'
61
- none: false
62
- - !ruby/object:Gem::Dependency
48
+ type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - ! '>='
52
+ - - '>='
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
- none: false
70
- type: :development
55
+ - !ruby/object:Gem::Dependency
71
56
  name: pry
72
57
  requirement: !ruby/object:Gem::Requirement
73
58
  requirements:
74
- - - ! '>='
59
+ - - '>='
75
60
  - !ruby/object:Gem::Version
76
61
  version: '0'
77
- none: false
78
- - !ruby/object:Gem::Dependency
62
+ type: :development
79
63
  prerelease: false
80
64
  version_requirements: !ruby/object:Gem::Requirement
81
65
  requirements:
82
- - - ! '>='
66
+ - - '>='
83
67
  - !ruby/object:Gem::Version
84
68
  version: '0'
85
- none: false
86
- type: :development
69
+ - !ruby/object:Gem::Dependency
87
70
  name: rake
88
71
  requirement: !ruby/object:Gem::Requirement
89
72
  requirements:
90
- - - ! '>='
73
+ - - '>='
91
74
  - !ruby/object:Gem::Version
92
75
  version: '0'
93
- none: false
94
- - !ruby/object:Gem::Dependency
76
+ type: :development
95
77
  prerelease: false
96
78
  version_requirements: !ruby/object:Gem::Requirement
97
79
  requirements:
98
- - - ! '>='
80
+ - - '>='
99
81
  - !ruby/object:Gem::Version
100
82
  version: '0'
101
- none: false
102
- type: :development
83
+ - !ruby/object:Gem::Dependency
103
84
  name: rdiscount
104
85
  requirement: !ruby/object:Gem::Requirement
105
86
  requirements:
106
- - - ! '>='
87
+ - - '>='
107
88
  - !ruby/object:Gem::Version
108
89
  version: '0'
109
- none: false
110
- - !ruby/object:Gem::Dependency
90
+ type: :development
111
91
  prerelease: false
112
92
  version_requirements: !ruby/object:Gem::Requirement
113
93
  requirements:
114
- - - ! '>='
94
+ - - '>='
115
95
  - !ruby/object:Gem::Version
116
96
  version: '0'
117
- none: false
118
- type: :development
97
+ - !ruby/object:Gem::Dependency
119
98
  name: sqlite3
120
99
  requirement: !ruby/object:Gem::Requirement
121
100
  requirements:
122
- - - ! '>='
101
+ - - '>='
123
102
  - !ruby/object:Gem::Version
124
103
  version: '0'
125
- none: false
126
- - !ruby/object:Gem::Dependency
104
+ type: :development
127
105
  prerelease: false
128
106
  version_requirements: !ruby/object:Gem::Requirement
129
107
  requirements:
130
- - - ! '>='
108
+ - - '>='
131
109
  - !ruby/object:Gem::Version
132
110
  version: '0'
133
- none: false
134
- type: :development
111
+ - !ruby/object:Gem::Dependency
135
112
  name: yard
136
113
  requirement: !ruby/object:Gem::Requirement
137
114
  requirements:
138
- - - ! '>='
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
139
123
  - !ruby/object:Gem::Version
140
124
  version: '0'
141
- none: false
142
125
  description: Liszt is an alternative to acts_as_list and sortable that uses atomic
143
126
  Redis operations to maintain scoped ordering information for ActiveRecord objects.
144
127
  email:
@@ -154,6 +137,7 @@ files:
154
137
  - Rakefile
155
138
  - lib/liszt.rb
156
139
  - lib/liszt/instantizeable.rb
140
+ - lib/liszt/railtie.rb
157
141
  - lib/liszt/redis_list.rb
158
142
  - lib/liszt/version.rb
159
143
  - liszt.gemspec
@@ -165,27 +149,26 @@ files:
165
149
  - test/test_helper.rb
166
150
  homepage: http://academia.edu
167
151
  licenses: []
152
+ metadata: {}
168
153
  post_install_message:
169
154
  rdoc_options: []
170
155
  require_paths:
171
156
  - lib
172
157
  required_ruby_version: !ruby/object:Gem::Requirement
173
158
  requirements:
174
- - - ! '>='
159
+ - - '>='
175
160
  - !ruby/object:Gem::Version
176
161
  version: '0'
177
- none: false
178
162
  required_rubygems_version: !ruby/object:Gem::Requirement
179
163
  requirements:
180
- - - ! '>='
164
+ - - '>='
181
165
  - !ruby/object:Gem::Version
182
166
  version: '0'
183
- none: false
184
167
  requirements: []
185
168
  rubyforge_project:
186
- rubygems_version: 1.8.25
169
+ rubygems_version: 2.0.0
187
170
  signing_key:
188
- specification_version: 3
171
+ specification_version: 4
189
172
  summary: ActiveRecord sorting using Redis lists
190
173
  test_files:
191
174
  - test/fixtures/groups.yml