activerecord-validate_unique_child_attribute 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/lib/active_record/validate_unique_child_attribute.rb +42 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df0c1a8eb4ba5574019d8ba763747edc4de25aa6
|
4
|
+
data.tar.gz: 9d0bd3fdc9e4c995d1ccd2cd84e3b39e58ac65be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ff3c0af7ed02a52538cceba2be0ab349bcad7eb8ad5db5d9ac3a0d5f76b37a2eecc5c1def8a3ee7322d954df9ed430cb9d19cd0fa66d05819bab7eafe0eaccb
|
7
|
+
data.tar.gz: 0fc7975b50ac6f3c4f6157b835b47f6c9a571aab401ad710959c3b40bced832a0089fd30c89dbbfe759e1dd54a00f355aab3857702b0a3650aa13a518cbd94da
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
3
|
+
# Work around https://github.com/rails/rails/issues/4568 :-(
|
4
|
+
module ActiveRecord
|
5
|
+
module ValidateUniqueChildAttribute
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def validates_uniqueness_of_child_attribute(relation, attribute, options = {}, &block)
|
10
|
+
options.reverse_merge!(validate: false,
|
11
|
+
error_formatter: :default_duplicate_child_attribute_error_formatter)
|
12
|
+
validate do |record|
|
13
|
+
record.validate_uniqueness_of_child_attribute(relation, attribute, options, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate_uniqueness_of_child_attribute(relation, attribute, options, &block)
|
19
|
+
records = send(relation)
|
20
|
+
records.each(&:valid?) if options[:validate]
|
21
|
+
dups = detect_duplicate_child_attributes(records, attribute, options)
|
22
|
+
dups.empty? || set_duplicate_child_attribute_errors(relation, attribute, dups, options, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def detect_duplicate_child_attributes(records, attribute, _options)
|
28
|
+
values = records.map(&attribute)
|
29
|
+
values.select { |v| values.count(v) > 1 }.uniq
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_duplicate_child_attribute_errors(relation, attribute, dups, options, &block)
|
33
|
+
proc = block_given? ? block : method(options[:error_formatter])
|
34
|
+
errors.add(relation, proc.call(attribute, dups))
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_duplicate_child_attribute_error_formatter(attribute, dups)
|
38
|
+
s = attribute.to_s.singularize.humanize.downcase
|
39
|
+
"The following #{dups.count == 1 ? "#{s} has" : "#{s.pluralize} have"} been duplicated: #{dups.map { |d| "'#{d}'" }.join(', ')}. Each #{s} should be added only once."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-validate_unique_child_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Marden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: appraisal
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 10.4.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 10.4.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: |-
|
112
|
+
Guarantee uniqueness of a single attribute across one or more children of an ActiveRecord object
|
113
|
+
|
114
|
+
Works around https://github.com/rails/rails/issues/4568
|
115
|
+
email: nick@rrsoft.co
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- lib/active_record/validate_unique_child_attribute.rb
|
121
|
+
homepage: http://github.com/RapidRiverSoftware/activerecord-validate_unique_child_attribute
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.4.3
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Guarantee uniqueness of a single attribute across one or more children of
|
145
|
+
an ActiveRecord object
|
146
|
+
test_files: []
|