scoped_search 3.2.1 → 3.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8e91bf7d864d576d7020bd56184d9ed7173e5c3
4
- data.tar.gz: 502a182f6303fb040a4b3e77f54d984a85d54093
3
+ metadata.gz: 3291810a245c631791ecafd20c61946c46f7edc1
4
+ data.tar.gz: cb14f9b463fa22b0537f48fcda37ecdd836302bc
5
5
  SHA512:
6
- metadata.gz: ce108bace0ec727d852a4e0d9db9d330bab9d157a4dd985d0bb77e88eff186553462451beaee8fc532ac8d0b95361174b60772511ee7e1016be5c6f3640fe16e
7
- data.tar.gz: c5dcd4e411820161628a61cf0485bf457121ba4ab8338f12d3723f83d8a2938bb1b85469eab096bce6f96f9b76bfb65af58723973de9d63f8e996692cfa4c3b4
6
+ metadata.gz: 43d3a5b905d957079ea970b3f7e936fc453200811edd3017f257fc9f0d052cececffc7ea6e1a4a073ddc909df3b5d0d0b34393aea6c24aebefba69cb4e284f20
7
+ data.tar.gz: 1fe059d3579a6c51bb3c29c977bd956dc0294c6993098bebb4712ece85854ddead8d7d294ff02fb63914db8020cb9e405a1e864ee4b6ee3776b40251b4aa15ea
@@ -246,6 +246,16 @@ module ScopedSearch
246
246
  end
247
247
  end
248
248
 
249
+ def find_has_many_through_association(field, through)
250
+ middle_table_association = nil
251
+ field.klass.reflect_on_all_associations(:has_many).each do |reflection|
252
+ class_name = reflection.options[:class_name].constantize.table_name if reflection.options[:class_name]
253
+ middle_table_association = reflection.name if class_name == through.to_s
254
+ middle_table_association = reflection.plural_name if reflection.plural_name == through.to_s
255
+ end
256
+ middle_table_association
257
+ end
258
+
249
259
  def has_many_through_join(field)
250
260
  many_class = field.definition.klass
251
261
  through = definition.reflection_by_name(many_class, field.relation).options[:through]
@@ -261,7 +271,8 @@ module ScopedSearch
261
271
  condition1 = field.reflection_conditions(definition.reflection_by_name(field.klass, many_table_name))
262
272
 
263
273
  # primary and foreign keys + optional condition for the endpoint to middle join
264
- pk2, fk2 = field.reflection_keys(definition.reflection_by_name(field.klass, middle_table_name))
274
+ middle_table_association = find_has_many_through_association(field, through) || middle_table_name
275
+ pk2, fk2 = field.reflection_keys(definition.reflection_by_name(field.klass, middle_table_association))
265
276
  condition2 = field.reflection_conditions(definition.reflection_by_name(many_class, field.relation))
266
277
 
267
278
  <<-SQL
@@ -1,3 +1,3 @@
1
1
  module ScopedSearch
2
- VERSION = "3.2.1"
2
+ VERSION = "3.2.2"
3
3
  end
@@ -417,7 +417,113 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
417
417
  it "should find the 3 tags that are related to dogs record" do
418
418
  Tag.search_for('baz').length.should == 3
419
419
  end
420
+ end
421
+
422
+
423
+ context 'querying a :has_many => :through relation with alternate name' do
424
+
425
+ before do
426
+
427
+ # Create some tables
428
+ ActiveRecord::Migration.create_table(:zaps) { |t| t.integer :moo_id; t.integer :paz_id }
429
+ ActiveRecord::Migration.create_table(:pazs) { |t| t.string :related }
430
+ ActiveRecord::Migration.create_table(:moos) { |t| t.string :foo }
431
+
432
+ # The related classes
433
+ class Zap < ActiveRecord::Base; belongs_to :paz; belongs_to :moo; end
434
+ class Paz < ActiveRecord::Base; has_many :other_zaps, :class_name => "Zap", :foreign_key => :paz_id; end
435
+
436
+ # The class on which to call search_for
437
+ class Moo < ActiveRecord::Base
438
+ has_many :zaps
439
+ has_many :pazs, :through => :zaps
440
+
441
+ scoped_search :in => :pazs, :on => :related
442
+ end
443
+
444
+ @moo_1 = Moo.create!(:foo => 'foo')
445
+ @moo_2 = Moo.create!(:foo => 'foo too')
446
+ @moo_3 = Moo.create!(:foo => 'foo three')
447
+
448
+ @paz_1 = Paz.create(:related => 'paz')
449
+ @paz_2 = Paz.create(:related => 'paz too!')
450
+
451
+ @bar_1 = Zap.create!(:moo => @moo_1, :paz => @paz_1)
452
+ @bar_2 = Zap.create!(:moo => @moo_1)
453
+ @bar_3 = Zap.create!(:moo => @moo_2, :paz => @paz_1)
454
+ @bar_3 = Zap.create!(:moo => @moo_2, :paz => @paz_2)
455
+ @bar_3 = Zap.create!(:moo => @moo_2, :paz => @paz_2)
456
+ @bar_4 = Zap.create!(:moo => @moo_3)
457
+ end
458
+
459
+ after do
460
+ ActiveRecord::Migration.drop_table(:pazs)
461
+ ActiveRecord::Migration.drop_table(:zaps)
462
+ ActiveRecord::Migration.drop_table(:moos)
463
+ end
464
+
465
+ it "should find the two records that are related to a paz record" do
466
+ Moo.search_for('paz').length.should == 2
467
+ end
468
+
469
+ it "should find the one record that is related to two paz records" do
470
+ Moo.search_for('related=paz AND related="paz too!"').length.should == 1
471
+ end
472
+ end
473
+
474
+
475
+ context 'querying a :has_many => :through relation with modules' do
476
+
477
+ before do
478
+
479
+ # Create some tables with namespaces
480
+ ActiveRecord::Migration.create_table(:zan_mars) { |t| t.integer :koo_id; t.integer :baz_id }
481
+ ActiveRecord::Migration.create_table(:zan_bazs) { |t| t.string :related }
482
+ ActiveRecord::Migration.create_table(:zan_koos) { |t| t.string :foo }
483
+
484
+ # The related classes
485
+ module Zan; class Mar < ActiveRecord::Base; belongs_to :baz; belongs_to :koo; self.table_name = "zan_mars"; end; end
486
+ module Zan; class Baz < ActiveRecord::Base; has_many :mars; self.table_name = "zan_bazs"; end; end
487
+
488
+ # The class on which to call search_for
489
+ module Zan
490
+ class Koo < ActiveRecord::Base
491
+ has_many :mars, :class_name => "Zan::Mar"
492
+ has_many :bazs, :through => :mars
493
+ self.table_name = "zan_koos"
494
+
495
+ scoped_search :in => :bazs, :on => :related
496
+ end
497
+ end
420
498
 
499
+ @koo_1 = Zan::Koo.create!(:foo => 'foo')
500
+ @koo_2 = Zan::Koo.create!(:foo => 'foo too')
501
+ @koo_3 = Zan::Koo.create!(:foo => 'foo three')
502
+
503
+ @baz_1 = Zan::Baz.create(:related => 'baz')
504
+ @baz_2 = Zan::Baz.create(:related => 'baz too!')
505
+
506
+ @bar_1 = Zan::Mar.create!(:koo => @koo_1, :baz => @baz_1)
507
+ @bar_2 = Zan::Mar.create!(:koo => @koo_1)
508
+ @bar_3 = Zan::Mar.create!(:koo => @koo_2, :baz => @baz_1)
509
+ @bar_3 = Zan::Mar.create!(:koo => @koo_2, :baz => @baz_2)
510
+ @bar_3 = Zan::Mar.create!(:koo => @koo_2, :baz => @baz_2)
511
+ @bar_4 = Zan::Mar.create!(:koo => @koo_3)
512
+ end
513
+
514
+ after do
515
+ ActiveRecord::Migration.drop_table(:zan_bazs)
516
+ ActiveRecord::Migration.drop_table(:zan_mars)
517
+ ActiveRecord::Migration.drop_table(:zan_koos)
518
+ end
519
+
520
+ it "should find the two records that are related to a baz record" do
521
+ Zan::Koo.search_for('baz').length.should == 2
522
+ end
523
+
524
+ it "should find the one record that is related to two baz records" do
525
+ Zan::Koo.search_for('related=baz AND related="baz too!"').length.should == 1
526
+ end
421
527
  end
422
528
  end
423
529
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amos Benari
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-06-23 00:00:00.000000000 Z
13
+ date: 2015-07-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord