validates_potty_mouth 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +12 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +13 -0
- data/lib/validates_potty_mouth/default_word_list.txt +19 -0
- data/lib/validates_potty_mouth/potty_mouth_validator.rb +37 -0
- data/lib/validates_potty_mouth/version.rb +3 -0
- data/lib/validates_potty_mouth.rb +7 -0
- data/test/custom_list.txt +2 -0
- data/test/model_test.rb +58 -0
- data/test/test_helper.rb +5 -0
- data/validates_potty_mouth.gemspec +25 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3aabdc86be9349fa62814915e1b71bdb793a453c
|
4
|
+
data.tar.gz: 9a0265cac74bbdccccca373be7c51724f8b3626e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a5b664d3602ab4e46b4601ff73e43b7db485c387b826c7e53c69a4b9bdd4e3463c2f7893752b650488371102c9e7fd44407095c85a02527f51c87bebb4cf605
|
7
|
+
data.tar.gz: 3804ae8129f19169ad6f5f35b35e4f64e45d485ba395cc11705dac76b33bbb36a972ffc90008bddc3cbd4879347bbb91135f194af24c8d95da0287dbd0221433
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in validates_not_shouting.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
am_version = ENV["AM_VERSION"] || "default"
|
7
|
+
|
8
|
+
am_version = case am_version
|
9
|
+
when "default"
|
10
|
+
">= 3.2.0"
|
11
|
+
else
|
12
|
+
"~> #{am_version}"
|
13
|
+
end
|
14
|
+
|
15
|
+
gem "activemodel", am_version
|
16
|
+
|
17
|
+
gem "codeclimate-test-reporter", group: :test, require: nil
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jeff Ching
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# ValidatesPottyMouth [](https://travis-ci.org/avvo/validates_potty_mouth) [](https://codeclimate.com/github/avvo/validates_potty_mouth) [](https://codeclimate.com/github/avvo/validates_potty_mouth)
|
2
|
+
|
3
|
+
Validate against blacklist of words
|
4
|
+
|
5
|
+
Originally idea conceived by [bvandenbos](https://github.com/bvandenbos). ActiveModel::Validator
|
6
|
+
to prevent bad words in your data.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'validates_potty_mouth'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install validates_potty_mouth
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```
|
27
|
+
|
28
|
+
class Foo
|
29
|
+
include ActiveModel::Validations
|
30
|
+
attr_accessor :foo
|
31
|
+
|
32
|
+
validates :foo, potty_mouth: true
|
33
|
+
end
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
## Configuration
|
38
|
+
|
39
|
+
You can set the word list message as configuration for the validator. The default list is named `:default`.
|
40
|
+
|
41
|
+
validates :foo, potty_mouth: {list: :mylist, message: 'cannot be so shouty.'}
|
42
|
+
|
43
|
+
To add a word list:
|
44
|
+
|
45
|
+
PottyMouthValidator.add_list(:my_list, '/path/to_file')
|
46
|
+
|
47
|
+
The file should have one word per line.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/avvo/validates_potty_mouth/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# this is not namespaced so that we can do
|
2
|
+
# validates :body, potty_mouth: true
|
3
|
+
class PottyMouthValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
return true if value.blank?
|
6
|
+
|
7
|
+
if banned?(value)
|
8
|
+
record.errors[attribute] << options.fetch(:message, "contains objectionable content")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def banned?(value)
|
13
|
+
text = value.gsub(/[\W\d]/, ' ') # get rid of non-letters
|
14
|
+
words = text.split.to_set
|
15
|
+
words.any?{|word| banned_word?(word)}
|
16
|
+
end
|
17
|
+
|
18
|
+
def banned_word?(word)
|
19
|
+
banned_word_list.include?(word)
|
20
|
+
end
|
21
|
+
|
22
|
+
def banned_word_list
|
23
|
+
self.class.banned_word_lists[options.fetch(:list, :default)]
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def banned_word_lists
|
28
|
+
@lists ||= Hash.new { Set.new }
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_word_list(type, path)
|
32
|
+
banned_word_lists[type.to_sym] = File.read(path).split("\n").to_set
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
PottyMouthValidator.add_word_list(:default, File.expand_path("../default_word_list.txt", __FILE__))
|
data/test/model_test.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ModelTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
class TestModel
|
6
|
+
attr_accessor :body, :subject
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validates :body, :subject, potty_mouth: true
|
10
|
+
end
|
11
|
+
|
12
|
+
class CustomList
|
13
|
+
attr_accessor :body, :subject
|
14
|
+
include ActiveModel::Validations
|
15
|
+
|
16
|
+
validates :body, :subject, potty_mouth: {list: :custom}
|
17
|
+
end
|
18
|
+
|
19
|
+
class MissingList
|
20
|
+
attr_accessor :body, :subject
|
21
|
+
include ActiveModel::Validations
|
22
|
+
|
23
|
+
validates :body, :subject, potty_mouth: {list: :missing}
|
24
|
+
end
|
25
|
+
|
26
|
+
PottyMouthValidator.add_word_list(:custom, File.expand_path("../custom_list.txt", __FILE__))
|
27
|
+
|
28
|
+
def test_validates
|
29
|
+
m = TestModel.new
|
30
|
+
m.subject = "what the fuck?"
|
31
|
+
m.body = "what the fuck is this?"
|
32
|
+
assert !m.valid?
|
33
|
+
|
34
|
+
assert m.errors[:subject].present?
|
35
|
+
assert m.errors[:body].present?
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_custom_list
|
39
|
+
m = CustomList.new
|
40
|
+
m.subject = "what the fuck?"
|
41
|
+
m.body = "foobar blah"
|
42
|
+
assert !m.valid?
|
43
|
+
|
44
|
+
assert m.errors[:subject].empty?
|
45
|
+
assert m.errors[:body].present?
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_missing_list
|
49
|
+
m = MissingList.new
|
50
|
+
m.subject = "what the fuck?"
|
51
|
+
m.body = "foobar blah"
|
52
|
+
assert m.valid?
|
53
|
+
|
54
|
+
assert m.errors[:subject].empty?
|
55
|
+
assert m.errors[:body].empty?
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validates_potty_mouth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "validates_potty_mouth"
|
8
|
+
spec.version = ValidatesPottyMouth::VERSION
|
9
|
+
spec.authors = ["Jeff Ching"]
|
10
|
+
spec.email = ["ching.jeff@gmail.com"]
|
11
|
+
spec.summary = "Validate against blacklist of words"
|
12
|
+
spec.description = "Validate against blacklist of words"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", ">= 3.2.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_potty_mouth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Ching
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Validate against blacklist of words
|
56
|
+
email:
|
57
|
+
- ching.jeff@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/validates_potty_mouth.rb
|
69
|
+
- lib/validates_potty_mouth/default_word_list.txt
|
70
|
+
- lib/validates_potty_mouth/potty_mouth_validator.rb
|
71
|
+
- lib/validates_potty_mouth/version.rb
|
72
|
+
- test/custom_list.txt
|
73
|
+
- test/model_test.rb
|
74
|
+
- test/test_helper.rb
|
75
|
+
- validates_potty_mouth.gemspec
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Validate against blacklist of words
|
100
|
+
test_files:
|
101
|
+
- test/custom_list.txt
|
102
|
+
- test/model_test.rb
|
103
|
+
- test/test_helper.rb
|
104
|
+
has_rdoc:
|