resort 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Codegram
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.
data/Readme.md CHANGED
@@ -17,9 +17,9 @@ the _weight_ of a given element within a tree. This field has no semantic sense,
17
17
  since "84" by itself gives you absolutely no information about an element's
18
18
  position or its relations with other elements of the tree.
19
19
 
20
- Resort is implemented quite like a [binary tree](http://en.wikipedia.org/wiki/Binary_tree),
20
+ Resort is implemented like a [linked list](http://en.wikipedia.org/wiki/Linked_list),
21
21
  rather than relying on absolute position values. This way, every model
22
- references a `next` and a `previous`, which seems a bit more sensible :)
22
+ references a `next` element, which seems a bit more sensible :)
23
23
 
24
24
  ##Usage
25
25
 
@@ -29,6 +29,8 @@ You must add two fields (`next_id` and `first`) to your model's table:
29
29
  def self.up
30
30
  add_column :products, :next_id, :integer
31
31
  add_column :products, :first, :boolean
32
+ add_index :products, :next_id
33
+ add_index :products, :first
32
34
  end
33
35
 
34
36
  def self.down
@@ -1,4 +1,4 @@
1
1
  module Resort
2
2
  # Resort's version number
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
data/lib/resort.rb CHANGED
@@ -128,7 +128,7 @@ module Resort
128
128
  # in the first position. Otherwise, it appends it to the end of the
129
129
  # empty list.
130
130
  def include_in_list!
131
- _siblings.count > 0 ? push\
131
+ _siblings.count > 0 ? last!\
132
132
  : prepend
133
133
  end
134
134
 
@@ -146,22 +146,20 @@ module Resort
146
146
 
147
147
  # Puts the object in the last position of the list.
148
148
  def push
149
- return if last?
150
- last_element = _siblings.where(:next_id => nil).first
151
- self.append_to(last_element)
149
+ self.append_to(last) unless last?
152
150
  end
153
151
 
154
152
  # Puts the object right after another object in the list.
155
153
  def append_to(another)
156
154
  if self.next
157
155
  delete_from_list
158
- elsif last?
159
- self.previous.update_attribute(:next_id, nil)
156
+ elsif last? && self.previous
157
+ # self.previous.update_attribute(:next_id, nil)
160
158
  self.previous = nil
161
159
  end
162
160
 
163
- self.update_attribute(:next_id, another.next_id)
164
- another.update_attribute(:next_id, self.id)
161
+ self.update_attribute(:next_id, another.next_id) if self.next_id or (another && another.next_id)
162
+ another.update_attribute(:next_id, self.id) if another
165
163
  end
166
164
 
167
165
  private
@@ -180,7 +178,15 @@ module Resort
180
178
  end
181
179
 
182
180
  def last?
183
- self.previous && !self.next
181
+ self.first != true && self.next_id == nil
182
+ end
183
+
184
+ def last
185
+ _siblings.where(:next_id => nil).first
186
+ end
187
+
188
+ def last!
189
+ last.update_attribute(:next_id, self.id)
184
190
  end
185
191
 
186
192
  def _siblings
data/spec/resort_spec.rb CHANGED
@@ -267,6 +267,24 @@ module Resort
267
267
  article4.next.name.should == '3'
268
268
  end
269
269
  end
270
+ context 'appending 3 after 1' do
271
+ it "appends the element after another element" do
272
+ @article3.append_to(@article1)
273
+
274
+ article1 = Article.find_by_name('1')
275
+ article1.next.name.should == '3'
276
+
277
+ article2 = Article.find_by_name('2')
278
+ article2.previous.name.should == '3'
279
+ article2.next.name.should == '4'
280
+
281
+ article3 = Article.find_by_name('3')
282
+ article3.previous.name.should == '1'
283
+ article3.next.name.should == '2'
284
+
285
+ @article4.previous.name.should == '2'
286
+ end
287
+ end
270
288
 
271
289
  context 'when the article is already after the other element' do
272
290
  it 'does nothing' do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: resort
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Oriol Gual
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-03-18 00:00:00 +01:00
15
+ date: 2011-03-24 00:00:00 +01:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - .rspec
85
85
  - .rvmrc
86
86
  - Gemfile
87
+ - LICENSE
87
88
  - Rakefile
88
89
  - Readme.md
89
90
  - lib/resort.rb
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  requirements: []
116
117
 
117
118
  rubyforge_project: resort
118
- rubygems_version: 1.5.2
119
+ rubygems_version: 1.5.3
119
120
  signing_key:
120
121
  specification_version: 3
121
122
  summary: Positionless model sorting for Rails 3.