without 0.1.0.beta
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.
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/without.rb +65 -0
- data/lib/without/version.rb +3 -0
- data/without.gemspec +26 -0
- metadata +124 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Bob Lail
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Without
|
2
|
+
|
3
|
+
An extension for ActiveRecord for finding broken associations
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'without'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install without
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
##### Find broken belongs_to associations
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Bookmark < ActiveRecord::Base
|
27
|
+
belongs_to :user
|
28
|
+
end
|
29
|
+
|
30
|
+
# all bookmarks with a user_id that doesn't belong to any user in the database
|
31
|
+
Bookmark.without_a(:user)
|
32
|
+
```
|
33
|
+
|
34
|
+
##### Find broken polymorphic associations
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class Photo < ActiveRecord::Base
|
38
|
+
belongs_to :subject, polymorphic: true
|
39
|
+
end
|
40
|
+
|
41
|
+
# all photos of castles with a subject_id that doesn't belong to any castle in the database
|
42
|
+
Photo.without_a(:subject, subject_type: "Castle")
|
43
|
+
```
|
44
|
+
|
45
|
+
##### Find has_many associations with none
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Post < ActiveRecord::Base
|
49
|
+
has_many :comments
|
50
|
+
end
|
51
|
+
|
52
|
+
# all posts with no comments
|
53
|
+
Post.without_any(:comments)
|
54
|
+
```
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/without.
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "without"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/without.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "without/version"
|
2
|
+
require "activerecord"
|
3
|
+
|
4
|
+
module Without
|
5
|
+
def without_a(belongs_to, options={})
|
6
|
+
options = options.with_indifferent_access
|
7
|
+
scope = all
|
8
|
+
association = scope.model.reflect_on_association(belongs_to)
|
9
|
+
|
10
|
+
unless association.macro == :belongs_to
|
11
|
+
raise ArgumentError, "`without_a` only works on belongs_to associations"
|
12
|
+
end
|
13
|
+
|
14
|
+
if association.polymorphic?
|
15
|
+
foreign_type = options.fetch(association.foreign_type) do
|
16
|
+
raise ArgumentError, "`#{belongs_to}` is a polymorphic association; please " <<
|
17
|
+
"pass :#{association.foreign_type} to `without_a`"
|
18
|
+
end
|
19
|
+
|
20
|
+
join_table = foreign_type.constantize.table_name
|
21
|
+
scope = scope.where(scope.arel_table[association.foreign_type].eq(foreign_type))
|
22
|
+
else
|
23
|
+
join_table = association.table_name
|
24
|
+
end
|
25
|
+
|
26
|
+
# This technique with the JOINs is a lot more work than simply saying
|
27
|
+
#
|
28
|
+
# where("#{association.foreign_key} NOT IN (SELECT id FROM #{association.table_name})")
|
29
|
+
#
|
30
|
+
# but `NOT IN` is very slow: it requires a sequence scan. On very large
|
31
|
+
# tables this thousands (or millions!) of times faster.
|
32
|
+
query = scope.joins("LEFT OUTER JOIN #{join_table} ON #{table_name}.#{association.foreign_key}=#{join_table}.id").where("#{join_table}.id" => nil)
|
33
|
+
|
34
|
+
# Postgres does not allow a DELETE statement to also use JOINs.
|
35
|
+
# The common technique (that Rails uses) is to
|
36
|
+
#
|
37
|
+
# DELETE FROM the_table WHERE primary_key in (the_query)
|
38
|
+
#
|
39
|
+
# This technique doesn't work with pure join tables which have
|
40
|
+
# no primary key.
|
41
|
+
#
|
42
|
+
# So instead we manually construct a query that moves the JOIN
|
43
|
+
# into a subquery and connects the two with a key that will work
|
44
|
+
# (the broken foreign_key).
|
45
|
+
unless scope.model.primary_key
|
46
|
+
query = scope.where(association.foreign_key => query.select(association.foreign_key))
|
47
|
+
end
|
48
|
+
|
49
|
+
query
|
50
|
+
end
|
51
|
+
alias :without_an :without_a
|
52
|
+
|
53
|
+
def without_any(has_many)
|
54
|
+
scope = all
|
55
|
+
association = scope.model.reflect_on_association(has_many)
|
56
|
+
|
57
|
+
unless association.macro == :has_many
|
58
|
+
raise ArgumentError, "`without_any` only works on has_many associations"
|
59
|
+
end
|
60
|
+
|
61
|
+
scope.joins("LEFT OUTER JOIN #{association.table_name} ON #{table_name}.id=#{association.table_name}.#{association.foreign_key}").where("#{association.table_name}.#{association.foreign_key}" => nil)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
ActiveRecord::Base.extend Without
|
data/without.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'without/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "without"
|
8
|
+
spec.version = Without::VERSION
|
9
|
+
spec.authors = ["Bob Lail"]
|
10
|
+
spec.email = ["bob.lailfamily@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{An extension for ActiveRecord for finding broken associations}
|
13
|
+
spec.homepage = "https://github.com/cph/without"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: without
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.beta
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bob Lail
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-04-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '4.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.11'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.11'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '5.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '5.0'
|
78
|
+
description:
|
79
|
+
email:
|
80
|
+
- bob.lailfamily@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- bin/console
|
92
|
+
- bin/setup
|
93
|
+
- lib/without.rb
|
94
|
+
- lib/without/version.rb
|
95
|
+
- without.gemspec
|
96
|
+
homepage: https://github.com/cph/without
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: -447154360329327183
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>'
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.3.1
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.8.23.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: An extension for ActiveRecord for finding broken associations
|
124
|
+
test_files: []
|