can_has_validations 0.5.0 → 0.5.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c8eada0f5dd41202696bbab1b493bb017489452ed29459b3f48925236eaec01d
|
4
|
+
data.tar.gz: 357ffbc02deb80c773b0b0f604c8dc0f773d7fd62da030f7fa03299a0fe3ba20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5a8f3386d4e3d420a4492370a77479658557f0d5d97e3f04fc7fe95bb04934a9e23cc4146b6f2be60d20db25a049b8387bc46ee1f6dd6156b175b69a8f598e1
|
7
|
+
data.tar.gz: 6cfa0fab71ff63d23c0d53c9b3780107b99a0af9a126cc695022613fe6b6b5ff28f68315706cd1153114d2c3e90bfb65401d9e4c99ba6fde8578c02c9982c82c
|
@@ -2,11 +2,15 @@
|
|
2
2
|
# :allow_nil will not only allow the attribute/association to be nil, but
|
3
3
|
# also any of the :scope's.
|
4
4
|
# eg: validates :user, grandparent: {scope: :org, parent: :realm}
|
5
|
-
# validates :
|
5
|
+
# validates :user_id, grandparent: {scope: [:phone, :address], parent: :account_id}
|
6
6
|
|
7
7
|
module ActiveModel::Validations
|
8
8
|
class GrandparentValidator < ActiveModel::EachValidator
|
9
9
|
def validate_each(record, attribute, association)
|
10
|
+
# to allow attribute to be the _id and not just the actual association
|
11
|
+
if attribute.to_s.ends_with?('_id')
|
12
|
+
association = record.send(attribute.to_s.sub(/_id$/,''))
|
13
|
+
end
|
10
14
|
all_match = Array(options[:scope]).all? do |scope|
|
11
15
|
cousin = record.send(scope)
|
12
16
|
if cousin.nil?
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# better behaved version of the uniqueness validator
|
2
|
+
# Difference is it doesn't puke if the foreign_key is an invalid type (for
|
3
|
+
# example, an invalid string being provided for UUID type key). Instead,
|
4
|
+
# returns an error about being unable to find the key value.
|
5
|
+
# eg: validates :user_id, safe_uniqueness: true
|
6
|
+
|
7
|
+
class SafeUniquenessValidator < ActiveRecord::Validations::UniquenessValidator
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
record.transaction(requires_new: true) do
|
10
|
+
super
|
11
|
+
end
|
12
|
+
rescue ActiveRecord::StatementInvalid
|
13
|
+
record.errors.add(attribute, "%{attribute} %{value} not found", options.except(:case_sensitive, :scope).merge(value: value))
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/can_has_validations/validators/grandparent_validator.rb
|
62
62
|
- lib/can_has_validations/validators/hostname_validator.rb
|
63
63
|
- lib/can_has_validations/validators/ordering_validator.rb
|
64
|
+
- lib/can_has_validations/validators/safe_uniqueness_validator.rb
|
64
65
|
- lib/can_has_validations/validators/url_validator.rb
|
65
66
|
- lib/can_has_validations/validators/write_once_validator.rb
|
66
67
|
- lib/can_has_validations/version.rb
|
@@ -114,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
115
|
version: '0'
|
115
116
|
requirements: []
|
116
117
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.6
|
118
|
+
rubygems_version: 2.7.6
|
118
119
|
signing_key:
|
119
120
|
specification_version: 4
|
120
121
|
summary: Assorted Rails 4.x-5.x validators
|