scoped_search 2.2.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -1
- data/.infinity_test +8 -0
- data/Gemfile +15 -0
- data/lib/scoped_search/auto_complete_builder.rb +251 -0
- data/lib/scoped_search/definition.rb +90 -10
- data/lib/scoped_search/query_builder.rb +209 -45
- data/lib/scoped_search/query_language/parser.rb +4 -2
- data/lib/scoped_search/query_language/tokenizer.rb +3 -3
- data/lib/scoped_search/rails_helper.rb +210 -0
- data/lib/scoped_search.rb +9 -1
- data/scoped_search.gemspec +7 -5
- data/spec/database.yml +13 -6
- data/spec/integration/api_spec.rb +1 -1
- data/spec/integration/auto_complete_spec.rb +140 -0
- data/spec/integration/key_value_querying_spec.rb +87 -0
- data/spec/integration/ordinal_querying_spec.rb +105 -10
- data/spec/integration/profile_querying_spec.rb +1 -1
- data/spec/integration/relation_querying_spec.rb +186 -194
- data/spec/integration/set_query_spec.rb +76 -0
- data/spec/integration/string_querying_spec.rb +19 -2
- data/spec/lib/matchers.rb +10 -0
- data/spec/spec_helper.rb +2 -4
- data/spec/unit/ast_spec.rb +1 -1
- data/spec/unit/auto_complete_builder_spec.rb +20 -0
- data/spec/unit/definition_spec.rb +1 -1
- data/spec/unit/parser_spec.rb +1 -1
- data/spec/unit/query_builder_spec.rb +2 -1
- data/spec/unit/tokenizer_spec.rb +1 -1
- data/tasks/github-gem.rake +18 -14
- metadata +33 -7
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "#{File.dirname(__FILE__)}/../spec_helper"
|
2
2
|
|
3
3
|
# These specs will run on all databases that are defined in the spec/database.yml file.
|
4
4
|
# Comment out any databases that you do not have available for testing purposes if needed.
|
@@ -20,7 +20,7 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
|
|
20
20
|
ScopedSearch::RSpec::Database.close_connection
|
21
21
|
end
|
22
22
|
|
23
|
-
context '
|
23
|
+
context 'querying numerical fields' do
|
24
24
|
|
25
25
|
before(:all) do
|
26
26
|
@record = @class.create!(:int => 9)
|
@@ -102,17 +102,19 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
|
|
102
102
|
@class.search_for('date = 09-01-02').should have(1).item
|
103
103
|
end
|
104
104
|
|
105
|
-
|
106
|
-
|
107
|
-
|
105
|
+
if RUBY_VERSION.to_f == 1.8
|
106
|
+
it "should accept MM/DD/YY as date format" do
|
107
|
+
@class.search_for('date = 01/02/09').should have(1).item
|
108
|
+
end
|
108
109
|
|
110
|
+
it "should accept MM/DD/YYYY as date format" do
|
111
|
+
@class.search_for('date = 01/02/2009').should have(1).item
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
109
115
|
it "should accept YYYY/MM/DD as date format" do
|
110
116
|
@class.search_for('date = 2009/01/02').should have(1).item
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should accept MM/DD/YYYY as date format" do
|
114
|
-
@class.search_for('date = 01/02/2009').should have(1).item
|
115
|
-
end
|
117
|
+
end
|
116
118
|
|
117
119
|
it "should ignore an invalid date and thus return all records" do
|
118
120
|
@class.search_for('>= 2009-14-57').should have(2).items
|
@@ -153,6 +155,99 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
|
|
153
155
|
it "should find no record with a timestamp later than today" do
|
154
156
|
@class.search_for('> 2009-01-02').should have(0).item
|
155
157
|
end
|
158
|
+
|
159
|
+
end
|
160
|
+
context 'humenized date and time query' do
|
161
|
+
|
162
|
+
before(:all) do
|
163
|
+
@curent_record = @class.create!(:timestamp => Time.current, :date => Date.current)
|
164
|
+
@hour_ago_record = @class.create!(:timestamp => Time.current - 1.hour, :date => Date.current)
|
165
|
+
@day_ago_record = @class.create!(:timestamp => Time.current - 1.day, :date => Date.current - 1.day)
|
166
|
+
@month_ago_record = @class.create!(:timestamp => Time.current - 1.month, :date => Date.current - 1.month)
|
167
|
+
@year_ago_record = @class.create!(:timestamp => Time.current - 1.year, :date => Date.current - 1.year)
|
168
|
+
end
|
169
|
+
|
170
|
+
after(:all) do
|
171
|
+
@curent_record.destroy
|
172
|
+
@hour_ago_record.destroy
|
173
|
+
@day_ago_record.destroy
|
174
|
+
@month_ago_record.destroy
|
175
|
+
@year_ago_record.destroy
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should accept Today as date format" do
|
179
|
+
@class.search_for('date = Today').should have(2).item
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should accept Yesterday as date format" do
|
183
|
+
@class.search_for('date = yesterday').should have(1).item
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should find all timestamps and date from today using the = operator" do
|
187
|
+
@class.search_for('= Today').should have(2).item
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should find all timestamps and date from today no operator" do
|
191
|
+
@class.search_for('Today').should have(2).item
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should accept 2 days ago as date format" do
|
195
|
+
@class.search_for('date < "2 days ago"').should have(2).item
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should accept 3 hours ago as date format" do
|
199
|
+
@class.search_for('timestamp > "3 hours ago"').should have(2).item
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should accept 1 month ago as date format" do
|
203
|
+
@class.search_for('date > "1 month ago"').should have(3).item
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should accept 1 year ago as date format" do
|
207
|
+
@class.search_for('date > "1 year ago"').should have(4).item
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'querying bitwize fields' do
|
213
|
+
|
214
|
+
before(:all) do
|
215
|
+
@foo = ScopedSearch::RSpec::Database.create_model(:int => :integer) do |klass|
|
216
|
+
klass.scoped_search :on => :int, :offset => 0, :word_size => 8, :rename => :first
|
217
|
+
klass.scoped_search :on => :int, :offset => 1, :word_size => 8, :rename => :sec
|
218
|
+
end
|
219
|
+
# 1026 => is first = 2 and sec = 4
|
220
|
+
@record = @foo.create!(:int => 1026)
|
221
|
+
end
|
222
|
+
|
223
|
+
after(:all) do
|
224
|
+
ScopedSearch::RSpec::Database.drop_model(@foo)
|
225
|
+
@record.destroy
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should not find any record because first equal = 2" do
|
229
|
+
@foo.search_for('first = 4').should have(0).item
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should find the record" do
|
233
|
+
@foo.search_for('first = 2').should have(1).item
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should not find any record with a grater than operator" do
|
237
|
+
@foo.search_for('first > 9').should have(0).item
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should find the record with an >= operator" do
|
241
|
+
@foo.search_for('sec >= 4').should have(1).item
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should find the record with AND operator is used" do
|
245
|
+
@foo.search_for('sec <= 8 and first = 2').should have(1).item
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should return the record in if one predicate is true and OR is used as operator" do
|
249
|
+
@foo.search_for('sec <= 8 || first > 8').should have(1).item
|
250
|
+
end
|
156
251
|
end
|
157
252
|
end
|
158
253
|
end
|
@@ -1,265 +1,257 @@
|
|
1
|
-
require
|
1
|
+
require "#{File.dirname(__FILE__)}/../spec_helper"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
ScopedSearch::RSpec::Database.test_databases.each do |db|
|
3
|
+
# These specs will run on all databases that are defined in the spec/database.yml file.
|
4
|
+
# Comment out any databases that you do not have available for testing purposes if needed.
|
5
|
+
ScopedSearch::RSpec::Database.test_databases.each do |db|
|
7
6
|
|
8
|
-
|
7
|
+
describe ScopedSearch, "using a #{db} database" do
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
after(:all) do
|
15
|
-
ScopedSearch::RSpec::Database.close_connection
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'querying a :belongs_to relation' do
|
19
|
-
|
20
|
-
before(:all) do
|
9
|
+
before(:all) do
|
10
|
+
ScopedSearch::RSpec::Database.establish_named_connection(db)
|
11
|
+
end
|
21
12
|
|
22
|
-
|
23
|
-
|
24
|
-
|
13
|
+
after(:all) do
|
14
|
+
ScopedSearch::RSpec::Database.close_connection
|
15
|
+
end
|
25
16
|
|
26
|
-
|
27
|
-
::Foo = ScopedSearch::Spec::Database.create_model(:foo => :string, :bar_id => :integer) do |klass|
|
28
|
-
klass.belongs_to :bar
|
29
|
-
klass.scoped_search :in => :bar, :on => :related
|
30
|
-
end
|
17
|
+
context 'querying a :belongs_to relation' do
|
31
18
|
|
32
|
-
|
19
|
+
before do
|
20
|
+
|
21
|
+
# The related class
|
22
|
+
ActiveRecord::Migration.create_table(:hars) { |t| t.string :related }
|
23
|
+
class Har < ActiveRecord::Base; has_many :loos; end
|
33
24
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
# The class on which to call search_for
|
26
|
+
ActiveRecord::Migration.create_table(:loos) { |t| t.string :foo; t.integer :har_id }
|
27
|
+
class Loo < ActiveRecord::Base
|
28
|
+
belongs_to :har
|
29
|
+
scoped_search :in => :har, :on => :related
|
38
30
|
end
|
39
31
|
|
40
|
-
|
41
|
-
ScopedSearch::RSpec::Database.drop_model(Bar)
|
42
|
-
ScopedSearch::RSpec::Database.drop_model(Foo)
|
43
|
-
Object.send :remove_const, :Foo
|
44
|
-
Object.send :remove_const, :Bar
|
45
|
-
end
|
32
|
+
@har_record = Har.create!(:related => 'bar')
|
46
33
|
|
47
|
-
|
48
|
-
|
49
|
-
|
34
|
+
Loo.create!(:foo => 'foo', :har => @har_record)
|
35
|
+
Loo.create!(:foo => 'foo too', :har => @har_record)
|
36
|
+
Loo.create!(:foo => 'foo three', :har => Har.create!(:related => 'another bar'))
|
37
|
+
Loo.create!(:foo => 'foo four')
|
38
|
+
end
|
50
39
|
|
51
|
-
|
52
|
-
|
53
|
-
|
40
|
+
after do
|
41
|
+
ScopedSearch::RSpec::Database.drop_model(Har)
|
42
|
+
ScopedSearch::RSpec::Database.drop_model(Loo)
|
43
|
+
end
|
54
44
|
|
55
|
-
|
56
|
-
|
57
|
-
end
|
45
|
+
it "should find all records with a related bar record containing bar" do
|
46
|
+
Loo.search_for('bar').should have(3).items
|
58
47
|
end
|
59
48
|
|
60
|
-
|
49
|
+
it "should find all records with a related bar record having an exact value of bar with an explicit field" do
|
50
|
+
Loo.search_for('related = bar').should have(2).items
|
51
|
+
end
|
61
52
|
|
62
|
-
|
53
|
+
it "should find records for which the bar relation is not set using null?" do
|
54
|
+
Loo.search_for('null? related').should have(1).items
|
55
|
+
end
|
63
56
|
|
64
|
-
|
65
|
-
|
66
|
-
|
57
|
+
it "should find records for which the bar relation is not set using null?" do
|
58
|
+
Loo.search_for('',:order => 'related asc').first.foo.should eql('foo four')
|
59
|
+
end
|
67
60
|
|
68
|
-
|
69
|
-
::Foo = ScopedSearch::Spec::Database.create_model(:foo => :string, :bar_id => :integer) do |klass|
|
70
|
-
klass.has_many :bars
|
71
|
-
klass.scoped_search :in => :bars, :on => :related
|
72
|
-
end
|
61
|
+
end
|
73
62
|
|
74
|
-
|
75
|
-
@foo_2 = Foo.create!(:foo => 'foo too')
|
76
|
-
@foo_3 = Foo.create!(:foo => 'foo three')
|
63
|
+
context 'querying a :has_many relation' do
|
77
64
|
|
78
|
-
|
79
|
-
Bar.create!(:related => 'another bar', :foo => @foo_1)
|
80
|
-
Bar.create!(:related => 'other bar', :foo => @foo_2)
|
81
|
-
end
|
65
|
+
before do
|
82
66
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
Object.send :remove_const, :Foo
|
87
|
-
Object.send :remove_const, :Bar
|
88
|
-
end
|
67
|
+
# The related class
|
68
|
+
ActiveRecord::Migration.create_table(:jars) { |t| t.string :related; t.integer :goo_id }
|
69
|
+
class Jar < ActiveRecord::Base; belongs_to :goo; end
|
89
70
|
|
90
|
-
|
91
|
-
|
71
|
+
# The class on which to call search_for
|
72
|
+
ActiveRecord::Migration.create_table(:goos) { |t| t.string :foo }
|
73
|
+
class Goo < ActiveRecord::Base
|
74
|
+
has_many :jars
|
75
|
+
scoped_search :in => :jars, :on => :related
|
92
76
|
end
|
93
77
|
|
94
|
-
|
95
|
-
|
96
|
-
|
78
|
+
@foo_1 = Goo.create!(:foo => 'foo')
|
79
|
+
@foo_2 = Goo.create!(:foo => 'foo too')
|
80
|
+
@foo_3 = Goo.create!(:foo => 'foo three')
|
97
81
|
|
98
|
-
|
99
|
-
|
100
|
-
|
82
|
+
Jar.create!(:related => 'bar', :goo => @foo_1)
|
83
|
+
Jar.create!(:related => 'another bar', :goo => @foo_1)
|
84
|
+
Jar.create!(:related => 'other bar', :goo => @foo_2)
|
85
|
+
end
|
101
86
|
|
102
|
-
|
103
|
-
|
104
|
-
|
87
|
+
after do
|
88
|
+
ScopedSearch::RSpec::Database.drop_model(Jar)
|
89
|
+
ScopedSearch::RSpec::Database.drop_model(Goo)
|
90
|
+
end
|
105
91
|
|
92
|
+
it "should find all records with at least one bar record containing 'bar'" do
|
93
|
+
::Goo.search_for('bar').should have(2).items
|
106
94
|
end
|
107
95
|
|
108
|
-
|
96
|
+
it "should find the only record with at least one bar record having the exact value 'bar'" do
|
97
|
+
::Goo.search_for('= bar').should have(1).item
|
98
|
+
end
|
109
99
|
|
110
|
-
|
100
|
+
it "should find all records for which at least one related bar record exists" do
|
101
|
+
::Goo.search_for('set? related').should have(2).items
|
102
|
+
end
|
111
103
|
|
112
|
-
|
113
|
-
|
114
|
-
|
104
|
+
it "should find all records for which none related bar records exist" do
|
105
|
+
::Goo.search_for('null? related').should have(1).items
|
106
|
+
end
|
115
107
|
|
116
|
-
|
117
|
-
::Foo = ScopedSearch::Spec::Database.create_model(:foo => :string) do |klass|
|
118
|
-
klass.has_one :bar
|
119
|
-
klass.scoped_search :in => :bar, :on => :related
|
120
|
-
end
|
108
|
+
end
|
121
109
|
|
122
|
-
|
123
|
-
@foo_2 = ::Foo.create!(:foo => 'foo too')
|
124
|
-
@foo_3 = ::Foo.create!(:foo => 'foo three')
|
110
|
+
context 'querying a :has_one relation' do
|
125
111
|
|
126
|
-
|
127
|
-
::Bar.create!(:related => 'other bar', :foo => @foo_2)
|
128
|
-
end
|
112
|
+
before do
|
129
113
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
Object.send :remove_const, :Bar
|
135
|
-
end
|
114
|
+
# The related class
|
115
|
+
ActiveRecord::Migration.drop_table(:cars) rescue nil
|
116
|
+
ActiveRecord::Migration.create_table(:cars) { |t| t.string :related; t.integer :hoo_id }
|
117
|
+
class Car < ActiveRecord::Base; belongs_to :hoo; end
|
136
118
|
|
137
|
-
|
138
|
-
|
119
|
+
# The class on which to call search_for
|
120
|
+
ActiveRecord::Migration.create_table(:hoos) { |t| t.string :foo }
|
121
|
+
class Hoo < ActiveRecord::Base
|
122
|
+
has_one :car
|
123
|
+
scoped_search :on => :foo
|
124
|
+
scoped_search :in => :car, :on => :related
|
139
125
|
end
|
140
126
|
|
141
|
-
|
142
|
-
|
143
|
-
|
127
|
+
@hoo_1 = Hoo.create!(:foo => 'foo')
|
128
|
+
@hoo_2 = Hoo.create!(:foo => 'foo too')
|
129
|
+
@hoo_3 = Hoo.create!(:foo => 'foo three')
|
144
130
|
|
145
|
-
|
146
|
-
|
147
|
-
|
131
|
+
Car.create!(:related => 'bar', :hoo => @hoo_1)
|
132
|
+
Car.create!(:related => 'other bar', :hoo => @hoo_2)
|
133
|
+
end
|
148
134
|
|
149
|
-
|
150
|
-
|
151
|
-
|
135
|
+
after do
|
136
|
+
ScopedSearch::RSpec::Database.drop_model(Car)
|
137
|
+
ScopedSearch::RSpec::Database.drop_model(Hoo)
|
152
138
|
end
|
153
139
|
|
154
|
-
|
140
|
+
it "should find all records with a car record containing 'bar" do
|
141
|
+
Hoo.search_for('bar').should have(2).items
|
142
|
+
end
|
155
143
|
|
156
|
-
|
144
|
+
it "should find the only record with the bar record has the exact value 'bar" do
|
145
|
+
Hoo.search_for('= bar').should have(1).item
|
146
|
+
end
|
157
147
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
ActiveRecord::Migration.create_table(:foos) { |t| t.string :foo }
|
148
|
+
it "should find all records for which the related bar record exists" do
|
149
|
+
Hoo.search_for('set? related').should have(2).items
|
150
|
+
end
|
162
151
|
|
163
|
-
|
164
|
-
|
152
|
+
it "should find all records for which the related bar record does not exist" do
|
153
|
+
Hoo.search_for('null? related').should have(1).items
|
154
|
+
end
|
155
|
+
end
|
165
156
|
|
166
|
-
|
167
|
-
class ::Foo < ActiveRecord::Base
|
168
|
-
has_and_belongs_to_many :bars
|
169
|
-
scoped_search :in => :bars, :on => :related
|
170
|
-
end
|
157
|
+
context 'querying a :has_and_belongs_to_many relation' do
|
171
158
|
|
172
|
-
|
173
|
-
@foo_2 = ::Foo.create!(:foo => 'foo too')
|
174
|
-
@foo_3 = ::Foo.create!(:foo => 'foo three')
|
159
|
+
before do
|
175
160
|
|
176
|
-
|
177
|
-
|
178
|
-
|
161
|
+
# Create some tables
|
162
|
+
ActiveRecord::Migration.create_table(:dars) { |t| t.string :related }
|
163
|
+
ActiveRecord::Migration.create_table(:dars_joos, :id => false) { |t| t.integer :joo_id; t.integer :dar_id }
|
164
|
+
ActiveRecord::Migration.create_table(:joos) { |t| t.string :foo }
|
179
165
|
|
180
|
-
|
181
|
-
|
182
|
-
end
|
166
|
+
# The related class
|
167
|
+
class Dar < ActiveRecord::Base; end
|
183
168
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
Object.send :remove_const, :Foo
|
189
|
-
Object.send :remove_const, :Bar
|
169
|
+
# The class on which to call search_for
|
170
|
+
class Joo < ActiveRecord::Base
|
171
|
+
has_and_belongs_to_many :dars
|
172
|
+
scoped_search :in => :dars, :on => :related
|
190
173
|
end
|
191
174
|
|
192
|
-
|
193
|
-
|
194
|
-
|
175
|
+
@joo_1 = Joo.create!(:foo => 'foo')
|
176
|
+
@joo_2 = Joo.create!(:foo => 'foo too')
|
177
|
+
@joo_3 = Joo.create!(:foo => 'foo three')
|
195
178
|
|
196
|
-
|
197
|
-
|
198
|
-
|
179
|
+
@dar_1 = Dar.create!(:related => 'bar')
|
180
|
+
@dar_2 = Dar.create!(:related => 'other bar')
|
181
|
+
@dar_3 = Dar.create!(:related => 'last bar')
|
199
182
|
|
200
|
-
|
201
|
-
|
202
|
-
|
183
|
+
@joo_1.dars << @dar_1 << @dar_2
|
184
|
+
@joo_2.dars << @dar_2 << @dar_3
|
185
|
+
end
|
203
186
|
|
204
|
-
|
205
|
-
|
206
|
-
|
187
|
+
after do
|
188
|
+
ActiveRecord::Migration.drop_table(:dars_joos)
|
189
|
+
ActiveRecord::Migration.drop_table(:dars)
|
190
|
+
ActiveRecord::Migration.drop_table(:joos)
|
207
191
|
end
|
208
192
|
|
209
|
-
|
193
|
+
it "should find all records with at least one associated bar record containing 'bar'" do
|
194
|
+
Joo.search_for('bar').should have(2).items
|
195
|
+
end
|
210
196
|
|
211
|
-
|
197
|
+
it "should find record which is related to @bar_1" do
|
198
|
+
Joo.search_for('= bar').should have(1).items
|
199
|
+
end
|
212
200
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
ActiveRecord::Migration.create_table(:foos) { |t| t.string :foo }
|
201
|
+
it "should find the only record related to @bar_3" do
|
202
|
+
Joo.search_for('last').should have(1).items
|
203
|
+
end
|
217
204
|
|
218
|
-
|
219
|
-
|
220
|
-
|
205
|
+
it "should find all records that are related to @bar_2" do
|
206
|
+
Joo.search_for('other').should have(2).items
|
207
|
+
end
|
208
|
+
end
|
221
209
|
|
222
|
-
|
223
|
-
class ::Foo < ActiveRecord::Base
|
224
|
-
has_many :bars
|
225
|
-
has_many :bazs, :through => :bars
|
210
|
+
context 'querying a :has_many => :through relation' do
|
226
211
|
|
227
|
-
|
228
|
-
end
|
212
|
+
before do
|
229
213
|
|
230
|
-
|
231
|
-
|
232
|
-
|
214
|
+
# Create some tables
|
215
|
+
ActiveRecord::Migration.create_table(:mars) { |t| t.integer :koo_id; t.integer :baz_id }
|
216
|
+
ActiveRecord::Migration.create_table(:bazs) { |t| t.string :related }
|
217
|
+
ActiveRecord::Migration.create_table(:koos) { |t| t.string :foo }
|
233
218
|
|
234
|
-
|
235
|
-
|
219
|
+
# The related classes
|
220
|
+
class Mar < ActiveRecord::Base; belongs_to :baz; belongs_to :koo; end
|
221
|
+
class Baz < ActiveRecord::Base; has_many :mars; end
|
236
222
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
@bar_3 = ::Bar.create!(:foo => @foo_2, :baz => @baz_2)
|
242
|
-
@bar_4 = ::Bar.create!(:foo => @foo_3)
|
243
|
-
end
|
223
|
+
# The class on which to call search_for
|
224
|
+
class Koo < ActiveRecord::Base
|
225
|
+
has_many :mars
|
226
|
+
has_many :bazs, :through => :mars
|
244
227
|
|
245
|
-
|
246
|
-
ActiveRecord::Migration.drop_table(:bazs)
|
247
|
-
ActiveRecord::Migration.drop_table(:bars)
|
248
|
-
ActiveRecord::Migration.drop_table(:foos)
|
249
|
-
Object.send :remove_const, :Foo
|
250
|
-
Object.send :remove_const, :Bar
|
251
|
-
Object.send :remove_const, :Baz
|
228
|
+
scoped_search :in => :bazs, :on => :related
|
252
229
|
end
|
253
230
|
|
254
|
-
|
255
|
-
|
256
|
-
|
231
|
+
@koo_1 = Koo.create!(:foo => 'foo')
|
232
|
+
@koo_2 = Koo.create!(:foo => 'foo too')
|
233
|
+
@koo_3 = Koo.create!(:foo => 'foo three')
|
234
|
+
|
235
|
+
@baz_1 = Baz.create(:related => 'baz')
|
236
|
+
@baz_2 = Baz.create(:related => 'baz too!')
|
237
|
+
|
238
|
+
@bar_1 = Mar.create!(:koo => @koo_1, :baz => @baz_1)
|
239
|
+
@bar_2 = Mar.create!(:koo => @koo_1)
|
240
|
+
@bar_3 = Mar.create!(:koo => @koo_2, :baz => @baz_1)
|
241
|
+
@bar_3 = Mar.create!(:koo => @koo_2, :baz => @baz_2)
|
242
|
+
@bar_3 = Mar.create!(:koo => @koo_2, :baz => @baz_2)
|
243
|
+
@bar_4 = Mar.create!(:koo => @koo_3)
|
244
|
+
end
|
245
|
+
|
246
|
+
after do
|
247
|
+
ActiveRecord::Migration.drop_table(:bazs)
|
248
|
+
ActiveRecord::Migration.drop_table(:mars)
|
249
|
+
ActiveRecord::Migration.drop_table(:koos)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should find the two records that are related to a baz record" do
|
253
|
+
Koo.search_for('baz').should have(2).items
|
257
254
|
end
|
258
255
|
end
|
259
256
|
end
|
260
|
-
else
|
261
|
-
puts
|
262
|
-
puts "WARNING:"
|
263
|
-
puts "Currently, relationships querying is only 100% supported on Rails 2.x. Use at your own risk when using Rails 3."
|
264
|
-
puts
|
265
257
|
end
|