cold_shoulder 1.0.6 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cold_shoulder.rb +31 -37
- data/locales/en.yml +3 -3
- 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: a119cbf79420849fd9744b5604596fa10cc2e2e8
|
4
|
+
data.tar.gz: 7dd5ea4211aa6aaa3b2cf9342d480cb259ecdad6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 860f36380a17265e4aba35ee08fd06b9f2e22921b45ab8fdbb62f4b39964ce38c8e453d75b85014f8aae1e337dabe08313b4e2d6c55cbc8cbb36e27c6bf291f4
|
7
|
+
data.tar.gz: 01d83e4f04c5872d2b14e4e33e86174e3ad92529429d7a204e2924f52ab1311219257d07d410da057e3a65dd90589ec216457512b9be4cce797cf0ec27fcb0cc
|
data/lib/cold_shoulder.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'active_model/validations'
|
3
|
-
|
4
|
-
require '
|
3
|
+
|
4
|
+
require 'action_view'
|
5
|
+
require 'action_view/helpers'
|
5
6
|
|
6
7
|
module ColdShoulder
|
7
8
|
if defined?(Rails)
|
@@ -20,7 +21,7 @@ module ActiveModel
|
|
20
21
|
|
21
22
|
# Extend the rails each validator so that this can be used like any Rails validator
|
22
23
|
class ColdShoulderValidator < EachValidator
|
23
|
-
include
|
24
|
+
include ActionView::Helpers::NumberHelper if defined?(Rails)
|
24
25
|
|
25
26
|
def validate_each(record, attr_name, value)
|
26
27
|
|
@@ -36,47 +37,40 @@ module ActiveModel
|
|
36
37
|
bullshit_free_phone = value.gsub /[^0-9,]|\n/i, ''
|
37
38
|
|
38
39
|
# Look for matches
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
))
|
52
|
-
end
|
40
|
+
detected = {
|
41
|
+
twitter: globbed_value.scan(twitter_regex),
|
42
|
+
email: value.scan(email_regex),
|
43
|
+
phone: globbed_value.scan(formatted_phone_regex).concat(
|
44
|
+
bullshit_free_phone.scan(formatted_phone_regex)
|
45
|
+
).uniq
|
46
|
+
}
|
47
|
+
|
48
|
+
# Catchall for base
|
49
|
+
errors = false
|
50
|
+
|
51
|
+
[:twitter, :email, :phone].each do |type|
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
unless twitter_handles.empty? or options[:ignore_twitter]
|
64
|
-
record.errors.add(attr_name, :contains_twitter_handle, options.merge(
|
65
|
-
handles: twitter_handles.map{|p| p[0] }.join(', ')
|
66
|
-
))
|
53
|
+
unless detected[type].empty? or options["ignore_#{type}".to_sym]
|
54
|
+
errors = true
|
55
|
+
next if options[:message]
|
56
|
+
|
57
|
+
record.errors.add(attr_name, "contains_#{type}".to_sym, options.merge(
|
58
|
+
detected: detected[type].map{ |p|
|
59
|
+
type == :phone && defined?(Rails) ? number_to_phone(p[0], area_code: true) : p[0]
|
60
|
+
}.join(', ')
|
61
|
+
))
|
67
62
|
end
|
68
63
|
|
69
64
|
end
|
65
|
+
|
66
|
+
# A base mesage might be used
|
67
|
+
if errors and options[:message]
|
68
|
+
record.errors.add(:base, options[:message])
|
69
|
+
end
|
70
|
+
|
70
71
|
end
|
71
72
|
|
72
73
|
module HelperMethods
|
73
|
-
# Validates that the specified attributes do not contain contact information.
|
74
|
-
# Happens by default on save.
|
75
|
-
#
|
76
|
-
# class Message < ActiveRecord::Base
|
77
|
-
# validates_no_contact_in :body
|
78
|
-
# end
|
79
|
-
|
80
74
|
def validates_with_cold_shouldr(*attr_names)
|
81
75
|
validates_with ColdShoulderValidator, _merge_attributes(attr_names)
|
82
76
|
end
|
data/locales/en.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
en:
|
2
2
|
errors:
|
3
3
|
messages:
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
contains_twitter: "contains the twitter handle: %{detected}"
|
5
|
+
contains_phone: "contains the phone number: %{detected}"
|
6
|
+
contains_email: "contains the email address: %{detected}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cold_shoulder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Hoffman
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: actionpack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '>='
|