validates_not_profane 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Michael J. Edgar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # validates_not_profane
2
+
3
+ This is a fork of Michael Edgar's [validates\_not\_profane](https://github.com/michaeledgar/validates_not_profane)
4
+ plugin, updated to a Rails 3 style validator gem.
5
+
6
+ ## Description
7
+
8
+ validates_not_profane provides a hook into the Profanalyzer gem as a validation
9
+ for your ActiveRecord models. It's use is simple:
10
+
11
+ validates_not_profane :column_name, :column_2
12
+
13
+ Will cause the model to produce errors if that column contains profanity
14
+ in its value. Of course, since the Profanalyzer gem is customizable, so is
15
+ the validation:
16
+
17
+ validates_not_profane :post_title, :racist => true, :sexual => false
18
+
19
+ will block racial slurs, but nothing else.
20
+
21
+ validates_not_profane :post_title, :tolerance => 5
22
+
23
+ will block only the most vile of profanity. The scale for tolerance is
24
+ from 0-5, which is mostly subjective, in all honesty.
25
+
26
+ ## Features/Problems
27
+
28
+ * Tolerance-based profanity checking
29
+ * Switch between checking all words, racist terms, sexual words, or some mixture
30
+ * Custom substitutions
31
+ * Boolean-based profanity checking
32
+
33
+ ## Install (with bundler)
34
+
35
+ In your Gemfile:
36
+
37
+ gem 'validates_not_profane', :git => "git@github.com:akqa/validates_not_profane.git"
38
+
39
+ Then run `bundle install`
40
+
41
+ ## License
42
+
43
+ (The MIT License)
44
+
45
+ Copyright (c) 2009 Michael J. Edgar
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ 'Software'), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
61
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
62
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
63
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
64
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the validates_not_profane plugin.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the validates_not_profane plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'ValidatesNotProfane'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
@@ -0,0 +1,65 @@
1
+ require 'active_model'
2
+ require 'profanalyzer'
3
+
4
+ module ActiveModel
5
+ module Validations
6
+ class ProfanityValidator < ActiveModel::EachValidator
7
+ DEFAULT_TOLERANCE = 2
8
+
9
+ def initialize(options)
10
+ super(options.reverse_merge(:message => "contains profanity"))
11
+ end
12
+
13
+ def validate_each(record, attribute, value)
14
+ if options.has_key?(:tolerance)
15
+ Profanalyzer.tolerance = options[:tolerance]
16
+ else
17
+ Profanalyzer.tolerance = DEFAULT_TOLERANCE
18
+ end
19
+
20
+ if options[:all]
21
+ Profanalyzer.check_all = true
22
+ elsif options[:sexual] && options[:racist]
23
+ Profanalyzer.check_all = false
24
+ Profanalyzer.check_sexual = true
25
+ Profanalyzer.check_racist = true
26
+ elsif options[:sexual]
27
+ Profanalyzer.check_all = false
28
+ Profanalyzer.check_sexual = true
29
+ Profanalyzer.check_racist = false
30
+ elsif options[:racist]
31
+ Profanalyzer.check_all = false
32
+ Profanalyzer.check_sexual = false
33
+ Profanalyzer.check_racist = true
34
+ else
35
+ Profanalyzer.check_all = true
36
+ end
37
+
38
+ if Profanalyzer.profane?(value)
39
+ record.errors.add(attribute, :profane, options)
40
+ end
41
+ end
42
+ end
43
+
44
+ module HelperMethods
45
+ # Validates whether the value of the specified attributes contain
46
+ # profanity
47
+ #
48
+ # class Comment < ActiveRecord::Base
49
+ # validates_not_profane :body
50
+ # end
51
+ #
52
+ # Configuration options:
53
+ # * <tt>:message</tt> - A custom error message (default is: "must not contain any vulgar words").
54
+ # * <tt>:tolerance</tt> - Tolerance for Profanalyzer
55
+ # * <tt>:sexual</tt> - Whether to check for sexual words
56
+ # * <tt>:racist</tt> - Whether to check for racial slurs
57
+ # * <tt>:all</tt> - Whether to check all bad words
58
+ def validates_not_profane(*attr_names)
59
+ validates_with ProfanityValidator, _merge_attributes(attr_names)
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+
@@ -0,0 +1,3 @@
1
+ module ValidatesNotProfane
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'validates_not_profane'
4
+ require 'test/unit'
5
+
6
+ class Model
7
+ include ActiveModel::Validations
8
+ def initialize(atts)
9
+ atts.each { |k,v| send("#{k}=", v) }
10
+ end
11
+ end
12
+
13
+ class User < Model
14
+ attr_accessor :name, :bio
15
+
16
+ validates_not_profane :name
17
+ validates_not_profane :bio, :tolerance => 5
18
+ end
19
+
20
+ class Post < Model
21
+ attr_accessor :subject, :post
22
+
23
+ validates_not_profane :subject, :racist => true
24
+ validates_not_profane :post, :sexual => true
25
+ end
26
+
27
+ class Test::Unit::TestCase
28
+ def assert_valid(record)
29
+ assert_block { record.valid? }
30
+ end
31
+
32
+ def assert_invalid(record)
33
+ assert_block { !record.valid? }
34
+ end
35
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper.rb'
2
+
3
+ class ValidatesNotProfaneTest < Test::Unit::TestCase
4
+
5
+ def test_basic_blocks_profane
6
+ u = User.new(:name => "fucker")
7
+ assert_invalid(u)
8
+ end
9
+
10
+ def test_basic_allows_clean
11
+ u = User.new(:name => "Steve")
12
+ assert_valid(u)
13
+ end
14
+
15
+ def test_tolerance_blocks
16
+ u = User.new(:bio => "fucking prick") # badness = 5
17
+ assert_invalid(u)
18
+ end
19
+
20
+ def test_tolerance_allows
21
+ u = User.new(:bio => "queef") # badness = 4
22
+ assert_valid(u)
23
+ end
24
+
25
+ def test_racist_blocks
26
+ u = Post.new(:subject => "mick")
27
+ assert_invalid(u)
28
+ end
29
+
30
+ def test_racist_allows_others
31
+ u = Post.new(:subject => "fucker")
32
+ assert_valid(u)
33
+ end
34
+
35
+ def test_sexual_blocks
36
+ u = Post.new(:post => "cocksucker")
37
+ assert_invalid(u)
38
+ end
39
+
40
+ def test_sexual_allows_others
41
+ u = Post.new(:post => "mick") # again, I'm Irish
42
+ assert_valid(u)
43
+ end
44
+
45
+ def test_default_message
46
+ u = User.new(:name => "fucker")
47
+ assert_invalid(u)
48
+ assert_equal ["contains profanity"], u.errors[:name]
49
+ end
50
+
51
+ class CustomUser < Model
52
+ attr_accessor :name
53
+ validates_not_profane :name, :message => "Sorry, can't say that!"
54
+ end
55
+
56
+ def test_custom_message
57
+ u = CustomUser.new(:name => "fucker")
58
+ assert_invalid(u)
59
+ assert_equal ["Sorry, can't say that!"], u.errors[:name]
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_not_profane/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validates_not_profane"
7
+ s.version = ValidatesNotProfane::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ben Marini", "Michael j. Edgar"]
10
+ s.email = ["bmarini@gmail.com"]
11
+ s.homepage = "http://github.com/akqa/validates_not_profane"
12
+ s.summary = %q{Rails 3 profanity validator for active model}
13
+ s.description = %q{Rails 3 profanity validator for active model}
14
+
15
+ s.rubyforge_project = "validates_not_profane"
16
+
17
+ s.add_dependency "profanalyzer", "~> 1.0"
18
+ s.add_dependency "activemodel", "~> 3.0"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_not_profane
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Ben Marini
14
+ - Michael j. Edgar
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-11 00:00:00 -08:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: profanalyzer
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 1
33
+ - 0
34
+ version: "1.0"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activemodel
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ version: "3.0"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Rails 3 profanity validator for active model
53
+ email:
54
+ - bmarini@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/validates_not_profane.rb
68
+ - lib/validates_not_profane/version.rb
69
+ - test/test_helper.rb
70
+ - test/validates_not_profane_test.rb
71
+ - validates_not_profane.gemspec
72
+ has_rdoc: true
73
+ homepage: http://github.com/akqa/validates_not_profane
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: validates_not_profane
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Rails 3 profanity validator for active model
106
+ test_files:
107
+ - test/test_helper.rb
108
+ - test/validates_not_profane_test.rb