dumb_quotes 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +63 -0
- data/Rakefile +22 -0
- data/init.rb +2 -0
- data/lib/dumb_quotes.rb +41 -0
- data/rails/init.rb +2 -0
- data/test/dumb_quotes_test.rb +102 -0
- data/test/test_helper.rb +20 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Peter Yandell
|
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,63 @@
|
|
1
|
+
== Dumb Quotes
|
2
|
+
|
3
|
+
For some reason I'm yet discern, I'm irrationally annoyed by "smart quotes",
|
4
|
+
the non-ASCII single and double quotes imposed on our databases by windows
|
5
|
+
users.
|
6
|
+
|
7
|
+
There's no real reason to hate them, but I do. Maybe it's the extra few bytes
|
8
|
+
they consume when encoded as UTF-8, or maybe it's the way they get converted to
|
9
|
+
numeric entities in my XML exports. In any case, this ActiveRecord plugin makes
|
10
|
+
the following changes to your model attributes before validation:
|
11
|
+
|
12
|
+
U+2018 (‘) -> '
|
13
|
+
U+2019 (’) -> '
|
14
|
+
U+02BC (ʼ) -> '
|
15
|
+
U+201C (“) -> "
|
16
|
+
U+201D (”) -> "
|
17
|
+
U+02EE (ˮ) -> "
|
18
|
+
|
19
|
+
=== Examples
|
20
|
+
|
21
|
+
class Product < ActiveRecord::Base
|
22
|
+
dumb_quotes!
|
23
|
+
end
|
24
|
+
|
25
|
+
class Product < ActiveRecord::Base
|
26
|
+
dumb_quotes!, :except => :title
|
27
|
+
end
|
28
|
+
|
29
|
+
class Product < ActiveRecord::Base
|
30
|
+
dumb_quotes!, :only => :description
|
31
|
+
end
|
32
|
+
|
33
|
+
=== Installation
|
34
|
+
|
35
|
+
Option 1. Load the plugin as a gem
|
36
|
+
|
37
|
+
gem install dumb_quotes
|
38
|
+
add "config.gem 'dumb_quotes'" to your environment.rb
|
39
|
+
|
40
|
+
Option 2. Use the standard Rails plugin install (assuming Rails 2.1).
|
41
|
+
|
42
|
+
./script/plugin install git://github.com/yob/dumb_quotes.git
|
43
|
+
|
44
|
+
=== Caveats
|
45
|
+
|
46
|
+
The translation is done with raw bytes. If you happen to be using anything other than
|
47
|
+
UTF-8 in your models, you're likely to munge data.
|
48
|
+
|
49
|
+
This also means it's almost certainly not ruby 1.9 compatible. Patches welcome.
|
50
|
+
|
51
|
+
=== Credits
|
52
|
+
|
53
|
+
This plugin is essentially a fork of the strip attributes plugin, released
|
54
|
+
under the MIT License by Ryan McGeary.
|
55
|
+
|
56
|
+
http://github.com/rmm5t/strip_attributes
|
57
|
+
|
58
|
+
=== License
|
59
|
+
|
60
|
+
Copyright (c) 2007-2008 Ryan McGeary released under the MIT license
|
61
|
+
Copyright (c) 2009 James Healy released under the MIT license
|
62
|
+
|
63
|
+
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 dumb quotes plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Dumb Quotes'
|
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
data/lib/dumb_quotes.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module DumbQuotes
|
2
|
+
# Strips whitespace from model fields and converts blank values to nil.
|
3
|
+
def dumb_quotes!(options = nil)
|
4
|
+
before_validation do |record|
|
5
|
+
attributes = DumbQuotes.narrow(record.attributes, options)
|
6
|
+
attributes.each do |attr, value|
|
7
|
+
if value.respond_to?(:gsub)
|
8
|
+
# single quotes
|
9
|
+
value.gsub!("\xE2\x80\x98","'") # U+2018
|
10
|
+
value.gsub!("\xE2\x80\x99","'") # U+2019
|
11
|
+
value.gsub!("\xCA\xBC","'") # U+02BC
|
12
|
+
|
13
|
+
# double quotes
|
14
|
+
value.gsub!("\xE2\x80\x9C",'"') # U+201C
|
15
|
+
value.gsub!("\xE2\x80\x9D",'"') # U+201D
|
16
|
+
value.gsub!("\xCB\xAE",'"') # U+02EE
|
17
|
+
|
18
|
+
record[attr] = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Necessary because Rails has removed the narrowing of attributes using :only
|
25
|
+
# and :except on Base#attributes
|
26
|
+
def self.narrow(attributes, options)
|
27
|
+
if options.nil?
|
28
|
+
attributes
|
29
|
+
else
|
30
|
+
if except = options[:except]
|
31
|
+
except = Array(except).collect { |attribute| attribute.to_s }
|
32
|
+
attributes.except(*except)
|
33
|
+
elsif only = options[:only]
|
34
|
+
only = Array(only).collect { |attribute| attribute.to_s }
|
35
|
+
attributes.slice(*only)
|
36
|
+
else
|
37
|
+
raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
module MockAttributes
|
4
|
+
def self.included(base)
|
5
|
+
base.column :foo, :string
|
6
|
+
base.column :bar, :string
|
7
|
+
base.column :biz, :string
|
8
|
+
base.column :baz, :string
|
9
|
+
base.column :bob, :string
|
10
|
+
base.column :alice, :string
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ConvertAllMockRecord < ActiveRecord::Base
|
15
|
+
include MockAttributes
|
16
|
+
dumb_quotes!
|
17
|
+
end
|
18
|
+
|
19
|
+
class ConvertOnlyOneMockRecord < ActiveRecord::Base
|
20
|
+
include MockAttributes
|
21
|
+
dumb_quotes! :only => :foo
|
22
|
+
end
|
23
|
+
|
24
|
+
class ConvertOnlyThreeMockRecord < ActiveRecord::Base
|
25
|
+
include MockAttributes
|
26
|
+
dumb_quotes! :only => [:foo, :bar, :biz]
|
27
|
+
end
|
28
|
+
|
29
|
+
class ConvertExceptOneMockRecord < ActiveRecord::Base
|
30
|
+
include MockAttributes
|
31
|
+
dumb_quotes! :except => :foo
|
32
|
+
end
|
33
|
+
|
34
|
+
class ConvertExceptThreeMockRecord < ActiveRecord::Base
|
35
|
+
include MockAttributes
|
36
|
+
dumb_quotes! :except => [:foo, :bar, :biz]
|
37
|
+
end
|
38
|
+
|
39
|
+
class DumbQuotesTest < Test::Unit::TestCase
|
40
|
+
def setup
|
41
|
+
@init_params = { :foo => "foo‘", :bar => "bar’", :biz => "bizʼ", :baz => "baz“", :bob => "bob”", :alice => "aliceˮ" }
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_should_exist
|
45
|
+
assert Object.const_defined?(:DumbQuotes)
|
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
|
+
assert_equal "bob\"", record.bob
|
56
|
+
assert_equal "alice\"", record.alice
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_should_convert_only_one_field
|
60
|
+
record = ConvertOnlyOneMockRecord.new(@init_params)
|
61
|
+
record.valid?
|
62
|
+
assert_equal "foo'", record.foo
|
63
|
+
assert_equal "bar’", record.bar
|
64
|
+
assert_equal "bizʼ", record.biz
|
65
|
+
assert_equal "baz“", record.baz
|
66
|
+
assert_equal "bob”", record.bob
|
67
|
+
assert_equal "aliceˮ", record.alice
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_convert_only_three_fields
|
71
|
+
record = ConvertOnlyThreeMockRecord.new(@init_params)
|
72
|
+
record.valid?
|
73
|
+
assert_equal "foo'", record.foo
|
74
|
+
assert_equal "bar'", record.bar
|
75
|
+
assert_equal "biz'", record.biz
|
76
|
+
assert_equal "baz“", record.baz
|
77
|
+
assert_equal "bob”", record.bob
|
78
|
+
assert_equal "aliceˮ", record.alice
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_should_convert_all_except_one_field
|
82
|
+
record = ConvertExceptOneMockRecord.new(@init_params)
|
83
|
+
record.valid?
|
84
|
+
assert_equal "foo‘", record.foo
|
85
|
+
assert_equal "bar'", record.bar
|
86
|
+
assert_equal "biz'", record.biz
|
87
|
+
assert_equal "baz\"", record.baz
|
88
|
+
assert_equal "bob\"", record.bob
|
89
|
+
assert_equal "alice\"", record.alice
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_should_convert_all_except_three_fields
|
93
|
+
record = ConvertExceptThreeMockRecord.new(@init_params)
|
94
|
+
record.valid?
|
95
|
+
assert_equal "foo‘", record.foo
|
96
|
+
assert_equal "bar’", record.bar
|
97
|
+
assert_equal "bizʼ", record.biz
|
98
|
+
assert_equal "baz\"", record.baz
|
99
|
+
assert_equal "bob\"", record.bob
|
100
|
+
assert_equal "alice\"", record.alice
|
101
|
+
end
|
102
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -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,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dumb_quotes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Healy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-16 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: a small ActiveRecord plugin that converts 'smart quotes' to their ASCII equivalents
|
17
|
+
email: james@yob.id.au
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- init.rb
|
26
|
+
- rails/init.rb
|
27
|
+
- lib/dumb_quotes.rb
|
28
|
+
- Rakefile
|
29
|
+
- MIT-LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/yob/dumb_quotes/tree/master
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --title
|
36
|
+
- Dumb Quotes
|
37
|
+
- --line-numbers
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project: yob-projects
|
55
|
+
rubygems_version: 1.3.1
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: a small ActiveRecord plugin that converts 'smart quotes' to their ASCII equivalents
|
59
|
+
test_files:
|
60
|
+
- test/dumb_quotes_test.rb
|
61
|
+
- test/test_helper.rb
|