textfilter 0.1.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.
@@ -0,0 +1,29 @@
1
+ Problem
2
+ =======
3
+ - Evil hackers try to insert html/script into model fields
4
+ - Too long texts, and any other sort of stuff we need to take care of...
5
+
6
+ Solution
7
+ ========
8
+ Before asignment apply text filters to the value.
9
+ To use something like strip_tags here you should define it on String see [ActionView helpers on String](http://pragmatig.wordpress.com/2009/05/30/all-actionview-helpers-on-strings)
10
+
11
+ Usage
12
+ =====
13
+ - As Rails plugin `script/plugin install git://github.com/grosser/text_filter.git`
14
+ - As gem `sudo gem install text_filter `
15
+
16
+
17
+ Example:
18
+ class Product < ActiveRecord::Base
19
+ text_filter :title, :description, :with => :strip_tags #=> on each assign: product.title = product.title.strip_tags
20
+ text_filter :summary, :with => [:strip_tags, {:truncate=>5}] #=> on each assign: product.summary = product.summary.strip_tags.truncate(5)
21
+ end
22
+
23
+ Note: with a multi-key-hash in `:with` order of execution is random (before ruby 1.9), e.g. :truncate=>10, :something_else=>[1,2]
24
+
25
+ Author
26
+ ======
27
+ [Michael Grosser](http://pragmatig.wordpress.com)
28
+ grosser.michael@gmail.com
29
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
@@ -0,0 +1,21 @@
1
+ task :default => :spec
2
+ require 'spec/rake/spectask'
3
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
4
+
5
+ begin
6
+ project = 'textfilter'
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = project
10
+ gem.summary = "ActiveRecord/Rails filter text fields before assigning them"
11
+ gem.email = "grosser.michael@gmail.com"
12
+ gem.homepage = "http://github.com/grosser/#{project}"
13
+ gem.authors = ["Michael Grosser"]
14
+ gem.files += (FileList["{lib,spec}/**/*"] + FileList["VERSION"] + FileList["README.markdown"]).to_a.sort
15
+ gem.add_dependency ['activerecord']
16
+ end
17
+
18
+ Jeweler::GemcutterTasks.new
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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ #Needed to load when used as Rails plugin
2
+ require 'text_filter'
@@ -0,0 +1,22 @@
1
+ module TextFilter
2
+ module ClassMethods
3
+ def text_filter(*args)
4
+ options = args.extract_options![:with]
5
+ args.each do |attribute|
6
+ define_method "#{attribute}=" do |value|
7
+ if value.is_a? String
8
+ [*options].each do |method|
9
+ if method.is_a? Hash
10
+ method.each {|name, args| value = value.send(name, *args)}
11
+ else
12
+ value = value.send(method)
13
+ end
14
+ end
15
+ end
16
+ write_attribute attribute, value
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ ActiveRecord::Base.send(:extend, TextFilter::ClassMethods)
@@ -0,0 +1,39 @@
1
+ #simple method versions for test...
2
+ class String
3
+ def strip_tags
4
+ gsub(/<.*?>/,'')
5
+ end
6
+
7
+ def simple_format
8
+ "<p>#{self}</p>"
9
+ end
10
+
11
+ def truncate(length)
12
+ self[0...length-3]+'...'
13
+ end
14
+
15
+ def replace_by_char(char)
16
+ char*length
17
+ end
18
+ end
19
+
20
+ ActiveRecord::Schema.define(:version => 1) do
21
+ create_table :users do |t|
22
+ t.string :name
23
+ end
24
+
25
+ create_table :products do |t|
26
+ t.string :title, :description
27
+ end
28
+ end
29
+
30
+ #create model
31
+ class User < ActiveRecord::Base
32
+ text_filter :name, :with => :strip_tags
33
+ end
34
+
35
+ class Product < ActiveRecord::Base
36
+ text_filter :title, :description, :with => :strip_tags
37
+ text_filter :title, :with => [{:truncate=>10}, :simple_format]
38
+ text_filter :summary, :with => [{:truncate=>10, :replace_by_char=>'x'}, :simple_format]
39
+ end
@@ -0,0 +1,18 @@
1
+ # ---- requirements
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'active_record'
5
+
6
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
7
+
8
+ # ---- setup environment/plugin
9
+ ActiveRecord::Base.establish_connection({
10
+ :adapter => "sqlite3",
11
+ :database => ":memory:",
12
+ })
13
+
14
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
15
+
16
+ require File.expand_path("../init", File.dirname(__FILE__))
17
+
18
+ require 'spec/models'
@@ -0,0 +1,55 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe String do
4
+ it "can strip tags" do
5
+ "<a>xx</a>".strip_tags.should == 'xx'
6
+ end
7
+ end
8
+
9
+ describe TextFilter do
10
+ before do
11
+ @user = User.new
12
+ @product = Product.new
13
+ end
14
+
15
+ it "is called on assign" do
16
+ @user.name = '<a>xx</a>'
17
+ @user.name.should == 'xx'
18
+ end
19
+
20
+ it "does nothing with blank" do
21
+ @user.name = ''
22
+ @user.name.should == ''
23
+ end
24
+
25
+ it "does nothing with nil" do
26
+ @user.name = nil
27
+ @user.name.should == nil
28
+ end
29
+
30
+ it "does nothing with numbers" do
31
+ @user.name = 123
32
+ @user.name.should == 123
33
+ end
34
+
35
+ it "works with multiple fields" do
36
+ @product.description = '<a>xx</a>'
37
+ @product.description.should == 'xx'
38
+ end
39
+
40
+ it "works with nested options" do
41
+ @product.title = '12345678901234567890'
42
+ @product.title.should == '<p>1234567...</p>'
43
+ end
44
+
45
+ it "works with multiple nested options" do
46
+ @product.summary = '12345678901234567890'
47
+ @product.summary.should == '<p>xxxxxxx...</p>'
48
+ end
49
+
50
+ #this behavior cannot be overwritten, but just to be on the safe side...
51
+ it "returns the assigned value" do
52
+ result = (@product.summary = 'abcd')
53
+ result.should == 'abcd'
54
+ end
55
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{textfilter}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2009-12-13}
13
+ s.email = %q{grosser.michael@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ "README.markdown",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/text_filter.rb",
25
+ "lib/text_filter.rb",
26
+ "spec/models.rb",
27
+ "spec/models.rb",
28
+ "spec/spec_helper.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/text_filter_spec.rb",
31
+ "spec/text_filter_spec.rb",
32
+ "textfilter.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/grosser/textfilter}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{ActiveRecord/Rails filter text fields before assigning them}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/text_filter_spec.rb",
42
+ "spec/models.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<activerecord>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<activerecord>, [">= 0"])
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textfilter
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-12-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: grosser.michael@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - README.markdown
35
+ - Rakefile
36
+ - VERSION
37
+ - init.rb
38
+ - lib/text_filter.rb
39
+ - spec/models.rb
40
+ - spec/spec_helper.rb
41
+ - spec/text_filter_spec.rb
42
+ - textfilter.gemspec
43
+ has_rdoc: true
44
+ homepage: http://github.com/grosser/textfilter
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.5
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: ActiveRecord/Rails filter text fields before assigning them
71
+ test_files:
72
+ - spec/spec_helper.rb
73
+ - spec/text_filter_spec.rb
74
+ - spec/models.rb