ancestry_uniqueness 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a3f1f849df54eb960d1d4da01069b45f241f4ce
4
+ data.tar.gz: 0e4894046ab5b51c882d7c8cb1efcb6c3a632632
5
+ SHA512:
6
+ metadata.gz: 97205f2e2cdbc80699af274a3f550b71d0c5deb96c28b950c65b1349ba23e9bd71690077fa898c1db4644c46cf8db587f75bbf065c838a24f61ce6224d559455
7
+ data.tar.gz: c7119403f2b09d188b86e4822d977d0b1e96d97e6612bc8464b6c0bf638f6a04027bcff93a6faa2447832cbfce7b8b58549d467d9db08dbcdbafa9b0c749b7af
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
19
+ .ruby-version
20
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ancestry_uniqueness.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 flauwekeul
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Ancestry uniqueness
2
+
3
+ Provides an activerecord uniqueness validator for objects that are ordered in a tree using the [ancestry](https://github.com/stefankroes/ancestry) gem.
4
+
5
+ The gem is just a custom validator as described in the [Rails guides](http://edgeguides.rubyonrails.org/active_record_validations.html#custom-validators).
6
+
7
+ ## Usage
8
+ After adding the gem to your project simply add `ancestry_uniqueness: true` as a parameter to the `validates` class method. E.g.:
9
+
10
+ ```ruby
11
+ class Page < ActiveRecord::Base
12
+ has_ancestry
13
+
14
+ validates :slug, ancestry_uniqueness: true
15
+ end
16
+ ```
17
+
18
+ This makes sure the object doesn't pass validation when the attribute (`slug` in this case) isn't unique among the object's siblings (i.e. within the same parent).
19
+
20
+ ### With scope
21
+
22
+ If you want to scope your attribute pass a hash to `ancestry_uniqueness` like so:
23
+
24
+ ```ruby
25
+ class Page < ActiveRecord::Base
26
+ has_ancestry
27
+
28
+ validates :slug, ancestry_uniqueness: {scope: :some_attribute}
29
+ end
30
+ ```
31
+
32
+ ## Compatibility
33
+ The gem depends on [ancestry](https://github.com/stefankroes/ancestry) 2 or greater. It should work in Rails 3 and Rails 4 apps.
34
+
35
+ Currently it's only tested in a Rails 4.0.1 app.
36
+
37
+ ## Credits
38
+ I really should mention [groe](https://github.com/groe) as I based this gem heavily on his [code snippet](https://github.com/stefankroes/ancestry/issues/72#issuecomment-18242566).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ancestry_uniqueness/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ancestry_uniqueness"
8
+ spec.version = Ancestry::Uniqueness::VERSION
9
+ spec.authors = ["Abbe Keultjes"]
10
+ spec.email = ["abbe.keultjes@gmail.com"]
11
+ spec.description = %q{Uniqueness validator when using the ancestry gem.}
12
+ spec.summary = %q{Provides an activerecord uniqueness validator for objects that are ordered in a tree using the ancestry gem.}
13
+ spec.homepage = "https://github.com/flauwekeul/ancestry_uniqueness"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 2.0"
24
+ spec.add_development_dependency "sqlite3", "~> 1.3"
25
+
26
+ spec.add_dependency "ancestry", "~> 2.0"
27
+ end
@@ -0,0 +1,5 @@
1
+ module Ancestry
2
+ module Uniqueness
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require "ancestry_uniqueness/version"
2
+
3
+ class AncestryUniquenessValidator < ActiveRecord::Validations::UniquenessValidator
4
+
5
+ def validate_each(record, attribute, value)
6
+ exclude_self = record.try(:id) ? ['id <> ?', record.id] : []
7
+ scope_opts = [*options[:scope]]
8
+
9
+ scope = scope_opts.inject({}) do |s, attr|
10
+ s.merge({attr.to_sym => record.send(attr)})
11
+ end
12
+
13
+ if record.siblings.where(exclude_self).where(scope.merge({attribute.to_sym => record.send(attribute)})).any?
14
+ record.errors.add(attribute, options[:message] || :taken)
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,116 @@
1
+ require "spec_helper"
2
+
3
+ describe AncestryUniquenessValidator do
4
+
5
+ # set default value if undefined
6
+ let(:scope_attr) { defined?(super) ? super() : nil }
7
+
8
+ shared_examples_for "a valid validatable" do
9
+ it "is valid" do
10
+ subject.provider = provider
11
+ subject.scope_attr = scope_attr
12
+ expect(subject).to be_valid
13
+ end
14
+ end
15
+
16
+ shared_examples_for "an invalid validatable" do
17
+ it "is invalid" do
18
+ subject.provider = provider
19
+ subject.scope_attr = scope_attr
20
+ expect(subject).not_to be_valid
21
+ end
22
+ end
23
+
24
+ # If this ever needs to be DRYed up: https://www.envygeeks.com/blog/mocking-activerecord-to-test-concerns
25
+ before :all do
26
+ migration = ActiveRecord::Migration.new
27
+ migration.verbose = false
28
+
29
+ migration.create_table :validatables do |t|
30
+ t.string :ancestry
31
+ t.string :provider
32
+ t.string :scope_attr
33
+ end
34
+ end
35
+
36
+ after :all do
37
+ migration = ActiveRecord::Migration.new
38
+ migration.verbose = false
39
+
40
+ migration.drop_table :validatables
41
+ end
42
+
43
+ context "without scope" do
44
+ class Validatable < ActiveRecord::Base
45
+ has_ancestry
46
+ validates_with AncestryUniquenessValidator, attributes: :provider
47
+ end
48
+
49
+ let(:a_record) { Validatable.create!(provider: 'provider', parent: nil) }
50
+
51
+ context "when record has no siblings" do
52
+ subject { Validatable.new(parent: a_record) }
53
+ let(:provider) { a_record.provider }
54
+ it_behaves_like "a valid validatable"
55
+ end
56
+
57
+ context "when record has a sibling" do
58
+ subject { Validatable.new(parent: a_record.parent) }
59
+
60
+ context "with unique provider among siblings" do
61
+ let(:provider) { 'different provider' }
62
+ it_behaves_like "a valid validatable"
63
+ end
64
+
65
+ context "with existing provider among siblings" do
66
+ let(:provider) { a_record.provider }
67
+ it_behaves_like "an invalid validatable"
68
+ end
69
+ end
70
+ end
71
+
72
+ context "with scope" do
73
+ class Validatable < ActiveRecord::Base
74
+ has_ancestry
75
+ validates_with AncestryUniquenessValidator, attributes: :provider, scope: :scope_attr
76
+ end
77
+
78
+ let(:a_record) { Validatable.create!(provider: 'provider', parent: nil, scope_attr: 'some scope') }
79
+
80
+ context "when record has no siblings" do
81
+ subject { Validatable.new(parent: a_record, scope_attr: a_record.scope_attr) }
82
+ let(:provider) { a_record.provider }
83
+ let(:scope_attr) { a_record.scope_attr }
84
+ it_behaves_like "a valid validatable"
85
+ end
86
+
87
+ context "when record has a sibling" do
88
+ subject { Validatable.new(parent: a_record.parent) }
89
+
90
+ context "with unique provider among siblings and scope" do
91
+ let(:provider) { 'different provider' }
92
+ let(:scope_attr) { 'different scope' }
93
+ it_behaves_like "a valid validatable"
94
+ end
95
+
96
+ context "with existing provider among siblings and unique provider among scope" do
97
+ let(:provider) { a_record.provider }
98
+ let(:scope_attr) { 'different scope' }
99
+ it_behaves_like "an invalid validatable"
100
+ end
101
+
102
+ context "with unique provider among siblings and existing provider among scope" do
103
+ let(:provider) { 'different provider' }
104
+ let(:scope_attr) { a_record.scope_attr }
105
+ it_behaves_like "a valid validatable"
106
+ end
107
+
108
+ context "with existing provider among siblings and scope" do
109
+ let(:provider) { a_record.provider }
110
+ let(:scope_attr) { a_record.scope_attr }
111
+ it_behaves_like "an invalid validatable"
112
+ end
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+ require 'ancestry'
3
+ require 'ancestry_uniqueness'
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ adapter: 'sqlite3',
7
+ database: ':memory:')
8
+
9
+ RSpec.configure do |config|
10
+ config.order = 'random'
11
+
12
+ config.around do |example|
13
+ ActiveRecord::Base.transaction do
14
+ example.run
15
+ raise ActiveRecord::Rollback
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ancestry_uniqueness
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Abbe Keultjes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ancestry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: Uniqueness validator when using the ancestry gem.
84
+ email:
85
+ - abbe.keultjes@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - ancestry_uniqueness.gemspec
96
+ - lib/ancestry_uniqueness.rb
97
+ - lib/ancestry_uniqueness/version.rb
98
+ - spec/lib/ancestry_uniqueness_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/flauwekeul/ancestry_uniqueness
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Provides an activerecord uniqueness validator for objects that are ordered
124
+ in a tree using the ancestry gem.
125
+ test_files:
126
+ - spec/lib/ancestry_uniqueness_spec.rb
127
+ - spec/spec_helper.rb