ranked-model 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.mkd CHANGED
@@ -34,7 +34,7 @@ This simple example assumes an integer column called `row_order`. To order Duck
34
34
 
35
35
  Duck.rank(:row_order).all
36
36
 
37
- The ranking integers stored in the `raw_order` column will be big and spaced apart. When you
37
+ The ranking integers stored in the `row_order` column will be big and spaced apart. When you
38
38
  implement a sorting UI, just update the resource with the position instead:
39
39
 
40
40
  @duck.update_attribute :row_order_position, 0 # or 1, 2, 37. :first and :last are also valid
@@ -128,7 +128,12 @@ module RankedModel
128
128
  end
129
129
 
130
130
  def rearrange_ranks
131
- if current_last.rank < (RankedModel::MAX_RANK_VALUE - 1) && rank < current_last.rank
131
+ if current_first.rank > RankedModel::MIN_RANK_VALUE && rank == RankedModel::MAX_RANK_VALUE
132
+ instance.class.
133
+ where( instance.class.arel_table[:id].not_eq(instance.id) ).
134
+ where( instance.class.arel_table[ranker.column].lteq(rank) ).
135
+ update_all( "#{ranker.column} = #{ranker.column} - 1" )
136
+ elsif current_last.rank < (RankedModel::MAX_RANK_VALUE - 1) && rank < current_last.rank
132
137
  instance.class.
133
138
  where( instance.class.arel_table[:id].not_eq(instance.id) ).
134
139
  where( instance.class.arel_table[ranker.column].gteq(rank) ).
@@ -1,3 +1,3 @@
1
1
  module RankedModel
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -37,6 +37,32 @@ describe Duck do
37
37
 
38
38
  end
39
39
 
40
+ describe 'last' do
41
+
42
+ before {
43
+ @last = Duck.last
44
+ @last.update_attribute :row_position, :last
45
+ }
46
+
47
+ subject { Duck.rank(:row).last }
48
+
49
+ its(:id) { should == @last.id }
50
+
51
+ end
52
+
53
+ describe 'first' do
54
+
55
+ before {
56
+ @last = Duck.last
57
+ @last.update_attribute :row_position, :first
58
+ }
59
+
60
+ subject { Duck.rank(:row).first }
61
+
62
+ its(:id) { should == @last.id }
63
+
64
+ end
65
+
40
66
  end
41
67
 
42
68
  describe "a rearrangement" do
@@ -55,7 +81,7 @@ describe Duck do
55
81
 
56
82
  subject { Duck.rank(:row).collect {|d| d.id } }
57
83
 
58
- it { should == (@ordered[0..-2] + [@first.id, @second.id, @ordered[-1]]) }
84
+ it { should == (@ordered[0..-2] + [@ordered[-1], @first.id, @second.id]) }
59
85
 
60
86
  }
61
87
 
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Element do
4
+
5
+ before {
6
+ @elements = {
7
+ :chromium => TransitionMetal.create( :symbol => 'Cr' ),
8
+ :manganese => TransitionMetal.create( :symbol => 'Mn' ),
9
+ :argon => NobleGas.create( :symbol => 'Ar' ),
10
+ :helium => NobleGas.create( :symbol => 'He' ),
11
+ :xenon => NobleGas.create( :symbol => 'Xe' )
12
+ }
13
+ @elements.each { |name, element|
14
+ element.reload
15
+ element.update_attribute :combination_order_position, 0
16
+ }
17
+ @elements.each {|name, element| element.reload }
18
+ }
19
+
20
+ describe "rebalancing on an STI class should not affect the other class" do
21
+
22
+ before {
23
+ @elements[:helium].update_attribute :combination_order_position, :first
24
+ @elements[:xenon].update_attribute :combination_order_position, :first
25
+ @elements[:argon].update_attribute :combination_order_position, :last
26
+
27
+ TransitionMetal.ranker(:combination_order).with(@elements[:chromium]).instance_eval { rebalance_ranks }
28
+ }
29
+
30
+ subject { NobleGas.rank(:combination_order) }
31
+
32
+ its(:size) { should == 3 }
33
+
34
+ its(:first) { should == @elements[:xenon] }
35
+
36
+ its(:last) { should == @elements[:argon] }
37
+
38
+ end
39
+
40
+ describe "setting positions on STI classes" do
41
+
42
+ before {
43
+ @elements[:helium].update_attribute :combination_order_position, :first
44
+ @elements[:xenon].update_attribute :combination_order_position, :first
45
+ @elements[:argon].update_attribute :combination_order_position, :first
46
+
47
+ @elements[:chromium].update_attribute :combination_order_position, 1
48
+ @elements[:manganese].update_attribute :combination_order_position, 1
49
+ @elements[:manganese].update_attribute :combination_order_position, 0
50
+ @elements[:chromium].update_attribute :combination_order_position, 0
51
+ @elements[:manganese].update_attribute :combination_order_position, 0
52
+ @elements[:chromium].update_attribute :combination_order_position, 0
53
+ }
54
+
55
+ describe "NobleGas" do
56
+
57
+ subject { NobleGas.rank(:combination_order) }
58
+
59
+ its(:size) { should == 3 }
60
+
61
+ its(:first) { should == @elements[:argon] }
62
+
63
+ its(:last) { should == @elements[:helium] }
64
+
65
+ end
66
+
67
+ describe "TransitionMetal" do
68
+
69
+ subject { TransitionMetal.rank(:combination_order) }
70
+
71
+ its(:size) { should == 2 }
72
+
73
+ its(:first) { should == @elements[:chromium] }
74
+
75
+ its(:last) { should == @elements[:manganese] }
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -30,6 +30,12 @@ ActiveRecord::Schema.define :version => 0 do
30
30
  t.integer :age
31
31
  t.string :pond
32
32
  end
33
+
34
+ create_table :elements, :force => true do |t|
35
+ t.string :symbol
36
+ t.string :type
37
+ t.integer :combination_order
38
+ end
33
39
  end
34
40
 
35
41
  class Duck < ActiveRecord::Base
@@ -58,3 +64,18 @@ class WrongFieldDuck < ActiveRecord::Base
58
64
  ranks :age, :with_same => :non_existant_field
59
65
 
60
66
  end
67
+
68
+ class Element < ActiveRecord::Base
69
+
70
+ include RankedModel
71
+ ranks :combination_order
72
+
73
+ end
74
+
75
+ class TransitionMetal < Element
76
+
77
+ end
78
+
79
+ class NobleGas < Element
80
+
81
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranked-model
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Beale
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-27 00:00:00 -04:00
18
+ date: 2011-06-15 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -132,6 +132,7 @@ files:
132
132
  - spec/ranked-model/ranker_spec.rb
133
133
  - spec/ranked-model/version_spec.rb
134
134
  - spec/spec_helper.rb
135
+ - spec/sti-model/element_spec.rb
135
136
  - spec/support/active_record.rb
136
137
  - spec/support/database.yml
137
138
  - tmp/.gitignore
@@ -176,5 +177,6 @@ test_files:
176
177
  - spec/ranked-model/ranker_spec.rb
177
178
  - spec/ranked-model/version_spec.rb
178
179
  - spec/spec_helper.rb
180
+ - spec/sti-model/element_spec.rb
179
181
  - spec/support/active_record.rb
180
182
  - spec/support/database.yml