where_exists 3.0.0 → 3.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 973c5c35ea3476b4178a7e2eb1f6ef93303d731922bf537e0d0d76e02a0f4372
4
- data.tar.gz: 6b30a099f143b8bfbcdcc4ff82b24f4a35c8c566d69c86a68b7b41e9342fd1bd
3
+ metadata.gz: 6107147002f4b860ab5570fc7f23a5fc38ed39425b38c581b1951394986c925c
4
+ data.tar.gz: c7ed7a2d3432032a0c719e8298293ba38d875992b7abe76832654678023eccc5
5
5
  SHA512:
6
- metadata.gz: a4386360276ae78d77f198690e06552a3c29ae08208749f580c4cfc78348b4c9dee005e8bb3f85fc182274926ef724b65daf605d9ba09159731aaf2ed2b8d353
7
- data.tar.gz: 9f47044c93ea46798c0bff7bad10ae3b1558eff50d23c1778c681169ad3979878ba52de97f138f0d1fa0dc639148d16acf9b1320bed128763e49472aa55afae9
6
+ metadata.gz: c4cd31754b8044d2ad4ff7d55e84bd3959fa742a4527c0955994e3ba2797a1f34b6ab1bcd5fb347543fde2d8fc5d2e97e633a13bbdd26b985cb039b45ab220c2
7
+ data.tar.gz: 419074f831ceac51d19aaf53cda7ffc4ebd8d14e4246f5439702e251ab424068c30f9e4f8fc5344235fb03848cd1d4a92452c7007df2d35be399181b66cd2b40
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
data/lib/where_exists.rb CHANGED
@@ -110,7 +110,7 @@ module WhereExists
110
110
  association = association.through_reflection
111
111
 
112
112
  case association.macro
113
- when :has_many, :has_one
113
+ when :has_many, :has_one, :belongs_to
114
114
  return where_exists_for_has_many_query(association, {}, next_association, &block)
115
115
  when :has_and_belongs_to_many
116
116
  return where_exists_for_habtm_query(association, {}, next_association, &block)
@@ -128,10 +128,17 @@ module WhereExists
128
128
  association_scope = next_association[:scope] || association.scope
129
129
 
130
130
  associated_model = association.klass
131
- primary_key = association.options[:primary_key] || self.primary_key
131
+
132
+ if association.macro == :belongs_to
133
+ foreign_key = association.options[:primary_key] || self.primary_key
134
+ primary_key = association.foreign_key
135
+ else
136
+ primary_key = association.options[:primary_key] || self.primary_key
137
+ foreign_key = association.foreign_key
138
+ end
132
139
 
133
140
  self_ids = quote_table_and_column_name(self.table_name, primary_key)
134
- associated_ids = quote_table_and_column_name(associated_model.table_name, association.foreign_key)
141
+ associated_ids = quote_table_and_column_name(associated_model.table_name, foreign_key)
135
142
 
136
143
  result = associated_model.select("1").where("#{associated_ids} = #{self_ids}")
137
144
 
data/test/db/test.db ADDED
Binary file
@@ -0,0 +1,62 @@
1
+ require_relative 'test_helper'
2
+
3
+ ActiveRecord::Migration.create_table :houses, force: true do |t|
4
+ t.string :name
5
+ end
6
+
7
+ ActiveRecord::Migration.create_table :windows, force: true do |t|
8
+ t.string :name
9
+ t.integer :house_id
10
+ end
11
+
12
+ ActiveRecord::Migration.create_table :doors, force: true do |t|
13
+ t.string :name
14
+ t.integer :house_id
15
+ end
16
+
17
+ class House < ActiveRecord::Base
18
+ has_many :windows
19
+ has_many :doors
20
+ end
21
+
22
+ class Window < ActiveRecord::Base
23
+ belongs_to :house
24
+ has_many :doors, through: :house
25
+ end
26
+
27
+ class Door < ActiveRecord::Base
28
+ belongs_to :house
29
+ end
30
+
31
+ class HasManyThroughBelongsToTest < Minitest::Test
32
+ def setup
33
+ ActiveRecord::Base.descendants.each(&:delete_all)
34
+ end
35
+
36
+ def test_through_belongs_to
37
+ house = House.create! name: 'relevant'
38
+ irrelevant_house = House.create! name: 'irrelevant'
39
+
40
+ window = Window.create!(house: house)
41
+ irrelevant_window = Window.create!(house: irrelevant_house)
42
+
43
+ door = Door.create!(name: 'relevant', house: house)
44
+ irrelevant_door = Door.create!(name: 'irrelevant', house: irrelevant_house)
45
+
46
+ result = House.where_exists(:doors, name: 'relevant')
47
+ assert_equal 1, result.length
48
+ assert_equal house.id, result.first.id
49
+
50
+ result = House.where_not_exists(:doors, name: 'relevant')
51
+ assert_equal 1, result.length
52
+ assert_equal irrelevant_house.id, result.first.id
53
+
54
+ result = House.where_not_exists(:doors, "name = ?", 'relevant')
55
+ assert_equal 1, result.length
56
+ assert_equal irrelevant_house.id, result.first.id
57
+
58
+ result = Window.where_exists(:doors, name: 'relevant')
59
+ assert_equal 1, result.length
60
+ assert_equal window.id, result.first.id
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_exists
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-11 00:00:00.000000000 Z
11
+ date: 2025-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -114,17 +114,19 @@ files:
114
114
  - lib/where_exists/version.rb
115
115
  - test/belongs_to_polymorphic_test.rb
116
116
  - test/belongs_to_test.rb
117
+ - test/db/test.db
117
118
  - test/documentation_test.rb
118
119
  - test/has_and_belongs_to_many.rb
119
120
  - test/has_many_polymorphic_test.rb
120
121
  - test/has_many_test.rb
122
+ - test/has_many_through_belongs_to_test.rb
121
123
  - test/has_many_through_test.rb
122
124
  - test/test_helper.rb
123
125
  homepage: http://github.com/eugzol/where_exists
124
126
  licenses:
125
127
  - MIT
126
128
  metadata: {}
127
- post_install_message:
129
+ post_install_message:
128
130
  rdoc_options: []
129
131
  require_paths:
130
132
  - lib
@@ -139,16 +141,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  - !ruby/object:Gem::Version
140
142
  version: '0'
141
143
  requirements: []
142
- rubygems_version: 3.5.20
143
- signing_key:
144
+ rubygems_version: 3.1.6
145
+ signing_key:
144
146
  specification_version: 4
145
147
  summary: "#where_exists extension of ActiveRecord"
146
148
  test_files:
149
+ - test/documentation_test.rb
147
150
  - test/belongs_to_polymorphic_test.rb
148
151
  - test/belongs_to_test.rb
149
- - test/documentation_test.rb
150
- - test/has_and_belongs_to_many.rb
151
152
  - test/has_many_polymorphic_test.rb
152
- - test/has_many_test.rb
153
+ - test/db/test.db
153
154
  - test/has_many_through_test.rb
154
155
  - test/test_helper.rb
156
+ - test/has_many_through_belongs_to_test.rb
157
+ - test/has_and_belongs_to_many.rb
158
+ - test/has_many_test.rb