replace_entities 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 James Healy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ == Replace Entities
2
+
3
+ If you import data from confused sources you may end up with HTMLish entites
4
+ littering your model data. I'm looking at you ®
5
+
6
+ If you don't want them, then replace them with the appropriate UTF-8 character
7
+ before they make it into your database.
8
+
9
+
10
+ === Examples
11
+
12
+ class Product < ActiveRecord::Base
13
+ replace_entities!
14
+ end
15
+
16
+ class Product < ActiveRecord::Base
17
+ replace_entities!, :except => :title
18
+ end
19
+
20
+ class Product < ActiveRecord::Base
21
+ replace_entities!, :only => :description
22
+ end
23
+
24
+ === Installation
25
+
26
+ Option 1. Load the plugin as a gem
27
+
28
+ gem install replace_entities
29
+ add "config.gem 'replace_entities'" to your environment.rb
30
+
31
+ Option 2. Use the standard Rails plugin install (assuming Rails >= 2.1).
32
+
33
+ ./script/plugin install git://github.com/yob/replace_entities.git
34
+
35
+ === Caveats
36
+
37
+ The entities are replaced with UTF-8 characters. If you're not expecting UTF-8
38
+ data, you may end up with invalid strings.
39
+
40
+ === Credits
41
+
42
+ This plugin is essentially a fork of the strip attributes plugin, released
43
+ under the MIT License by Ryan McGeary.
44
+
45
+ http://github.com/rmm5t/strip_attributes
46
+
47
+ === License
48
+
49
+ Copyright (c) 2007-2008 Ryan McGeary released under the MIT license
50
+ Copyright (c) 2010 James Healy released under the MIT license
51
+
52
+ http://en.wikipedia.org/wiki/MIT_License
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the dumb quotes plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the replace entities plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Replace Entities'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'replace_entities'
2
+ ActiveRecord::Base.extend(ReplaceEntities)
@@ -0,0 +1,34 @@
1
+ require 'htmlentities'
2
+
3
+ module ReplaceEntities
4
+ # Strips ASCII control chars from attributes before they get saved
5
+ def replace_entities!(options = nil)
6
+ before_validation do |record|
7
+ attributes = ReplaceEntities.narrow(record.attributes, options)
8
+ attributes.each do |attr, value|
9
+ if value.is_a?(String)
10
+ coder = HTMLEntities.new
11
+ record[attr] = coder.decode(value)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ # Necessary because Rails has removed the narrowing of attributes using :only
18
+ # and :except on Base#attributes
19
+ def self.narrow(attributes, options)
20
+ if options.nil?
21
+ attributes
22
+ else
23
+ if except = options[:except]
24
+ except = Array(except).collect { |attribute| attribute.to_s }
25
+ attributes.except(*except)
26
+ elsif only = options[:only]
27
+ only = Array(only).collect { |attribute| attribute.to_s }
28
+ attributes.slice(*only)
29
+ else
30
+ raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
31
+ end
32
+ end
33
+ end
34
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'replace_entities'
2
+ ActiveRecord::Base.extend(ReplaceEntities)
@@ -0,0 +1,92 @@
1
+ # coding: utf-8
2
+
3
+ require "#{File.dirname(__FILE__)}/test_helper"
4
+
5
+ module MockAttributes
6
+ def self.included(base)
7
+ base.column :foo, :string
8
+ base.column :bar, :string
9
+ base.column :biz, :string
10
+ base.column :baz, :string
11
+ end
12
+ end
13
+
14
+ class ConvertAllMockRecord < ActiveRecord::Base
15
+ include MockAttributes
16
+ replace_entities!
17
+ end
18
+
19
+ class ConvertOnlyOneMockRecord < ActiveRecord::Base
20
+ include MockAttributes
21
+ replace_entities! :only => :foo
22
+ end
23
+
24
+ class ConvertOnlyThreeMockRecord < ActiveRecord::Base
25
+ include MockAttributes
26
+ replace_entities! :only => [:foo, :bar, :biz]
27
+ end
28
+
29
+ class ConvertExceptOneMockRecord < ActiveRecord::Base
30
+ include MockAttributes
31
+ replace_entities! :except => :foo
32
+ end
33
+
34
+ class ConvertExceptThreeMockRecord < ActiveRecord::Base
35
+ include MockAttributes
36
+ replace_entities! :except => [:foo, :bar, :biz]
37
+ end
38
+
39
+ class ReplaceEntitiesTest < Test::Unit::TestCase
40
+ def setup
41
+ @init_params = { :foo => "foo&reg;", :bar => "bar&copy;", :biz => "biz&lt;", :baz => "baz&gt;" }
42
+ end
43
+
44
+ def test_should_exist
45
+ assert Object.const_defined?(:ReplaceEntities)
46
+ end
47
+
48
+ def test_should_fix_all_fields
49
+ record = ConvertAllMockRecord.new(@init_params)
50
+ record.valid?
51
+ assert_equal "foo®", record.foo
52
+ assert_equal "bar©", record.bar
53
+ assert_equal "biz<", record.biz
54
+ assert_equal "baz>", record.baz
55
+ end
56
+
57
+ def test_should_convert_only_one_field
58
+ record = ConvertOnlyOneMockRecord.new(@init_params)
59
+ record.valid?
60
+ assert_equal "foo®", record.foo
61
+ assert_equal "bar&copy;", record.bar
62
+ assert_equal "biz&lt;", record.biz
63
+ assert_equal "baz&gt;", record.baz
64
+ end
65
+
66
+ def test_should_convert_only_three_fields
67
+ record = ConvertOnlyThreeMockRecord.new(@init_params)
68
+ record.valid?
69
+ assert_equal "foo®", record.foo
70
+ assert_equal "bar©", record.bar
71
+ assert_equal "biz<", record.biz
72
+ assert_equal "baz&gt;", record.baz
73
+ end
74
+
75
+ def test_should_convert_all_except_one_field
76
+ record = ConvertExceptOneMockRecord.new(@init_params)
77
+ record.valid?
78
+ assert_equal "foo&reg;", record.foo
79
+ assert_equal "bar©", record.bar
80
+ assert_equal "biz<", record.biz
81
+ assert_equal "baz>", record.baz
82
+ end
83
+
84
+ def test_should_convert_all_except_three_fields
85
+ record = ConvertExceptThreeMockRecord.new(@init_params)
86
+ record.valid?
87
+ assert_equal "foo&reg;", record.foo
88
+ assert_equal "bar&copy;", record.bar
89
+ assert_equal "biz&lt;", record.biz
90
+ assert_equal "baz>", record.baz
91
+ end
92
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+
5
+ PLUGIN_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
6
+
7
+ $LOAD_PATH.unshift "#{PLUGIN_ROOT}/lib"
8
+ require "#{PLUGIN_ROOT}/init"
9
+
10
+ class ActiveRecord::Base
11
+ alias_method :save, :valid?
12
+ def self.columns()
13
+ @columns ||= []
14
+ end
15
+
16
+ def self.column(name, sql_type = nil, default = nil, null = true)
17
+ @columns ||= []
18
+ @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: replace_entities
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ version: "1.0"
9
+ platform: ruby
10
+ authors:
11
+ - James Healy
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-06-03 00:00:00 +10:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: htmlentities
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ version: "0"
29
+ type: :runtime
30
+ version_requirements: *id001
31
+ description: a small ActiveRecord plugin that removes common HTML entities from attributes
32
+ email: james@yob.id.au
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - init.rb
41
+ - rails/init.rb
42
+ - lib/replace_entities.rb
43
+ - Rakefile
44
+ - MIT-LICENSE
45
+ - README.rdoc
46
+ has_rdoc: true
47
+ homepage: http://github.com/yob/replace_entities
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --title
53
+ - Replace Entities
54
+ - --line-numbers
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.6
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: a small ActiveRecord plugin that removes common HTML entities from attributes
78
+ test_files:
79
+ - test/replace_entities_test.rb
80
+ - test/test_helper.rb