where_exists 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/where_exists.rb +4 -2
- data/lib/where_exists/version.rb +1 -1
- data/test/belongs_to_test.rb +10 -9
- data/test/db/test.db +0 -0
- data/test/has_many_test.rb +12 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5779b5deb8c1a1c9bcc4685a636f537a0771352a
|
4
|
+
data.tar.gz: 40e44d5113bd916717ac8d2e2e8aa9a8c621075d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a00a2b3f908457473c4347071d19e83c0f7e0b258ee3355ef02b3f07801a066382de7cb4b58d878eaee257a480b1589f0703041804cb61490443d135765ca35a
|
7
|
+
data.tar.gz: b00c7dc5fed3bef71965296ad320a81d8d12d9f161d6cd2e4b8d98ccf72bdb659e146e31da74fc0c3c70dfb15cb3dc90eb293d78b849466993272071980ebc13
|
data/lib/where_exists.rb
CHANGED
@@ -55,7 +55,8 @@ module WhereExists
|
|
55
55
|
self_type = quote_table_and_column_name(self.table_name, association.foreign_type)
|
56
56
|
|
57
57
|
associated_models.each do |associated_model|
|
58
|
-
|
58
|
+
primary_key = association.options[:primary_key] || associated_model.primary_key
|
59
|
+
other_ids = quote_table_and_column_name(associated_model.table_name, primary_key)
|
59
60
|
query = associated_model.select("1").where("#{self_ids} = #{other_ids}").where(where_parameters)
|
60
61
|
if polymorphic
|
61
62
|
other_type = connection.quote(associated_model.name)
|
@@ -76,8 +77,9 @@ module WhereExists
|
|
76
77
|
end
|
77
78
|
|
78
79
|
associated_model = association.klass
|
80
|
+
primary_key = association.options[:primary_key] || self.primary_key
|
79
81
|
|
80
|
-
self_ids = quote_table_and_column_name(self.table_name,
|
82
|
+
self_ids = quote_table_and_column_name(self.table_name, primary_key)
|
81
83
|
associated_ids = quote_table_and_column_name(associated_model.table_name, association.foreign_key)
|
82
84
|
|
83
85
|
result = associated_model.select("1").where("#{associated_ids} = #{self_ids}")
|
data/lib/where_exists/version.rb
CHANGED
data/test/belongs_to_test.rb
CHANGED
@@ -2,19 +2,20 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
ActiveRecord::Migration.create_table :simple_entities, :force => true do |t|
|
4
4
|
t.string :name
|
5
|
+
t.integer :my_id
|
5
6
|
end
|
6
7
|
|
7
8
|
ActiveRecord::Migration.create_table :simple_entity_children, :force => true do |t|
|
8
|
-
t.integer :
|
9
|
+
t.integer :parent_id
|
9
10
|
t.string :name
|
10
11
|
end
|
11
12
|
|
12
13
|
class SimpleEntity < ActiveRecord::Base
|
13
|
-
has_many :simple_entity_children
|
14
|
+
has_many :simple_entity_children, primary_key: :my_id, foreign_key: :parent_id
|
14
15
|
end
|
15
16
|
|
16
17
|
class SimpleEntityChild < ActiveRecord::Base
|
17
|
-
belongs_to :simple_entity
|
18
|
+
belongs_to :simple_entity, foreign_key: :parent_id, primary_key: :my_id
|
18
19
|
end
|
19
20
|
|
20
21
|
class BelongsToTest < Minitest::Unit::TestCase
|
@@ -23,10 +24,10 @@ class BelongsToTest < Minitest::Unit::TestCase
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def test_nil_foreign_key
|
26
|
-
entity = SimpleEntity.create!
|
27
|
+
entity = SimpleEntity.create!(my_id: 999)
|
27
28
|
|
28
|
-
child = SimpleEntityChild.create!(
|
29
|
-
orphaned_child = SimpleEntityChild.create!(
|
29
|
+
child = SimpleEntityChild.create!(parent_id: 999)
|
30
|
+
orphaned_child = SimpleEntityChild.create!(parent_id: nil)
|
30
31
|
|
31
32
|
result = SimpleEntityChild.where_exists(:simple_entity)
|
32
33
|
|
@@ -35,10 +36,10 @@ class BelongsToTest < Minitest::Unit::TestCase
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def test_not_existing_foreign_object
|
38
|
-
entity = SimpleEntity.create!
|
39
|
+
entity = SimpleEntity.create!(my_id: 999)
|
39
40
|
|
40
|
-
child = SimpleEntityChild.create!(
|
41
|
-
orphaned_child = SimpleEntityChild.create!(
|
41
|
+
child = SimpleEntityChild.create!(parent_id: 999)
|
42
|
+
orphaned_child = SimpleEntityChild.create!(parent_id: 500)
|
42
43
|
|
43
44
|
result = SimpleEntityChild.where_exists(:simple_entity)
|
44
45
|
|
data/test/db/test.db
CHANGED
Binary file
|
data/test/has_many_test.rb
CHANGED
@@ -2,19 +2,20 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
ActiveRecord::Migration.create_table :simple_entities, :force => true do |t|
|
4
4
|
t.string :name
|
5
|
+
t.integer :my_id
|
5
6
|
end
|
6
7
|
|
7
8
|
ActiveRecord::Migration.create_table :simple_entity_children, :force => true do |t|
|
8
|
-
t.integer :
|
9
|
+
t.integer :parent_id
|
9
10
|
t.string :name
|
10
11
|
end
|
11
12
|
|
12
13
|
class SimpleEntity < ActiveRecord::Base
|
13
|
-
has_many :simple_entity_children
|
14
|
+
has_many :simple_entity_children, primary_key: :my_id, foreign_key: :parent_id
|
14
15
|
end
|
15
16
|
|
16
17
|
class SimpleEntityChild < ActiveRecord::Base
|
17
|
-
belongs_to :simple_entity
|
18
|
+
belongs_to :simple_entity, foreign_key: :parent_id
|
18
19
|
end
|
19
20
|
|
20
21
|
class HasManyTest < Minitest::Unit::TestCase
|
@@ -25,8 +26,8 @@ class HasManyTest < Minitest::Unit::TestCase
|
|
25
26
|
def test_without_parameters
|
26
27
|
child = SimpleEntityChild.create!
|
27
28
|
|
28
|
-
blank_entity = SimpleEntity.create!
|
29
|
-
filled_entity = SimpleEntity.create!(simple_entity_children: [child])
|
29
|
+
blank_entity = SimpleEntity.create!(my_id: 999)
|
30
|
+
filled_entity = SimpleEntity.create!(simple_entity_children: [child], my_id: 500)
|
30
31
|
|
31
32
|
result = SimpleEntity.where_exists(:simple_entity_children)
|
32
33
|
|
@@ -38,9 +39,9 @@ class HasManyTest < Minitest::Unit::TestCase
|
|
38
39
|
wrong_child = SimpleEntityChild.create!(name: 'wrong')
|
39
40
|
child = SimpleEntityChild.create!(name: 'right')
|
40
41
|
|
41
|
-
blank_entity = SimpleEntity.create!
|
42
|
-
wrong_entity = SimpleEntity.create!(simple_entity_children: [wrong_child])
|
43
|
-
entity = SimpleEntity.create!(name: 'this field is irrelevant', simple_entity_children: [child])
|
42
|
+
blank_entity = SimpleEntity.create!(my_id: 999)
|
43
|
+
wrong_entity = SimpleEntity.create!(simple_entity_children: [wrong_child], my_id: 500)
|
44
|
+
entity = SimpleEntity.create!(name: 'this field is irrelevant', simple_entity_children: [child], my_id: 300)
|
44
45
|
|
45
46
|
result = SimpleEntity.where_exists(:simple_entity_children, name: 'right')
|
46
47
|
|
@@ -50,7 +51,7 @@ class HasManyTest < Minitest::Unit::TestCase
|
|
50
51
|
|
51
52
|
def test_with_scope
|
52
53
|
child = SimpleEntityChild.create!
|
53
|
-
entity = SimpleEntity.create!(simple_entity_children: [child])
|
54
|
+
entity = SimpleEntity.create!(simple_entity_children: [child], my_id: 999)
|
54
55
|
|
55
56
|
result = SimpleEntity.unscoped.where_exists(:simple_entity_children)
|
56
57
|
|
@@ -61,8 +62,8 @@ class HasManyTest < Minitest::Unit::TestCase
|
|
61
62
|
def test_not_exists
|
62
63
|
child = SimpleEntityChild.create!
|
63
64
|
|
64
|
-
blank_entity = SimpleEntity.create!
|
65
|
-
filled_entity = SimpleEntity.create!(simple_entity_children: [child])
|
65
|
+
blank_entity = SimpleEntity.create!(my_id: 999)
|
66
|
+
filled_entity = SimpleEntity.create!(simple_entity_children: [child], my_id: 500)
|
66
67
|
|
67
68
|
result = SimpleEntity.where_not_exists(:simple_entity_children)
|
68
69
|
|
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: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eugene Zolotarev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|