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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cold_shoulder.rb +31 -37
  3. data/locales/en.yml +3 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0236f2cbf906291b1a1060bc38bcd398a0e4f0b0
4
- data.tar.gz: 46482c7b989aaa556044286ee901cd3125b1b09e
3
+ metadata.gz: a119cbf79420849fd9744b5604596fa10cc2e2e8
4
+ data.tar.gz: 7dd5ea4211aa6aaa3b2cf9342d480cb259ecdad6
5
5
  SHA512:
6
- metadata.gz: 9758533a6bd4d2a0ea08e6ac9b2146cca1c93ac0b154a9dea91259e27dd1e55ceef72163091699df840b3aed5eb3063338ebd58d052d3f4afd7da845e9063af3
7
- data.tar.gz: d389c0ca4f0e828515537658b0465524a610bbf0a75bf5963a90789d882093896dc90859ec7bc4b30bd113ddf6cda3a27c076e61f9449c1b7da82039410be6e0
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
- require 'active_support'
4
- require 'active_support/number_helper'
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 ActiveSupport::NumberHelper if defined?(Rails)
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
- twitter_handles = globbed_value.scan twitter_regex
40
- email_addresses = value.scan email_regex
41
- phone_numbers = globbed_value.scan(formatted_phone_regex).concat(
42
- bullshit_free_phone.scan(formatted_phone_regex)
43
- ).uniq
44
-
45
- # Phone numbers
46
- unless phone_numbers.empty? or options[:ignore_phone]
47
- record.errors.add(attr_name, :contains_phone_number, options.merge(
48
- phone_numbers: phone_numbers.map{ |p|
49
- defined?(Rails) ? number_to_phone(p[0]) : p[0]
50
- }.join(', ')
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
- # Email addys
55
- unless email_addresses.empty? or options[:ignore_email]
56
- record.errors.add(attr_name, :contains_email_address, options.merge(
57
- email_addresses: email_addresses.map{|p| p[0] }.join(', ')
58
- ))
59
- else
60
-
61
- # Twitter handles
62
- # Any email address is going to register twitter handles as well
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
- contains_twitter_handle: "contains the twitter handle: %{handles}"
5
- contains_phone_number: "contains the phone number: %{phone_numbers}"
6
- contains_email_address: "contains the email address: %{email_addresses}"
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.6
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: activesupport
28
+ name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='