validates_email_whitelist_of 1.0.2 → 1.1.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.
data/README.markdown CHANGED
@@ -16,23 +16,28 @@ Then on ActiveRecord 2.0+ you can do
16
16
 
17
17
  class User < ActiveRecord::Base
18
18
  validates_email_whitelist_of :email, :whitelist => ['example.com', 'google.com']
19
+
20
+ validates_email_blacklist_of :email, :blacklist => ['123.com']
19
21
  end
20
22
 
21
23
  On ActiveRecord 3.0+ you can do
22
24
 
23
25
  class User < ActiveRecord::Base
24
- validates :email, :whitelist => true
26
+ validates :email, :whitelist => ['example.com', 'google.com']
27
+ validates :email, :blacklist => ['123.com']
25
28
  end
26
29
 
27
30
  If you want to use I18n, make sure you add the scope
28
31
 
29
32
  `activerecord.errors.messages.invalid_email`
30
33
  `activerecord.errors.messages.invalid_whitelist`
34
+ `activerecord.errors.messages.invalid_blacklist`
31
35
 
32
36
  or
33
37
 
34
38
  `errors.messages.invalid_email`
35
39
  `errors.messages.invalid_whitelist`
40
+ `errors.messages.invalid_blacklist`
36
41
 
37
42
  The following locales are builtin:
38
43
 
@@ -4,7 +4,9 @@ en:
4
4
  messages:
5
5
  invalid_email: "is not a valid address"
6
6
  invalid_whitelist: "is not an allowed email"
7
+ invalid_blacklist: "is not an allowed email"
7
8
  errors:
8
9
  messages:
9
10
  invalid_email: "is not a valid e-mail"
10
11
  invalid_whitelist: "is not an allowed email"
12
+ invalid_blacklist: "is not an allowed email"
@@ -4,10 +4,12 @@ pt: &pt
4
4
  messages:
5
5
  invalid_email: "não parece ser um e-mail válido"
6
6
  invalid_whitelist: "não é um e-mail permitido"
7
+ invalid_blacklist: "não é um e-mail permitido"
7
8
  errors:
8
9
  messages:
9
10
  invalid_email: "não parece ser um e-mail válido"
10
11
  invalid_whitelist: "não é um e-mail permitido"
12
+ invalid_blacklist: "não é um e-mail permitido"
11
13
 
12
14
  pt-BR:
13
15
  <<: *pt
@@ -20,6 +20,27 @@ module SimplesIdeias
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ def validates_email_blacklist_of(*attr_names)
25
+ options = {
26
+ :on => :save,
27
+ :allow_nil => false,
28
+ :allow_blank => false,
29
+ :blacklist => []
30
+ }
31
+
32
+ options.update(attr_names.pop) if attr_names.last.kind_of?(Hash)
33
+
34
+ validates_each(attr_names, options) do |record, attr_name, value|
35
+ if value.to_s =~ EMAIL_FORMAT
36
+ message = ::I18n.t(:invalid_blacklist, :scope => 'activerecord.errors.messages')
37
+ record.errors.add(attr_name, message) if options[:blacklist].include?($2)
38
+ else
39
+ message = ::I18n.translate(options[:message], :default => [:"activerecord.errors.messages.invalid_email", "is not a valid address"])
40
+ record.errors.add(attr_name, message)
41
+ end
42
+ end
43
+ end
23
44
  end
24
45
  end
25
46
 
@@ -5,9 +5,13 @@ module ActiveModel
5
5
  if value.to_s !~ SimplesIdeias::ValidatesEmailWhitelistOf::EMAIL_FORMAT
6
6
  record.errors.add(attribute, :invalid_email, :default => options[:message], :value => value)
7
7
  else
8
- unless (options[:whitelist] || []).include?($2)
8
+ if options[:whitelist] && !options[:whitelist].include?($2)
9
9
  record.errors.add(attribute, :invalid_whitelist, :default => options[:message], :value => value)
10
10
  end
11
+
12
+ if options[:blacklist] && options[:blacklist].include?($2)
13
+ record.errors.add(attribute, :invalid_blacklist, :default => options[:message], :value => value)
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -16,6 +20,10 @@ module ActiveModel
16
20
  def validates_email_whitelist_of(*attr_names)
17
21
  validates_with EmailWhitelistValidator, _merge_attributes(attr_names)
18
22
  end
23
+
24
+ def validates_email_blacklist_of(*attr_names)
25
+ validates_with EmailWhitelistValidator, _merge_attributes(attr_names)
26
+ end
19
27
  end
20
28
  end
21
29
  end
@@ -1,8 +1,8 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'validates_email_whitelist_of'
3
3
 
4
- s.version = '1.0.2'
5
- s.summary = 'Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.'
4
+ s.version = '1.1.0'
5
+ s.summary = 'Validates e-mail address format (RFC2822 and RFC3696) and whitelist/blacklist the domain part.'
6
6
  s.description = "Rails 2 and Rails 3 plugin. #{s.summary}"
7
7
  s.homepage = 'https://github.com/rubenfonseca/validates_email_whitelist_of'
8
8
  s.extra_rdoc_files = ['README.markdown', 'MIT-LICENSE']
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 1.0.2
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ruben Fonseca
@@ -63,7 +63,7 @@ dependencies:
63
63
  version: "0"
64
64
  type: :development
65
65
  version_requirements: *id003
66
- description: Rails 2 and Rails 3 plugin. Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.
66
+ description: Rails 2 and Rails 3 plugin. Validates e-mail address format (RFC2822 and RFC3696) and whitelist/blacklist the domain part.
67
67
  email: fonseka@gmail.com
68
68
  executables: []
69
69
 
@@ -121,7 +121,7 @@ rubyforge_project:
121
121
  rubygems_version: 1.7.2
122
122
  signing_key:
123
123
  specification_version: 3
124
- summary: Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.
124
+ summary: Validates e-mail address format (RFC2822 and RFC3696) and whitelist/blacklist the domain part.
125
125
  test_files:
126
126
  - spec/spec_helper.rb
127
127
  - spec/validates_email_format_of_spec.rb