ranked-model-rails2 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vehicle do
4
+
5
+ before {
6
+ @vehicles = {
7
+ :ford => Car.create( :manufacturer => 'Ford' ),
8
+ :bmw => Car.create( :manufacturer => 'BMW' ),
9
+ :daimler => Truck.create( :manufacturer => 'Daimler' ),
10
+ :volvo => Truck.create( :manufacturer => 'Volvo' ),
11
+ :kenworth => Truck.create( :manufacturer => 'Kenworth' )
12
+ }
13
+ @vehicles.each { |name, vehicle|
14
+ vehicle.reload
15
+ vehicle.update_attribute :parking_order_position, 0
16
+ }
17
+ @vehicles.each {|name, vehicle| vehicle.reload }
18
+ }
19
+
20
+ describe "ranking by STI parent" do
21
+
22
+ before {
23
+ @vehicles[:volvo].update_attribute :parking_order_position, :first
24
+ @vehicles[:ford].update_attribute :parking_order_position, :first
25
+ }
26
+
27
+ describe "Vehicle" do
28
+
29
+ subject { Vehicle.rank(:parking_order) }
30
+
31
+ its(:size) { should == 5 }
32
+
33
+ its(:first) { should == @vehicles[:ford] }
34
+
35
+ its(:second) { should == @vehicles[:volvo] }
36
+
37
+ end
38
+
39
+ end
40
+
41
+ describe "overriding parent's ranking" do
42
+
43
+ describe "Vehicle" do
44
+
45
+ it "should have one ranker object" do
46
+ Vehicle.rankers.count.should == 1
47
+ end
48
+
49
+ subject { Vehicle.rankers.first }
50
+
51
+ its(:class_name) { should == 'Vehicle' }
52
+
53
+ its(:column) { should == :parking_order }
54
+
55
+ end
56
+
57
+ describe "MotorBike" do
58
+
59
+ it "should have one ranker object" do
60
+ MotorBike.rankers.count.should == 1
61
+ end
62
+
63
+ subject { MotorBike.rankers.first }
64
+
65
+ its(:class_name) { should == 'Vehicle' }
66
+
67
+ its(:column) { should == :parking_order }
68
+
69
+ its(:with_same) { should == :color }
70
+
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,150 @@
1
+ require 'active_record'
2
+ require 'logger'
3
+
4
+ ROOT = File.join(File.dirname(__FILE__), '..')
5
+
6
+ DB_CONFIG = "test" + (ENV['DB'] ? "_#{ENV['DB'].downcase}" : '')
7
+
8
+ ActiveRecord::Base.logger = Logger.new('tmp/ar_debug.log')
9
+ ActiveRecord::Base.configurations = YAML::load(IO.read('spec/support/database.yml'))
10
+ ActiveRecord::Base.establish_connection(DB_CONFIG)
11
+
12
+ ActiveRecord::Schema.define :version => 0 do
13
+ create_table :ducks, :force => true do |t|
14
+ t.string :name
15
+ t.integer :row
16
+ t.integer :size
17
+ t.integer :age
18
+ t.integer :lake_id
19
+ t.integer :flock_id
20
+ t.integer :landing_order
21
+ t.string :pond
22
+ end
23
+
24
+ create_table :wrong_scope_ducks, :force => true do |t|
25
+ t.string :name
26
+ t.integer :size
27
+ t.string :pond
28
+ end
29
+
30
+ create_table :wrong_field_ducks, :force => true do |t|
31
+ t.string :name
32
+ t.integer :age
33
+ t.string :pond
34
+ end
35
+
36
+ create_table :elements, :force => true do |t|
37
+ t.string :symbol
38
+ t.string :type
39
+ t.integer :combination_order
40
+ end
41
+
42
+ create_table :vehicles, :force => true do |t|
43
+ t.string :color
44
+ t.string :manufacturer
45
+ t.string :type
46
+ t.integer :parking_order
47
+ end
48
+
49
+ create_table :egos, :primary_key => :alternative_to_id, :force => true do |t|
50
+ t.string :name
51
+ t.integer :size
52
+ end
53
+
54
+ create_table :players, :force => true do |t|
55
+ t.string :name
56
+ t.string :city
57
+ t.integer :score
58
+ end
59
+ end
60
+
61
+ class Duck < ActiveRecord::Base
62
+
63
+ include RankedModel
64
+ ranks :row
65
+ ranks :size, :scope => :in_shin_pond
66
+ ranks :age, :with_same => :pond
67
+
68
+ ranks :landing_order, :with_same => [:lake_id, :flock_id]
69
+ scope :in_lake_and_flock, lambda {|lake, flock| where(:lake_id => lake, :flock_id => flock) }
70
+
71
+ scope :in_shin_pond, lambda { where(:pond => 'Shin') }
72
+
73
+ end
74
+
75
+ # Negative examples
76
+
77
+ class WrongScopeDuck < ActiveRecord::Base
78
+
79
+ include RankedModel
80
+ ranks :size, :scope => :non_existant_scope
81
+
82
+ end
83
+
84
+ class WrongFieldDuck < ActiveRecord::Base
85
+
86
+ include RankedModel
87
+ ranks :age, :with_same => :non_existant_field
88
+ end
89
+
90
+ class ReallyWrongFieldDuck < ActiveRecord::Base
91
+ self.table_name = :wrong_field_ducks
92
+ include RankedModel
93
+ ranks :age, :with_same => [:name, :non_existant_field]
94
+ end
95
+
96
+ # Example for STI, ranking within each child class
97
+
98
+ class Element < ActiveRecord::Base
99
+
100
+ include RankedModel
101
+ ranks :combination_order
102
+
103
+ end
104
+
105
+ class TransitionMetal < Element
106
+
107
+ end
108
+
109
+ class NobleGas < Element
110
+
111
+ end
112
+
113
+ # Example for STI, ranking within parent
114
+
115
+ class Vehicle < ActiveRecord::Base
116
+
117
+ include RankedModel
118
+ ranks :parking_order, :class_name => 'Vehicle'
119
+
120
+ end
121
+
122
+ class Car < Vehicle
123
+
124
+ end
125
+
126
+ class Truck < Vehicle
127
+
128
+ end
129
+
130
+ # Example for STI, overriding parent's ranking
131
+
132
+ class MotorBike < Vehicle
133
+
134
+ include RankedModel
135
+ ranks :parking_order, :class_name => 'Vehicle', :with_same => :color
136
+
137
+ end
138
+
139
+ class Ego < ActiveRecord::Base
140
+ def self.primary_key
141
+ :alternative_to_id
142
+ end
143
+
144
+ include RankedModel
145
+ ranks :size
146
+ end
147
+
148
+ class Player < ActiveRecord::Base
149
+ # don't add rank yet, do it in the specs
150
+ end
@@ -0,0 +1,30 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: tmp/data.sqlite3
4
+ pool: 5
5
+ timeout: 5000
6
+
7
+ test_mysql:
8
+ adapter: mysql
9
+ database: ranked_model_test
10
+ pool: 5
11
+ timeout: 5000
12
+
13
+ test_postgresql:
14
+ adapter: postgresql
15
+ database: ranked_model_test
16
+ pool: 5
17
+ timeout: 5000
18
+
19
+ test_mysql_travis:
20
+ adapter: mysql
21
+ database: ranked_model_test
22
+ pool: 5
23
+ timeout: 5000
24
+ username: travis
25
+
26
+ test_postgresql_travis:
27
+ adapter: postgresql
28
+ database: ranked_model_test
29
+ pool: 5
30
+ timeout: 5000
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranked-model-rails2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - ThoughtWorks Studios
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2016-04-12 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.18
22
+ type: :runtime
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: fake_arel
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.1
32
+ type: :runtime
33
+ version_requirements: *id002
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: 2.13.0
42
+ type: :development
43
+ version_requirements: *id003
44
+ - !ruby/object:Gem::Dependency
45
+ name: sqlite3
46
+ prerelease: false
47
+ requirement: &id004 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: 1.3.7
52
+ type: :development
53
+ version_requirements: *id004
54
+ - !ruby/object:Gem::Dependency
55
+ name: genspec
56
+ prerelease: false
57
+ requirement: &id005 !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.8
62
+ type: :development
63
+ version_requirements: *id005
64
+ - !ruby/object:Gem::Dependency
65
+ name: mocha
66
+ prerelease: false
67
+ requirement: &id006 !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: 0.14.0
72
+ type: :development
73
+ version_requirements: *id006
74
+ - !ruby/object:Gem::Dependency
75
+ name: database_cleaner
76
+ prerelease: false
77
+ requirement: &id007 !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: 1.2.0
82
+ type: :development
83
+ version_requirements: *id007
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ prerelease: false
87
+ requirement: &id008 !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: 10.1.0
92
+ type: :development
93
+ version_requirements: *id008
94
+ description: ranked-model-rails2 is a modern row sorting library built for Rails 2, backported from ranked-model. It uses fake_arel and is better optimized than most other libraries.
95
+ email:
96
+ - mingle-dev@thoughtworks.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files: []
102
+
103
+ files:
104
+ - .gitignore
105
+ - .rbenv-vars
106
+ - .rspec
107
+ - .ruby-gemset
108
+ - .ruby-version
109
+ - .travis.yml
110
+ - Gemfile
111
+ - LICENSE
112
+ - Rakefile
113
+ - Readme.mkd
114
+ - lib/ranked-model.rb
115
+ - lib/ranked-model/railtie.rb
116
+ - lib/ranked-model/ranker.rb
117
+ - lib/ranked-model/version.rb
118
+ - rails/init.rb
119
+ - ranked-model-rails2.gemspec
120
+ - spec/duck-model/duck_spec.rb
121
+ - spec/duck-model/lots_of_ducks_spec.rb
122
+ - spec/duck-model/wrong_ducks_spec.rb
123
+ - spec/ego-model/ego_spec.rb
124
+ - spec/player-model/records_already_exist_spec.rb
125
+ - spec/ranked-model/ranker_spec.rb
126
+ - spec/ranked-model/version_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/sti-model/element_spec.rb
129
+ - spec/sti-model/vehicle_spec.rb
130
+ - spec/support/active_record.rb
131
+ - spec/support/database.yml
132
+ - tmp/.gitignore
133
+ homepage: https://github.com/ThoughtWorksStudios/ranked-model-rails2
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+
138
+ post_install_message:
139
+ rdoc_options: []
140
+
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - &id009
146
+ - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - *id009
152
+ requirements: []
153
+
154
+ rubyforge_project:
155
+ rubygems_version: 2.1.9
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: An acts_as_sortable replacement built for Rails 2
159
+ test_files:
160
+ - spec/duck-model/duck_spec.rb
161
+ - spec/duck-model/lots_of_ducks_spec.rb
162
+ - spec/duck-model/wrong_ducks_spec.rb
163
+ - spec/ego-model/ego_spec.rb
164
+ - spec/player-model/records_already_exist_spec.rb
165
+ - spec/ranked-model/ranker_spec.rb
166
+ - spec/ranked-model/version_spec.rb
167
+ - spec/spec_helper.rb
168
+ - spec/sti-model/element_spec.rb
169
+ - spec/sti-model/vehicle_spec.rb
170
+ - spec/support/active_record.rb
171
+ - spec/support/database.yml