scoped_search 4.1.1 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed2cb536ff3e9eb2ae79adb69c0d16deded93503
4
- data.tar.gz: d71fcf06f4526cca6f0f74c674b700c63cc53710
3
+ metadata.gz: 31cbd8d30fd85c320926ca1ee4719f98e2860211
4
+ data.tar.gz: c502bfc5ef2f14ad4aec19b253b6e9942d316236
5
5
  SHA512:
6
- metadata.gz: 574ce08bfd3a7e6910c52b814c5c0b977e6de7675a80dc53b971915e2fe4619a9e82453dcdafa547d66ff9a6464737a0b26e935ebd4f2f78121ff0061a5096b2
7
- data.tar.gz: 0adcf62f38857c13dd4e49c835b4cfe33821ddf273f5e190ccd557b279ca49512d35700a05549ca9b8eb2c332e73c0ae501daa7fbbccb10da4b5b8720f30429a
6
+ metadata.gz: 018d005d707959d03cabee90cca7c17450e719fe0e6a32124d153e21f709980b7aae8f98be22a2620531d3c07ebacc32e8de8889ef13165e98de4c9162ef9cf0
7
+ data.tar.gz: 4b6997024cb1e5ee01d3c7bc41a34b1ac31f77eae6e09ce885087645f5b530f264baeab5fd74c302ac7a42b38c45fc2740f81741ad03f6cc0ee39adda9cc89bd
data/CHANGELOG.rdoc CHANGED
@@ -8,6 +8,10 @@ Please add an entry to the "Unreleased changes" section in your pull requests.
8
8
 
9
9
  *Nothing yet*
10
10
 
11
+ === Version 4.1.2
12
+
13
+ - Bugfix related to using polymorphic relations introduced in 4.1.1 (#170)
14
+
11
15
  === Version 4.1.1
12
16
 
13
17
  - Bugfix related to quoting when building autocomplete queries for date fields (#167)
@@ -262,17 +262,22 @@ module ScopedSearch
262
262
  def has_many_through_join(field)
263
263
  many_class = field.definition.klass
264
264
  through = definition.reflection_by_name(many_class, field.relation).options[:through]
265
+ through_class = definition.reflection_by_name(many_class, through).klass
265
266
 
266
267
  connection = many_class.connection
267
268
 
268
269
  # table names
269
270
  endpoint_table_name = field.klass.table_name
270
271
  many_table_name = many_class.table_name
271
- middle_table_name = definition.reflection_by_name(many_class, through).klass.table_name
272
+ middle_table_name = through_class.table_name
272
273
 
273
274
  # primary and foreign keys + optional conditions for the joins
274
275
  pk1, fk1 = field.reflection_keys(definition.reflection_by_name(many_class, through))
275
- condition_many_to_middle = field.reflection_conditions(definition.reflection_by_name(field.klass, many_table_name))
276
+ condition_many_to_middle = if with_polymorphism?(many_class, field.klass, through, through_class)
277
+ field.reflection_conditions(definition.reflection_by_name(field.klass, many_table_name))
278
+ else
279
+ ''
280
+ end
276
281
  condition_middle_to_end = field.reflection_conditions(definition.reflection_by_name(field.klass, middle_table_name))
277
282
 
278
283
  # primary and foreign keys + optional condition for the endpoint to middle join
@@ -289,6 +294,13 @@ module ScopedSearch
289
294
  SQL
290
295
  end
291
296
 
297
+ def with_polymorphism?(many_class, endpoint_class, through, through_class)
298
+ reflections = [definition.reflection_by_name(endpoint_class, through), definition.reflection_by_name(many_class, through)].compact
299
+ as = reflections.map(&:options).compact.map { |opt| opt[:as] }.compact
300
+ return false if as.empty?
301
+ definition.reflection_by_name(through_class, as.first).options[:polymorphic]
302
+ end
303
+
292
304
  # This module gets included into the Field class to add SQL generation.
293
305
  module Field
294
306
 
@@ -1,3 +1,3 @@
1
1
  module ScopedSearch
2
- VERSION = "4.1.1"
2
+ VERSION = "4.1.2"
3
3
  end
@@ -479,11 +479,11 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
479
479
  ActiveRecord::Migration.create_table(:user_groups) { |t| t.integer :user_id; t.integer :group_id }
480
480
  ActiveRecord::Migration.create_table(:conflicts) { |t| t.integer :group_id; t.integer :user_id }
481
481
  ActiveRecord::Migration.create_table(:groups) { |t| t.string :related; t.integer :user_id }
482
- ActiveRecord::Migration.create_table(:users) { |t| t.string :foo; t.string :user_type }
482
+ ActiveRecord::Migration.create_table(:users) { |t| t.string :foo }
483
483
 
484
484
  # The related classes
485
485
  class UserGroup < ActiveRecord::Base; belongs_to :user; belongs_to :group; end
486
- class Conflict < ActiveRecord::Base; belongs_to :user, :polymorphic => true; belongs_to :group; end
486
+ class Conflict < ActiveRecord::Base; belongs_to :user; belongs_to :group; end
487
487
  class Group < ActiveRecord::Base
488
488
  has_many :user_groups
489
489
  has_many :users, :through => :conflicts, :source_type => 'User', :source => :user
@@ -497,9 +497,9 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
497
497
  scoped_search :relation => :groups, :on => :related
498
498
  end
499
499
 
500
- @user_1 = User.create!(:foo => 'foo', :user_type => 'User')
501
- @user_2 = User.create!(:foo => 'foo too', :user_type => 'User')
502
- @user_3 = User.create!(:foo => 'foo three', :user_type => 'User')
500
+ @user_1 = User.create!(:foo => 'foo')
501
+ @user_2 = User.create!(:foo => 'foo too')
502
+ @user_3 = User.create!(:foo => 'foo three')
503
503
 
504
504
  @group_1 = Group.create(:related => 'value')
505
505
  @group_2 = Group.create(:related => 'value too!')
@@ -581,16 +581,29 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
581
581
  context 'querying a :has_many => :through with polymorphism' do
582
582
  before do
583
583
  ActiveRecord::Migration.create_table(:subnets) { |t| t.string :name }
584
+ ActiveRecord::Migration.create_table(:domains) { |t| t.string :name }
584
585
  ActiveRecord::Migration.create_table(:taxable_taxonomies) { |t| t.integer :taxable_id; t.integer :taxonomy_id; t.string :taxable_type }
585
586
  ActiveRecord::Migration.create_table(:taxonomies) { |t| t.string :type; t.string :name }
586
587
 
588
+ module Taxonomix
589
+ def self.included(base)
590
+ base.class_eval do
591
+ has_many :taxable_taxonomies, :as => :taxable
592
+ has_many :locations, -> { where(:type => 'Location') }, :through => :taxable_taxonomies, :source => :taxonomy
593
+ has_many :organizations, -> { where(:type => 'Organization') }, :through => :taxable_taxonomies, :source => :taxonomy
594
+
595
+ scoped_search :relation => :locations, :on => :id, :rename => :location_id
596
+ scoped_search :relation => :organizations, :on => :id, :rename => :organization_id
597
+ end
598
+ end
599
+ end
600
+
587
601
  class Subnet < ActiveRecord::Base
588
- has_many :taxable_taxonomies, :as => :taxable
589
- has_many :locations, -> { where(:type => 'Location') }, :through => :taxable_taxonomies, :source => :taxonomy
590
- has_many :organizations, -> { where(:type => 'Organization') }, :through => :taxable_taxonomies, :source => :taxonomy
602
+ include Taxonomix
603
+ end
591
604
 
592
- scoped_search :relation => :locations, :on => :id, :rename => :location_id
593
- scoped_search :relation => :organizations, :on => :id, :rename => :organization_id
605
+ class Domain < ActiveRecord::Base
606
+ include Taxonomix
594
607
  end
595
608
 
596
609
  class TaxableTaxonomy < ActiveRecord::Base
@@ -614,15 +627,23 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
614
627
  @subnet_a = Subnet.create!(:name => 'Subnet A')
615
628
  @subnet_b = Subnet.create!(:name => 'Subnet B')
616
629
 
630
+ @domain_a = Domain.create!(:name => 'Domain A')
631
+ @domain_b = Domain.create!(:name => 'Domain B')
617
632
 
618
633
  TaxableTaxonomy.create!(:taxable_id => @subnet_a.id, :taxonomy_id => @loc_a.id, :taxable_type => 'Subnet')
619
634
  TaxableTaxonomy.create!(:taxable_id => @subnet_b.id, :taxonomy_id => @loc_b.id, :taxable_type => 'Subnet')
620
635
  TaxableTaxonomy.create!(:taxable_id => @subnet_a.id, :taxonomy_id => @org_a.id, :taxable_type => 'Subnet')
621
- TaxableTaxonomy.create!(:taxable_id => @subnet_b.id, :taxonomy_id => @loc_b.id, :taxable_type => 'Subnet')
636
+ TaxableTaxonomy.create!(:taxable_id => @subnet_b.id, :taxonomy_id => @org_b.id, :taxable_type => 'Subnet')
637
+
638
+ TaxableTaxonomy.create!(:taxable_id => @domain_a.id, :taxonomy_id => @loc_a.id, :taxable_type => 'Domain')
639
+ TaxableTaxonomy.create!(:taxable_id => @domain_b.id, :taxonomy_id => @loc_b.id, :taxable_type => 'Domain')
640
+ TaxableTaxonomy.create!(:taxable_id => @domain_a.id, :taxonomy_id => @org_a.id, :taxable_type => 'Domain')
641
+ TaxableTaxonomy.create!(:taxable_id => @domain_b.id, :taxonomy_id => @org_b.id, :taxable_type => 'Domain')
622
642
  end
623
643
 
624
644
  after do
625
645
  ActiveRecord::Migration.drop_table :subnets
646
+ ActiveRecord::Migration.drop_table :domains
626
647
  ActiveRecord::Migration.drop_table :taxable_taxonomies
627
648
  ActiveRecord::Migration.drop_table :taxonomies
628
649
  end
@@ -635,5 +656,69 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
635
656
  Subnet.search_for("organization_id = #{@org_a.id}").length.should == 1
636
657
  end
637
658
  end
659
+
660
+ context 'querying with multiple :has_many => :through and polymorphism' do
661
+ before do
662
+ ActiveRecord::Migration.create_table(:usergroups) { |t| t.string :name }
663
+ ActiveRecord::Migration.create_table(:usergroup_members) { |t| t.integer :usergroup_id; t.integer :member_id; t.string :member_type }
664
+ ActiveRecord::Migration.create_table(:usermats) { |t| t.string :username }
665
+ ActiveRecord::Migration.create_table(:cached_usergroup_members) { |t| t.integer :usergroup_id; t.integer :usermat_id }
666
+
667
+ class Usergroup < ActiveRecord::Base
668
+ has_many :usergroup_members
669
+ has_many :usermats, :through => :usergroup_members, :source => :member, :source_type => 'Usermat'
670
+ has_many :usergroups, :through => :usergroup_members, :source => :member, :source_type => 'Usergroup'
671
+
672
+ has_many :cached_usergroup_members
673
+ has_many :cached_usergroups, :through => :cached_usergroup_members, :source => :usergroup
674
+ has_many :cached_usergroup_members, :foreign_key => 'usergroup_id'
675
+ end
676
+
677
+ class UsergroupMember < ActiveRecord::Base
678
+ belongs_to :member, :polymorphic => true
679
+ belongs_to :usergroup
680
+ end
681
+
682
+ class Usermat < ActiveRecord::Base
683
+ has_many :usergroup_member, :as => :member
684
+ has_many :cached_usergroup_members
685
+ has_many :cached_usergroups, :through => :cached_usergroup_members, :source => :usergroup
686
+
687
+ scoped_search :relation => :cached_usergroups, :on => :name, :rename => :usergroup_name
688
+ end
689
+
690
+ class CachedUsergroupMember < ActiveRecord::Base
691
+ belongs_to :usermat
692
+ belongs_to :usergroup
693
+ end
694
+
695
+ @group_1 = Usergroup.create!(:name => 'first')
696
+ @group_2 = Usergroup.create!(:name => 'second')
697
+ @group_3 = Usergroup.create!(:name => 'third')
698
+ @group_4 = Usergroup.create!(:name => 'fourth')
699
+
700
+ @usermat_1 = Usermat.create(:username => 'user A')
701
+ @usermat_2 = Usermat.create(:username => 'user B')
702
+
703
+ UsergroupMember.create!(:usergroup_id => @group_2.id, :member_id => @group_3.id, :member_type => 'Usergroup')
704
+ UsergroupMember.create!(:usergroup_id => @group_1.id, :member_id => @usermat_2, :member_type => 'Usermat')
705
+ UsergroupMember.create!(:usergroup_id => @group_4.id, :member_id => @usermat_1, :member_type => 'Usermat')
706
+
707
+ CachedUsergroupMember.create!(:usergroup_id => @group_1.id, :usermat_id => @usermat_1.id)
708
+ end
709
+
710
+ after do
711
+ ActiveRecord::Migration.drop_table :usergroups
712
+ ActiveRecord::Migration.drop_table :usergroup_members
713
+ ActiveRecord::Migration.drop_table :usermats
714
+ ActiveRecord::Migration.drop_table :cached_usergroup_members
715
+ end
716
+
717
+ it "should find the usermat when searching on usergroup" do
718
+ result = Usermat.search_for("usergroup_name = #{@group_1.name}")
719
+ result.length.should == 1
720
+ result.first.username.should == @usermat_1.username
721
+ end
722
+ end
638
723
  end
639
724
  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: 4.1.1
4
+ version: 4.1.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: 2017-09-05 00:00:00.000000000 Z
13
+ date: 2017-09-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord