where_exists 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +32 -0
- data/lib/where_exists.rb +110 -0
- data/lib/where_exists/version.rb +3 -0
- data/test/belongs_to_polymorphic_test.rb +63 -0
- data/test/belongs_to_test.rb +48 -0
- data/test/db/test.db +0 -0
- data/test/has_many_polymorphic_test.rb +44 -0
- data/test/has_many_test.rb +72 -0
- data/test/has_many_through_test.rb +95 -0
- data/test/test_helper.rb +14 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3183fa7ecdfc73c32ee05f640fcd732b86a6b2a4
|
4
|
+
data.tar.gz: c6a3fec1c884fa8a817d0cf89827644accc03e03
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6270202fd65580dff8c02681a877f79a5627e8c5e5477fa90e69ec2572ac8d90bc9941f2ff8bfb0bb219168dd3fb707c6990e49cdc1fbea845bc3d5611ed1f78
|
7
|
+
data.tar.gz: 9a1aeac1ec732eee43e42760ba2013351960b86a87ee5282a1658f10a84ea21782b2822094c422644ff399dc59cd4e13d649e5f357f47228f36e169762225b29
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Eugene Zolotarev
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'WhereExists'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
data/lib/where_exists.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module WhereExists
|
4
|
+
def where_exists(association_name, where_parameters = {})
|
5
|
+
where_exists_or_not_exists(true, association_name, where_parameters)
|
6
|
+
end
|
7
|
+
|
8
|
+
def where_not_exists(association_name, where_parameters = {})
|
9
|
+
where_exists_or_not_exists(false, association_name, where_parameters)
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def where_exists_or_not_exists(does_exist, association_name, where_parameters)
|
15
|
+
association = self.reflect_on_association(association_name)
|
16
|
+
|
17
|
+
unless association
|
18
|
+
raise ArgumentError.new("where_exists: association #{association_name.inspect} not found on #{self.name}")
|
19
|
+
end
|
20
|
+
|
21
|
+
case association.macro
|
22
|
+
when :belongs_to
|
23
|
+
queries = where_exists_for_belongs_to_query(association, where_parameters)
|
24
|
+
when :has_many, :has_one
|
25
|
+
queries = where_exists_for_has_many_query(association, where_parameters)
|
26
|
+
else
|
27
|
+
raise ArgumentError.new("where_exists: not supported association – #{association.macros.inspect}")
|
28
|
+
end
|
29
|
+
|
30
|
+
if does_exist
|
31
|
+
not_string = ""
|
32
|
+
else
|
33
|
+
not_string = "NOT "
|
34
|
+
end
|
35
|
+
|
36
|
+
#queries.map!{|query| query.select(ActiveRecord::FinderMethods::ONE_AS_ONE).where(where_parameters)}
|
37
|
+
|
38
|
+
queries_sql = queries.map{|query| "EXISTS (" + query.to_sql + ")"}.join(" OR ")
|
39
|
+
|
40
|
+
self.where("#{not_string}(#{queries_sql})")
|
41
|
+
end
|
42
|
+
|
43
|
+
def where_exists_for_belongs_to_query(association, where_parameters)
|
44
|
+
polymorphic = association.options[:polymorphic].present?
|
45
|
+
|
46
|
+
if polymorphic
|
47
|
+
associated_models = self.select("DISTINCT #{connection.quote_column_name(association.foreign_type)}").pluck(association.foreign_type).map(&:constantize)
|
48
|
+
else
|
49
|
+
associated_models = [association.klass]
|
50
|
+
end
|
51
|
+
|
52
|
+
queries = []
|
53
|
+
|
54
|
+
self_ids = quote_table_and_column_name(self.table_name, association.foreign_key)
|
55
|
+
self_type = quote_table_and_column_name(self.table_name, association.foreign_type)
|
56
|
+
|
57
|
+
associated_models.each do |associated_model|
|
58
|
+
other_ids = quote_table_and_column_name(associated_model.table_name, associated_model.primary_key)
|
59
|
+
query = associated_model.select("1").where("#{self_ids} = #{other_ids}").where(where_parameters)
|
60
|
+
if polymorphic
|
61
|
+
other_type = connection.quote(associated_model.name)
|
62
|
+
query = query.where("#{self_type} = #{other_type}")
|
63
|
+
end
|
64
|
+
queries.push query
|
65
|
+
end
|
66
|
+
|
67
|
+
queries
|
68
|
+
end
|
69
|
+
|
70
|
+
def where_exists_for_has_many_query(association, where_parameters)
|
71
|
+
through = association.options[:through].present?
|
72
|
+
|
73
|
+
if through
|
74
|
+
original_association_name = association.name
|
75
|
+
association = association.through_reflection
|
76
|
+
end
|
77
|
+
|
78
|
+
associated_model = association.klass
|
79
|
+
|
80
|
+
self_ids = quote_table_and_column_name(self.table_name, self.primary_key)
|
81
|
+
associated_ids = quote_table_and_column_name(associated_model.table_name, association.foreign_key)
|
82
|
+
|
83
|
+
result = associated_model.select("1").where("#{associated_ids} = #{self_ids}")
|
84
|
+
|
85
|
+
if association.options[:as]
|
86
|
+
other_types = quote_table_and_column_name(associated_model.table_name, association.type)
|
87
|
+
self_class = connection.quote(self.name)
|
88
|
+
result = result.where("#{other_types} = #{self_class}")
|
89
|
+
end
|
90
|
+
|
91
|
+
if through
|
92
|
+
unless associated_model.reflect_on_association(original_association_name)
|
93
|
+
original_association_name = original_association_name.to_s.singularize.to_sym
|
94
|
+
end
|
95
|
+
result = result.where_exists(original_association_name, where_parameters)
|
96
|
+
else
|
97
|
+
result = result.where(where_parameters)
|
98
|
+
end
|
99
|
+
|
100
|
+
[result]
|
101
|
+
end
|
102
|
+
|
103
|
+
def quote_table_and_column_name(table_name, column_name)
|
104
|
+
connection.quote_table_name(table_name) + '.' + connection.quote_column_name(column_name)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class ActiveRecord::Base
|
109
|
+
extend WhereExists
|
110
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Migration.create_table :first_polymorphic_entities, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :second_polymorphic_entities, :force => true do |t|
|
8
|
+
t.string :name
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Migration.create_table :polymorphic_children, :force => true do |t|
|
12
|
+
t.integer :polymorphic_entity_id
|
13
|
+
t.string :polymorphic_entity_type
|
14
|
+
t.string :name
|
15
|
+
end
|
16
|
+
|
17
|
+
class PolymorphicChild < ActiveRecord::Base
|
18
|
+
belongs_to :polymorphic_entity, polymorphic: true
|
19
|
+
end
|
20
|
+
|
21
|
+
class FirstPolymorphicEntity < ActiveRecord::Base
|
22
|
+
has_many :children, as: :polymorphic_entity, class_name: PolymorphicChild
|
23
|
+
end
|
24
|
+
|
25
|
+
class SecondPolymorphicEntity < ActiveRecord::Base
|
26
|
+
has_many :children, as: :polymorphic_entity, class_name: PolymorphicChild
|
27
|
+
end
|
28
|
+
|
29
|
+
class BelongsToPolymorphicTest < Minitest::Unit::TestCase
|
30
|
+
def setup
|
31
|
+
ActiveRecord::Base.descendants.each(&:delete_all)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_exists_only_one_kind
|
35
|
+
first_entity = FirstPolymorphicEntity.create!
|
36
|
+
second_entity = SecondPolymorphicEntity.create!
|
37
|
+
second_entity.update_column(:id, first_entity.id + 1)
|
38
|
+
|
39
|
+
first_child = PolymorphicChild.create!(polymorphic_entity: first_entity)
|
40
|
+
second_child = PolymorphicChild.create!(polymorphic_entity: second_entity)
|
41
|
+
really_orphaned_child = PolymorphicChild.create!(polymorphic_entity_type: 'FirstPolymorphicEntity', polymorphic_entity_id: second_entity.id)
|
42
|
+
another_really_orphaned_child = PolymorphicChild.create!(polymorphic_entity_type: 'SecondPolymorphicEntity', polymorphic_entity_id: first_entity.id)
|
43
|
+
|
44
|
+
result = PolymorphicChild.where_exists(:polymorphic_entity)
|
45
|
+
|
46
|
+
assert_equal 2, result.length
|
47
|
+
assert_equal [first_child, second_child].map(&:id).sort, result.map(&:id).sort
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_neither_exists
|
51
|
+
first_entity = FirstPolymorphicEntity.create!
|
52
|
+
second_entity = SecondPolymorphicEntity.create!
|
53
|
+
second_entity.update_column(:id, first_entity.id + 1)
|
54
|
+
|
55
|
+
first_child = PolymorphicChild.create!(polymorphic_entity: first_entity)
|
56
|
+
orphaned_child = PolymorphicChild.create!(polymorphic_entity_id: second_entity.id, polymorphic_entity_type: 'FirstPolymorphicEntity')
|
57
|
+
|
58
|
+
result = PolymorphicChild.where_not_exists(:polymorphic_entity)
|
59
|
+
|
60
|
+
assert_equal 1, result.length
|
61
|
+
assert_equal orphaned_child.id, result.first.id
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Migration.create_table :simple_entities, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :simple_entity_children, :force => true do |t|
|
8
|
+
t.integer :simple_entity_id
|
9
|
+
t.string :name
|
10
|
+
end
|
11
|
+
|
12
|
+
class SimpleEntity < ActiveRecord::Base
|
13
|
+
has_many :simple_entity_children
|
14
|
+
end
|
15
|
+
|
16
|
+
class SimpleEntityChild < ActiveRecord::Base
|
17
|
+
belongs_to :simple_entity
|
18
|
+
end
|
19
|
+
|
20
|
+
class BelongsToTest < Minitest::Unit::TestCase
|
21
|
+
def setup
|
22
|
+
ActiveRecord::Base.descendants.each(&:delete_all)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_nil_foreign_key
|
26
|
+
entity = SimpleEntity.create!
|
27
|
+
|
28
|
+
child = SimpleEntityChild.create!(simple_entity_id: entity.id)
|
29
|
+
orphaned_child = SimpleEntityChild.create!(simple_entity_id: nil)
|
30
|
+
|
31
|
+
result = SimpleEntityChild.where_exists(:simple_entity)
|
32
|
+
|
33
|
+
assert_equal 1, result.length
|
34
|
+
assert_equal result.first.id, child.id
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_not_existing_foreign_object
|
38
|
+
entity = SimpleEntity.create!
|
39
|
+
|
40
|
+
child = SimpleEntityChild.create!(simple_entity_id: entity.id)
|
41
|
+
orphaned_child = SimpleEntityChild.create!(simple_entity_id: entity.id + 1)
|
42
|
+
|
43
|
+
result = SimpleEntityChild.where_exists(:simple_entity)
|
44
|
+
|
45
|
+
assert_equal 1, result.length
|
46
|
+
assert_equal result.first.id, child.id
|
47
|
+
end
|
48
|
+
end
|
data/test/db/test.db
ADDED
Binary file
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Migration.create_table :relevant_polymorphic_entities, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :irrelevant_polymorphic_entities, :force => true do |t|
|
8
|
+
t.string :name
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Migration.create_table :polymorphic_children, :force => true do |t|
|
12
|
+
t.integer :polymorphic_entity_id
|
13
|
+
t.string :polymorphic_entity_type
|
14
|
+
t.string :name
|
15
|
+
end
|
16
|
+
|
17
|
+
class PolymorphicChild < ActiveRecord::Base
|
18
|
+
belongs_to :polymorphic_entity, polymorphic: true
|
19
|
+
end
|
20
|
+
|
21
|
+
class RelevantPolymorphicEntity < ActiveRecord::Base
|
22
|
+
has_many :children, as: :polymorphic_entity, class_name: PolymorphicChild
|
23
|
+
end
|
24
|
+
|
25
|
+
class IrrelevantPolymorphicEntity < ActiveRecord::Base
|
26
|
+
has_many :children, as: :polymorphic_entity, class_name: PolymorphicChild
|
27
|
+
end
|
28
|
+
|
29
|
+
class HasManyPolymorphicTest < Minitest::Unit::TestCase
|
30
|
+
def setup
|
31
|
+
ActiveRecord::Base.descendants.each(&:delete_all)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_polymorphic
|
35
|
+
child = PolymorphicChild.create!
|
36
|
+
|
37
|
+
irrelevant_entity = IrrelevantPolymorphicEntity.create!(children: [child])
|
38
|
+
relevant_entity = RelevantPolymorphicEntity.create!(id: irrelevant_entity.id)
|
39
|
+
|
40
|
+
result = RelevantPolymorphicEntity.where_exists(:children)
|
41
|
+
|
42
|
+
assert_equal 0, result.length
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Migration.create_table :simple_entities, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :simple_entity_children, :force => true do |t|
|
8
|
+
t.integer :simple_entity_id
|
9
|
+
t.string :name
|
10
|
+
end
|
11
|
+
|
12
|
+
class SimpleEntity < ActiveRecord::Base
|
13
|
+
has_many :simple_entity_children
|
14
|
+
end
|
15
|
+
|
16
|
+
class SimpleEntityChild < ActiveRecord::Base
|
17
|
+
belongs_to :simple_entity
|
18
|
+
end
|
19
|
+
|
20
|
+
class HasManyTest < Minitest::Unit::TestCase
|
21
|
+
def setup
|
22
|
+
ActiveRecord::Base.descendants.each(&:delete_all)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_without_parameters
|
26
|
+
child = SimpleEntityChild.create!
|
27
|
+
|
28
|
+
blank_entity = SimpleEntity.create!
|
29
|
+
filled_entity = SimpleEntity.create!(simple_entity_children: [child])
|
30
|
+
|
31
|
+
result = SimpleEntity.where_exists(:simple_entity_children)
|
32
|
+
|
33
|
+
assert_equal 1, result.length
|
34
|
+
assert_equal result.first.id, filled_entity.id
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_with_parameters
|
38
|
+
wrong_child = SimpleEntityChild.create!(name: 'wrong')
|
39
|
+
child = SimpleEntityChild.create!(name: 'right')
|
40
|
+
|
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])
|
44
|
+
|
45
|
+
result = SimpleEntity.where_exists(:simple_entity_children, name: 'right')
|
46
|
+
|
47
|
+
assert_equal 1, result.length
|
48
|
+
assert_equal result.first.id, entity.id
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_with_scope
|
52
|
+
child = SimpleEntityChild.create!
|
53
|
+
entity = SimpleEntity.create!(simple_entity_children: [child])
|
54
|
+
|
55
|
+
result = SimpleEntity.unscoped.where_exists(:simple_entity_children)
|
56
|
+
|
57
|
+
assert_equal 1, result.length
|
58
|
+
assert_equal result.first.id, entity.id
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_not_exists
|
62
|
+
child = SimpleEntityChild.create!
|
63
|
+
|
64
|
+
blank_entity = SimpleEntity.create!
|
65
|
+
filled_entity = SimpleEntity.create!(simple_entity_children: [child])
|
66
|
+
|
67
|
+
result = SimpleEntity.where_not_exists(:simple_entity_children)
|
68
|
+
|
69
|
+
assert_equal 1, result.length
|
70
|
+
assert_equal result.first.id, blank_entity.id
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Migration.create_table :projects, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
ActiveRecord::Migration.create_table :tasks, :force => true do |t|
|
8
|
+
t.string :name
|
9
|
+
t.integer :project_id
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveRecord::Migration.create_table :line_items, :force => true do |t|
|
13
|
+
t.string :name
|
14
|
+
t.integer :invoice_id
|
15
|
+
t.integer :task_id
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Migration.create_table :invoices, :force => true do |t|
|
19
|
+
t.string :name
|
20
|
+
end
|
21
|
+
|
22
|
+
class Project < ActiveRecord::Base
|
23
|
+
has_many :tasks
|
24
|
+
has_many :invoices, :through => :tasks
|
25
|
+
has_many :line_items, :through => :tasks
|
26
|
+
end
|
27
|
+
|
28
|
+
class Task < ActiveRecord::Base
|
29
|
+
belongs_to :project
|
30
|
+
|
31
|
+
has_many :invoices, :through => :line_items
|
32
|
+
has_many :line_items
|
33
|
+
end
|
34
|
+
|
35
|
+
class LineItem < ActiveRecord::Base
|
36
|
+
belongs_to :invoice
|
37
|
+
belongs_to :task
|
38
|
+
end
|
39
|
+
|
40
|
+
class Invoice < ActiveRecord::Base
|
41
|
+
has_many :tasks, :through => :line_item
|
42
|
+
has_many :line_items
|
43
|
+
end
|
44
|
+
|
45
|
+
# Invoices -> LineItems <- Tasks <- Project
|
46
|
+
|
47
|
+
class HasManyThroughTest < Minitest::Unit::TestCase
|
48
|
+
def setup
|
49
|
+
ActiveRecord::Base.descendants.each(&:delete_all)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_one_level_through
|
53
|
+
project = Project.create!
|
54
|
+
irrelevant_project = Project.create!
|
55
|
+
|
56
|
+
task = Task.create!(project: project)
|
57
|
+
irrelevant_task = Task.create!(project: irrelevant_project)
|
58
|
+
|
59
|
+
line_item = LineItem.create!(name: 'relevant', task: task)
|
60
|
+
irrelevant_line_item = LineItem.create!(name: 'irrelevant', task: irrelevant_task)
|
61
|
+
|
62
|
+
result = Project.where_exists(:line_items, name: 'relevant')
|
63
|
+
|
64
|
+
assert_equal 1, result.length
|
65
|
+
assert_equal project.id, result.first.id
|
66
|
+
|
67
|
+
result = Project.where_not_exists(:line_items, name: 'relevant')
|
68
|
+
assert_equal 1, result.length
|
69
|
+
assert_equal irrelevant_project.id, result.first.id
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_deep_through
|
73
|
+
project = Project.create!
|
74
|
+
irrelevant_project = Project.create!
|
75
|
+
|
76
|
+
task = Task.create!(project: project)
|
77
|
+
irrelevant_task = Task.create!(project: irrelevant_project)
|
78
|
+
|
79
|
+
invoice = Invoice.create!(name: 'relevant')
|
80
|
+
irrelevant_invoice = Invoice.create!(name: 'irrelevant')
|
81
|
+
|
82
|
+
line_item = LineItem.create!(task: task, invoice: invoice)
|
83
|
+
irrelevant_line_item = LineItem.create!(task: irrelevant_task, invoice: irrelevant_invoice)
|
84
|
+
|
85
|
+
result = Project.where_exists(:invoices, name: 'relevant')
|
86
|
+
|
87
|
+
assert_equal 1, result.length
|
88
|
+
assert_equal project.id, result.first.id
|
89
|
+
|
90
|
+
result = Project.where_not_exists(:invoices, name: 'relevant')
|
91
|
+
|
92
|
+
assert_equal 1, result.length
|
93
|
+
assert_equal irrelevant_project.id, result.first.id
|
94
|
+
end
|
95
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/pride'
|
3
|
+
require 'bundler/setup'
|
4
|
+
Bundler.require(:default)
|
5
|
+
require 'active_record'
|
6
|
+
require File.dirname(__FILE__) + '/../lib/where_exists'
|
7
|
+
|
8
|
+
ActiveRecord::Base.default_timezone = :utc
|
9
|
+
ActiveRecord::Base.time_zone_aware_attributes = true
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(
|
12
|
+
:adapter => 'sqlite3',
|
13
|
+
:database => File.dirname(__FILE__) + "/db/test.db"
|
14
|
+
)
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: where_exists
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugene Zolotarev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.22
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.22
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sqlite3
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.3'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.3'
|
47
|
+
description: Rails way to harness the power of SQL "EXISTS" statement
|
48
|
+
email:
|
49
|
+
- eugzol@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- MIT-LICENSE
|
55
|
+
- Rakefile
|
56
|
+
- lib/where_exists.rb
|
57
|
+
- lib/where_exists/version.rb
|
58
|
+
- test/belongs_to_polymorphic_test.rb
|
59
|
+
- test/belongs_to_test.rb
|
60
|
+
- test/db/test.db
|
61
|
+
- test/has_many_polymorphic_test.rb
|
62
|
+
- test/has_many_test.rb
|
63
|
+
- test/has_many_through_test.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
homepage: http://github.com/eugzol/where_exists
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.4.4
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: "#where_exists extension of ActiveRecord"
|
89
|
+
test_files:
|
90
|
+
- test/belongs_to_polymorphic_test.rb
|
91
|
+
- test/belongs_to_test.rb
|
92
|
+
- test/db/test.db
|
93
|
+
- test/has_many_polymorphic_test.rb
|
94
|
+
- test/has_many_test.rb
|
95
|
+
- test/has_many_through_test.rb
|
96
|
+
- test/test_helper.rb
|