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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6693a7481613ac7e5d8021b5d3aaf682c6f82dac
4
- data.tar.gz: efb361f0b7a53f03692653c660dc4c90135a1d14
3
+ metadata.gz: 2729b5136cdc717dd8f70812757a632dbf3f0438
4
+ data.tar.gz: bebb71f95e7b16d6228a26135218288031527286
5
5
  SHA512:
6
- metadata.gz: 0439b71550c1d0f4524a9d8a852ab3add86522d9e95fc2a4cea4ef39ad0457fa07aadda830149345dcc53da72d93fa6711138ad2e81eebf561d11c00d5486ec7
7
- data.tar.gz: 25135bf01635e1b8635f23d29b6723146b5fb81ac7c425670feb0e341857abfb622390f71ccd9a5bea2b3515f42b0a5593065eeb57beebd1a024ce13500f730e
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
 
@@ -1,3 +1,3 @@
1
1
  module EmailDomainInclusion
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -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
- domains = options[:allowed_domains]
9
- domains = domains.call if domains.respond_to?(:call)
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.1.0
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-02-25 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler