where_exists 1.0.1 → 1.1.0

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: 74e2e9d06491fe5983970c707ff517e6bddcddbe
4
- data.tar.gz: 7124e05a2566dcf93df5942e4311edb012d70c2f
3
+ metadata.gz: b896dc7f4e36987c9fee2d3f99624944241c5778
4
+ data.tar.gz: 3b63cf040cc3eccc90b22ffb167815cbc6c26762
5
5
  SHA512:
6
- metadata.gz: b0dfa7573e12a6a5c163e79c47ed9552b0fdc9331a8670adea62a5dedb0e61e9e79d18a66dfd56b6205476646217d8aaa71f8002fdd1d5373325da9923a5b8dc
7
- data.tar.gz: 2debc428fa41ccb5fe7a6ca37ade8bdb93684e75a6dac4639a805a656387d38ab9c7d38d0d29b98b619ed76411393112646223167097c484801893cf69255c7a
6
+ metadata.gz: f41ff3048e44820bd889403986a48ad90f95e1b3c97b0cfcd43a5797b2c5fdf706b8c6ac21ff3c9655489610a1ec38675001fd10ad5452237b0d46862f02660b
7
+ data.tar.gz: 744a933a1fcc1fc3bd5251fc1b686c3009d4b7894996a64bef294947d18ed283552b00a225213d3181ae9e8768f976fede2a26aec03c05e6910a35407c7d9003
@@ -1,11 +1,11 @@
1
1
  require 'active_record'
2
2
 
3
3
  module WhereExists
4
- def where_exists(association_name, where_parameters = {})
4
+ def where_exists(association_name, *where_parameters)
5
5
  where_exists_or_not_exists(true, association_name, where_parameters)
6
6
  end
7
7
 
8
- def where_not_exists(association_name, where_parameters = {})
8
+ def where_not_exists(association_name, *where_parameters)
9
9
  where_exists_or_not_exists(false, association_name, where_parameters)
10
10
  end
11
11
 
@@ -33,8 +33,6 @@ module WhereExists
33
33
  not_string = "NOT "
34
34
  end
35
35
 
36
- #queries.map!{|query| query.select(ActiveRecord::FinderMethods::ONE_AS_ONE).where(where_parameters)}
37
-
38
36
  queries_sql = queries.map{|query| "EXISTS (" + query.to_sql + ")"}.join(" OR ")
39
37
 
40
38
  self.where("#{not_string}(#{queries_sql})")
@@ -43,6 +41,8 @@ module WhereExists
43
41
  def where_exists_for_belongs_to_query(association, where_parameters)
44
42
  polymorphic = association.options[:polymorphic].present?
45
43
 
44
+ association_scope = association.scope
45
+
46
46
  if polymorphic
47
47
  associated_models = self.select("DISTINCT #{connection.quote_column_name(association.foreign_type)}").pluck(association.foreign_type).map(&:constantize)
48
48
  else
@@ -57,7 +57,13 @@ module WhereExists
57
57
  associated_models.each do |associated_model|
58
58
  primary_key = association.options[:primary_key] || associated_model.primary_key
59
59
  other_ids = quote_table_and_column_name(associated_model.table_name, primary_key)
60
- query = associated_model.select("1").where("#{self_ids} = #{other_ids}").where(where_parameters)
60
+ query = associated_model.select("1").where("#{self_ids} = #{other_ids}")
61
+ if where_parameters != []
62
+ query = query.where(*where_parameters)
63
+ end
64
+ if association_scope
65
+ result = result.instance_exec(&association_scope)
66
+ end
61
67
  if polymorphic
62
68
  other_type = connection.quote(associated_model.name)
63
69
  query = query.where("#{self_type} = #{other_type}")
@@ -71,6 +77,8 @@ module WhereExists
71
77
  def where_exists_for_has_many_query(association, where_parameters)
72
78
  through = association.options[:through].present?
73
79
 
80
+ association_scope = association.scope
81
+
74
82
  if through
75
83
  next_association = association.source_reflection
76
84
  association = association.through_reflection
@@ -84,6 +92,10 @@ module WhereExists
84
92
 
85
93
  result = associated_model.select("1").where("#{associated_ids} = #{self_ids}")
86
94
 
95
+ if association_scope
96
+ result = result.instance_exec(&association_scope)
97
+ end
98
+
87
99
  if association.options[:as]
88
100
  other_types = quote_table_and_column_name(associated_model.table_name, association.type)
89
101
  self_class = connection.quote(self.name)
@@ -91,9 +103,11 @@ module WhereExists
91
103
  end
92
104
 
93
105
  if through
94
- result = result.where_exists(next_association.name, where_parameters)
106
+ result = result.where_exists(next_association.name, *where_parameters)
95
107
  else
96
- result = result.where(where_parameters)
108
+ if where_parameters != []
109
+ result = result.where(*where_parameters)
110
+ end
97
111
  end
98
112
 
99
113
  [result]
@@ -106,4 +120,4 @@ end
106
120
 
107
121
  class ActiveRecord::Base
108
122
  extend WhereExists
109
- end
123
+ end
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -19,11 +19,11 @@ class BelongsToPolymorphicChild < ActiveRecord::Base
19
19
  end
20
20
 
21
21
  class FirstPolymorphicEntity < ActiveRecord::Base
22
- has_many :children, as: :polymorphic_entity, class_name: BelongsToPolymorphicChild
22
+ has_many :children, as: :polymorphic_entity, class_name: 'BelongsToPolymorphicChild'
23
23
  end
24
24
 
25
25
  class SecondPolymorphicEntity < ActiveRecord::Base
26
- has_many :children, as: :polymorphic_entity, class_name: BelongsToPolymorphicChild
26
+ has_many :children, as: :polymorphic_entity, class_name: 'BelongsToPolymorphicChild'
27
27
  end
28
28
 
29
29
  class BelongsToPolymorphicTest < Minitest::Test
@@ -11,11 +11,12 @@ ActiveRecord::Migration.create_table :belongs_to_simple_entity_children, :force
11
11
  end
12
12
 
13
13
  class BelongsToSimpleEntity < ActiveRecord::Base
14
- has_many :simple_entity_children, primary_key: :my_id, foreign_key: :parent_id, class_name: "BelongsToSimpleEntityChild"
14
+ has_many :simple_entity_children, primary_key: :my_id, foreign_key: :parent_id, class_name: 'BelongsToSimpleEntityChild'
15
+ has_many :unnamed_children, -> { where name: nil }, primary_key: :my_id, foreign_key: :parent_id, class_name: 'BelongsToSimpleEntityChild'
15
16
  end
16
17
 
17
18
  class BelongsToSimpleEntityChild < ActiveRecord::Base
18
- belongs_to :simple_entity, foreign_key: :parent_id, primary_key: :my_id, class_name: "BelongsToSimpleEntity"
19
+ belongs_to :simple_entity, foreign_key: :parent_id, primary_key: :my_id, class_name: 'BelongsToSimpleEntity'
19
20
  end
20
21
 
21
22
  class BelongsToTest < Minitest::Test
@@ -46,4 +47,34 @@ class BelongsToTest < Minitest::Test
46
47
  assert_equal 1, result.length
47
48
  assert_equal result.first.id, child.id
48
49
  end
50
+
51
+ def test_with_parameters
52
+ wrong_child = BelongsToSimpleEntityChild.create!(name: 'wrong')
53
+ child = BelongsToSimpleEntityChild.create!(name: 'right')
54
+
55
+ _blank_entity = BelongsToSimpleEntity.create!(my_id: 999)
56
+ _wrong_entity = BelongsToSimpleEntity.create!(simple_entity_children: [wrong_child], my_id: 500)
57
+ entity = BelongsToSimpleEntity.create!(name: 'this field is irrelevant', simple_entity_children: [child], my_id: 300)
58
+
59
+ result = BelongsToSimpleEntity.where_exists(:simple_entity_children, name: 'right')
60
+
61
+ assert_equal 1, result.length
62
+ assert_equal result.first.id, entity.id
63
+ end
64
+
65
+ def test_with_condition
66
+ child_1 = BelongsToSimpleEntityChild.create! name: nil
67
+ child_2 = BelongsToSimpleEntityChild.create! name: 'Luke'
68
+
69
+ entity_1 = BelongsToSimpleEntity.create!(simple_entity_children: [child_1], my_id: 999)
70
+ entity_2 = BelongsToSimpleEntity.create!(simple_entity_children: [child_2], my_id: 500)
71
+
72
+ result = BelongsToSimpleEntity.unscoped.where_exists(:unnamed_children)
73
+ assert_equal 1, result.length
74
+ assert_equal result.first.id, entity_1.id
75
+
76
+ result = BelongsToSimpleEntity.unscoped.where_not_exists(:unnamed_children)
77
+ assert_equal 1, result.length
78
+ assert_equal result.first.id, entity_2.id
79
+ end
49
80
  end
Binary file
@@ -19,11 +19,11 @@ class HasManyPolymorphicChild < ActiveRecord::Base
19
19
  end
20
20
 
21
21
  class RelevantPolymorphicEntity < ActiveRecord::Base
22
- has_many :children, as: :polymorphic_thing, class_name: HasManyPolymorphicChild
22
+ has_many :children, as: :polymorphic_thing, class_name: 'HasManyPolymorphicChild'
23
23
  end
24
24
 
25
25
  class IrrelevantPolymorphicEntity < ActiveRecord::Base
26
- has_many :children, as: :polymorphic_thing, class_name: HasManyPolymorphicChild
26
+ has_many :children, as: :polymorphic_thing, class_name: 'HasManyPolymorphicChild'
27
27
  end
28
28
 
29
29
  class HasManyPolymorphicTest < Minitest::Test
@@ -12,6 +12,7 @@ end
12
12
 
13
13
  class SimpleEntity < ActiveRecord::Base
14
14
  has_many :simple_entity_children, primary_key: :my_id, foreign_key: :parent_id
15
+ has_many :unnamed_children, -> { where name: nil }, primary_key: :my_id, foreign_key: :parent_id, class_name: 'SimpleEntityChild'
15
16
  end
16
17
 
17
18
  class SimpleEntityChild < ActiveRecord::Base
@@ -59,6 +60,22 @@ class HasManyTest < Minitest::Test
59
60
  assert_equal result.first.id, entity.id
60
61
  end
61
62
 
63
+ def test_with_condition
64
+ child_1 = SimpleEntityChild.create! name: nil
65
+ child_2 = SimpleEntityChild.create! name: 'Luke'
66
+
67
+ entity_1 = SimpleEntity.create!(simple_entity_children: [child_1], my_id: 999)
68
+ entity_2 = SimpleEntity.create!(simple_entity_children: [child_2], my_id: 500)
69
+
70
+ result = SimpleEntity.unscoped.where_exists(:unnamed_children)
71
+ assert_equal 1, result.length
72
+ assert_equal result.first.id, entity_1.id
73
+
74
+ result = SimpleEntity.unscoped.where_not_exists(:unnamed_children)
75
+ assert_equal 1, result.length
76
+ assert_equal result.first.id, entity_2.id
77
+ end
78
+
62
79
  def test_not_exists
63
80
  child = SimpleEntityChild.create!
64
81
 
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: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2018-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.22
19
+ version: '4.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '6'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 3.2.22
29
+ version: '4.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '6'
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.5.1
100
+ rubygems_version: 2.6.11
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: "#where_exists extension of ActiveRecord"