grosser-validates_exclusion_of_html 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ Simple filtering for html as validation.
2
+ There are more complete and complex solutions out there, but this is simple/painless.
3
+
4
+ class User < ActiveRecord::Base
5
+ validates_exclusion_of_html :name, :username
6
+ validates_exclusion_of_html :firstname, :message=>'dont be evil...'
7
+ end
8
+
9
+ Only supports :message option for now (default message is 'must not include &gt; or &lt;')
10
+
11
+ Install
12
+ =======
13
+ script/plugin install git://github.com/grosser/validates_exclusion_of_html.git
14
+ OR
15
+ sudo gem install grosser-validates_exclusion_of_html
16
+
17
+ Author
18
+ ======
19
+ [Michael Grosser](http://pragmatig.wordpress.com)
20
+ grosser.michael@gmail.com
21
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
@@ -0,0 +1,21 @@
1
+ desc "Run all specs in spec directory"
2
+ task :default do
3
+ options = "--colour --format progress --loadby --reverse"
4
+ files = FileList['spec/**/*_spec.rb']
5
+ system("spec #{options} #{files}")
6
+ end
7
+
8
+ begin
9
+ project = 'validates_exclusion_of_html'
10
+ require 'jeweler'
11
+ Jeweler::Tasks.new do |gem|
12
+ gem.name = project
13
+ gem.summary = "ActiveRecord: Simple filtering for html as validation."
14
+ gem.email = "grosser.michael@gmail.com"
15
+ gem.homepage = "http://github.com/grosser/#{project}"
16
+ gem.authors = ["Michael Grosser"]
17
+ gem.files += (FileList["{vendor,lib,spec}/**/*"] + FileList["VERSION.yml"] + FileList["README.markdown"]).to_a.sort
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 1
4
+ :major: 0
@@ -0,0 +1,13 @@
1
+ require 'activerecord'
2
+ class ActiveRecord::Base
3
+ def self.validates_exclusion_of_html(*attributes)
4
+ options = attributes.extract_options!
5
+ validate do |object|
6
+ attributes.each do |attr|
7
+ if object.send(attr) =~ /<|>/
8
+ object.errors.add(attr,options[:message]||'must not include &gt; or &lt;')
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ # ---- requirements
2
+ $LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
3
+ require 'validates_exclusion_of_html'
@@ -0,0 +1,45 @@
1
+ require File.join(File.dirname(__FILE__),'spec_helper')
2
+
3
+ ActiveRecord::Base.establish_connection({
4
+ :adapter => "sqlite3",
5
+ :database => ":memory:",
6
+ })
7
+
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table :users, :force=>true do |t|
10
+ t.string :name, :title
11
+ t.timestamps
12
+ end
13
+ end
14
+
15
+ class User < ActiveRecord::Base
16
+ validates_exclusion_of_html :name
17
+ validates_exclusion_of_html :title, :message=>'custom'
18
+ end
19
+
20
+ describe :validates_exclusion_of_html do
21
+ before do
22
+ @user = User.new(:name=>'<a>evil!</a>')
23
+ end
24
+
25
+ it "makes records invalid" do
26
+ @user.should_not be_valid
27
+ end
28
+
29
+ it "adds an error message to invalid objects" do
30
+ @user.errors.should be_empty
31
+ @user.valid?
32
+ @user.errors[:name].should == "must not include &gt; or &lt;"
33
+ end
34
+
35
+ it "adds a custom message" do
36
+ @user.title = '<script>fu</script>'
37
+ @user.valid?
38
+ @user.errors[:title].should == "custom"
39
+ end
40
+
41
+ it "does not mark valid records as invalid" do
42
+ @user.name = 'not so evil'
43
+ @user.should be_valid
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grosser-validates_exclusion_of_html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: grosser.michael@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - Rakefile
27
+ - VERSION.yml
28
+ - lib/validates_exclusion_of_html.rb
29
+ - spec/spec_helper.rb
30
+ - spec/validates_exclusion_of_html_spec.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/grosser/validates_exclusion_of_html
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: "ActiveRecord: Simple filtering for html as validation."
57
+ test_files:
58
+ - spec/spec_helper.rb
59
+ - spec/validates_exclusion_of_html_spec.rb