find_duplicates 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/find_duplicates.gemspec +29 -0
- data/lib/find_duplicates/version.rb +3 -0
- data/lib/find_duplicates.rb +28 -0
- data/spec/find_duplicates_spec.rb +49 -0
- data/spec/spec_helper.rb +34 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6418b13e2f8b0ef1e2560ecfd8a539c5f949a92a
|
4
|
+
data.tar.gz: 86207fd75fbc54eb803627a6ce7884e32fb83156
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 99d2919631fed769b0d0964212c6ad610b0d09745e0eb787c74745cb80972d47bfc47fed62636bba2473f92b1b969d8ee52e9a715b7e4e9d118a51465400911d
|
7
|
+
data.tar.gz: 65309b95e0acb4da772c7aa992b702c3f1485ab157d1c3cea56f97f6acfa4f788f0fe5471f83eb1d8fd43a0a3bf20e569aa8b6e84ca67ce21a40445220699edf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 adam klein
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# FindDuplicates
|
2
|
+
|
3
|
+
This is home to the find_duplicates GEM.
|
4
|
+
|
5
|
+
Use it to add methods to ActiveRecord models, that allow to find rows with duplicate values in certain columns. For example, find all users with the same first name, or find all items with the same price as a specific item.
|
6
|
+
|
7
|
+
Just add 'find_duplicates' to your Gemfile, and you automatically enhance your AR models functionality.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'find_duplicates'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install find_duplicates
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Given an AR model, for example User, with 'first_name' and 'last_name' fields:
|
26
|
+
```ruby
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
attr_accessible :first_name, :last_name
|
29
|
+
end
|
30
|
+
|
31
|
+
> @user1 = User.create first_name: 'Adam', last_name: 'Klein'
|
32
|
+
> @user2 = User.create first_name: 'Calvin', last_name: 'Klein'
|
33
|
+
> @user3 = User.create first_name: 'Adam', last_name: 'Sandler'
|
34
|
+
```
|
35
|
+
|
36
|
+
class methods:
|
37
|
+
```ruby
|
38
|
+
> User.duplicates(:first_name) # => [@user1, @user3]
|
39
|
+
> User.duplicates(:last_name) # => [@user1, @user2]
|
40
|
+
```
|
41
|
+
|
42
|
+
instance methods:
|
43
|
+
```ruby
|
44
|
+
> @user1.duplicate?(:first_name) # => true
|
45
|
+
> @user1.duplicates(:first_name) # => [@user3]
|
46
|
+
> @user1.duplicates_with_self(:last_name) # => [@user1, @user2]
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'find_duplicates/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "find_duplicates"
|
8
|
+
spec.version = FindDuplicates::VERSION
|
9
|
+
spec.authors = ["adam klein", "500tech"]
|
10
|
+
spec.email = ["adam@500tech.com", "info@500tech.com"]
|
11
|
+
spec.summary = %q{adds methods to find duplicate rows to AR models}
|
12
|
+
spec.description = %q{adds methods to find duplicate rows to AR models}
|
13
|
+
spec.homepage = "https://github.com/500tech/find_duplicates"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'activerecord', '>= 3.0'
|
22
|
+
spec.add_runtime_dependency 'activesupport', '>= 3.0'
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency 'sqlite3'
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "find_duplicates/version"
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module FindDuplicates
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
def duplicate?(field)
|
8
|
+
duplicates_with_self(field).count > 1
|
9
|
+
end
|
10
|
+
|
11
|
+
def duplicates(field)
|
12
|
+
duplicates_with_self(field).where('id <> ?', id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def duplicates_with_self(field)
|
16
|
+
self.class.where(field => self[field])
|
17
|
+
end
|
18
|
+
module ClassMethods
|
19
|
+
def duplicates(field)
|
20
|
+
field = field.to_sym
|
21
|
+
where field => self.select(['count(*)', field]).group(field).having('count(*) > 1').map(&field)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ActiveRecord::Base
|
27
|
+
include FindDuplicates
|
28
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pry'
|
3
|
+
describe FindDuplicates do
|
4
|
+
before do
|
5
|
+
setup
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'duplicate records' do
|
9
|
+
before do
|
10
|
+
@first = TestDuplicates.create!(title: 'first!')
|
11
|
+
@second = TestDuplicates.create!(title: 'second!')
|
12
|
+
@third = TestDuplicates.create!(title: 'first!')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'class duplicates' do
|
16
|
+
expect(TestDuplicates.duplicates(:title)).not_to be_empty
|
17
|
+
end
|
18
|
+
it 'instance duplicates' do
|
19
|
+
expect(@first.duplicates(:title)).to match_array([@third])
|
20
|
+
end
|
21
|
+
it 'duplicates_with_self' do
|
22
|
+
expect(@first.duplicates_with_self(:title)).to match_array([@first, @third])
|
23
|
+
end
|
24
|
+
it 'duplicate?' do
|
25
|
+
expect(@first.duplicate?(:title)).to be_truthy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context 'no duplicate records' do
|
29
|
+
before do
|
30
|
+
@first = TestDuplicates.create!(title: 'first!')
|
31
|
+
@second = TestDuplicates.create!(title: 'second!')
|
32
|
+
@third = TestDuplicates.create!(title: 'third!')
|
33
|
+
end
|
34
|
+
it 'duplicates' do
|
35
|
+
expect(TestDuplicates.duplicates(:title)).to be_empty
|
36
|
+
end
|
37
|
+
it 'instance duplicates' do
|
38
|
+
expect(@first.duplicates(:title)).to be_empty
|
39
|
+
end
|
40
|
+
it 'duplicates_with_self' do
|
41
|
+
expect(@first.duplicates_with_self(:title)).to match_array([@first])
|
42
|
+
end
|
43
|
+
it 'duplicate?' do
|
44
|
+
expect(@first.duplicate?(:title)).to be_falsey
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'find_duplicates'
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
19
|
+
|
20
|
+
def setup
|
21
|
+
ActiveRecord::Base.establish_connection(
|
22
|
+
:adapter => 'sqlite3',
|
23
|
+
:database => ':memory:'
|
24
|
+
)
|
25
|
+
|
26
|
+
ActiveRecord::Schema.define do
|
27
|
+
create_table :test_duplicates, force: true do |t|
|
28
|
+
t.column :title, :string
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class TestDuplicates < ActiveRecord::Base
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: find_duplicates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- adam klein
|
8
|
+
- 500tech
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '3.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '3.0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '3.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.5'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.5'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rake
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: sqlite3
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: pry
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
description: adds methods to find duplicate rows to AR models
|
113
|
+
email:
|
114
|
+
- adam@500tech.com
|
115
|
+
- info@500tech.com
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- ".gitignore"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- find_duplicates.gemspec
|
126
|
+
- lib/find_duplicates.rb
|
127
|
+
- lib/find_duplicates/version.rb
|
128
|
+
- spec/find_duplicates_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
homepage: https://github.com/500tech/find_duplicates
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.0.8
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: adds methods to find duplicate rows to AR models
|
154
|
+
test_files:
|
155
|
+
- spec/find_duplicates_spec.rb
|
156
|
+
- spec/spec_helper.rb
|