email_domain_inclusion 1.1.0 → 1.2.0
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 +4 -4
- data/README.md +1 -1
- data/lib/email_domain_inclusion/version.rb +1 -1
- data/lib/email_domain_inclusion.rb +18 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2729b5136cdc717dd8f70812757a632dbf3f0438
|
4
|
+
data.tar.gz: bebb71f95e7b16d6228a26135218288031527286
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14e074510939b06348208d3575d896042832d4378cf82a3ade8ac45e58a4b422e9f4e2f1bd79a8be08425f445532b09cf89fefcf3bed784dd572a0e0207f4814
|
7
|
+
data.tar.gz: 12451415cce64c359ceba4f9e09121b2eaf0b9e50da9ad4daeb2279f5903a473ff1d7f6336e02a1d13eab5971f2808fc860f28c547935cc7b860956c2bffb3ce
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ class Person
|
|
25
25
|
include ActiveModel::Validations
|
26
26
|
attr_accessor :name, :email
|
27
27
|
|
28
|
-
validates :email, email_domain_inclusion: {allowed_domains: ["hotmail.com", "gmail.com"]}
|
28
|
+
validates :email, email_domain_inclusion: {allow_subdomains: true, allowed_domains: ["hotmail.com", "gmail.com"]}
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
@@ -5,15 +5,29 @@ require "email_domain_inclusion/version"
|
|
5
5
|
# +allowed_domains+ can be an array of strings, or a Proc that returns an array of strings
|
6
6
|
class EmailDomainInclusionValidator < ActiveModel::EachValidator
|
7
7
|
def validate_each(record, attribute, value)
|
8
|
-
|
9
|
-
|
10
|
-
domain = domain_from_email(value)
|
11
|
-
unless domains.include?(domain)
|
8
|
+
allow_subdomains = options[:allow_subdomains]
|
9
|
+
unless valid_full_domain?(value) || (allow_subdomains && valid_subdomain?(value))
|
12
10
|
record.errors[attribute] << options[:message] || "domain_not_in_list"
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
16
14
|
private
|
15
|
+
def valid_subdomain?(value)
|
16
|
+
domain = domain_from_email(value)
|
17
|
+
allowed_domains.any? { |allowed_domain| domain.end_with? ".#{allowed_domain}" }
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_full_domain?(value)
|
21
|
+
domain = domain_from_email(value)
|
22
|
+
allowed_domains.include?(domain)
|
23
|
+
end
|
24
|
+
|
25
|
+
def allowed_domains
|
26
|
+
domains = options[:allowed_domains]
|
27
|
+
domains = domains.call if domains.respond_to?(:call)
|
28
|
+
domains
|
29
|
+
end
|
30
|
+
|
17
31
|
def domain_from_email(email)
|
18
32
|
Mail::Address.new(email).try(:domain).try(:downcase)
|
19
33
|
rescue Mail::Field::ParseError
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_domain_inclusion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Fecheyr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|