nullify_blanks 1.0.0
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.
- data/.gitignore +1 -0
- data/CHANGELOG +3 -0
- data/MIT-LICENSE +20 -0
- data/README +10 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/nullify_blanks.rb +32 -0
- data/nullify_blanks.gemspec +51 -0
- data/rails/init.rb +1 -0
- data/tasks/nullify_blanks_tasks.rake +12 -0
- data/test/fixtures/products.yml +4 -0
- data/test/nullify_blanks_test.rb +85 -0
- metadata +67 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andrew White
|
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
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
== NullifyBlanks
|
2
|
+
|
3
|
+
This plugin makes ActiveRecord write blank strings as null to
|
4
|
+
nullable columns in the database rather than zero length strings.
|
5
|
+
|
6
|
+
Just drop the plugin into your plugins folder and that’s all there
|
7
|
+
is to it. There’s a rake task to nullify zero length strings in
|
8
|
+
existing records if required.
|
9
|
+
|
10
|
+
Copyright (c) 2009 Andrew White, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
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 nullify_blanks plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the nullify_blanks plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'NullifyBlanks'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler::Tasks.new do |gemspec|
|
28
|
+
gemspec.name = "nullify_blanks"
|
29
|
+
gemspec.summary = "Nullify blank columns in ActiveRecord"
|
30
|
+
gemspec.email = "andyw@pixeltrix.co.uk"
|
31
|
+
gemspec.homepage = "http://github.com/pixeltrix/nullify_blanks/"
|
32
|
+
gemspec.description = "Ruby on Rails plugin to ensure nullable databases columns are set to null rather than empty strings."
|
33
|
+
gemspec.authors = ["Andrew White"]
|
34
|
+
end
|
35
|
+
Jeweler::GemcutterTasks.new
|
36
|
+
rescue LoadError
|
37
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# == NullifyBlanks
|
2
|
+
#
|
3
|
+
# This plugin makes ActiveRecord write blank strings as null to
|
4
|
+
# nullable columns in the database rather than zero length strings.
|
5
|
+
#
|
6
|
+
# Just drop the plugin into your plugins folder and that’s all there
|
7
|
+
# is to it. There’s a rake task to nullify zero length strings in
|
8
|
+
# existing records if required.
|
9
|
+
#
|
10
|
+
# Copyright (c) 2009 Andrew White, released under the MIT license
|
11
|
+
|
12
|
+
module NullifyBlanks
|
13
|
+
def self.included(base) #:nodoc:
|
14
|
+
base.class_eval do
|
15
|
+
|
16
|
+
def write_attribute_with_nullify(attr_name, value) #:nodoc:
|
17
|
+
write_attribute_without_nullify(attr_name, nullify?(attr_name, value) ? nil : value)
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method_chain :write_attribute, :nullify
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def nullify?(attr_name, value) #:nodoc:
|
25
|
+
value.blank? && ((column = column_for_attribute(attr_name)) && column.text? && column.null)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::Base.send :include, NullifyBlanks
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{nullify_blanks}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andrew White"]
|
12
|
+
s.date = %q{2009-10-05}
|
13
|
+
s.description = %q{Ruby on Rails plugin to ensure nullable databases columns are set to null rather than empty strings.}
|
14
|
+
s.email = %q{andyw@pixeltrix.co.uk}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"CHANGELOG",
|
21
|
+
"MIT-LICENSE",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/nullify_blanks.rb",
|
27
|
+
"nullify_blanks.gemspec",
|
28
|
+
"rails/init.rb",
|
29
|
+
"tasks/nullify_blanks_tasks.rake",
|
30
|
+
"test/fixtures/products.yml",
|
31
|
+
"test/nullify_blanks_test.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/pixeltrix/nullify_blanks/}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Nullify blank columns in ActiveRecord}
|
38
|
+
s.test_files = [
|
39
|
+
"test/nullify_blanks_test.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'nullify_blanks'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
desc "Nullify all the nullable text columns in the model specified in MODEL"
|
2
|
+
task :nullify_blanks do
|
3
|
+
raise RuntimeError, "Please specify a model to nullify" if ENV["MODEL"].blank?
|
4
|
+
|
5
|
+
Rake::Task[:environment].invoke
|
6
|
+
|
7
|
+
klass = ENV["MODEL"].constantize
|
8
|
+
klass.columns.each do |column|
|
9
|
+
quoted_column = klass.connection.quote_column_name(column.name)
|
10
|
+
klass.update_all("#{quoted_column} = NULL", "#{quoted_column} = ''") if column.text? && column.null
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/fixtures'
|
5
|
+
require File.dirname(__FILE__) + '/../lib/nullify_blanks'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
8
|
+
|
9
|
+
def setup_db
|
10
|
+
ActiveRecord::Migration.suppress_messages do
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
create_table :products do |t|
|
13
|
+
t.string :name, :limit => 100, :null => false
|
14
|
+
t.string :description, :limit => 255, :null => true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures', ActiveRecord::Base.connection.tables)
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_db
|
23
|
+
ActiveRecord::Migration.suppress_messages do
|
24
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
25
|
+
ActiveRecord::Base.connection.drop_table(table)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Fixtures.reset_cache
|
30
|
+
end
|
31
|
+
|
32
|
+
class Product < ActiveRecord::Base
|
33
|
+
end
|
34
|
+
|
35
|
+
class NullifyBlanksTest < Test::Unit::TestCase
|
36
|
+
|
37
|
+
def setup
|
38
|
+
setup_db
|
39
|
+
end
|
40
|
+
|
41
|
+
def teardown
|
42
|
+
teardown_db
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_nullable_column_is_nullified_when_assigned_an_empty_string
|
46
|
+
product = Product.find(1)
|
47
|
+
assert !product.description.blank?
|
48
|
+
product.update_attributes!(:description => "")
|
49
|
+
product.reload
|
50
|
+
assert product.description.nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_nullable_column_is_nullified_when_assigned_a_nil_value
|
54
|
+
product = Product.find(1)
|
55
|
+
assert !product.description.blank?
|
56
|
+
product.update_attributes!(:description => nil)
|
57
|
+
product.reload
|
58
|
+
assert product.description.nil?
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_nullable_column_is_not_nullified_when_assigned_a_non_empty_string
|
62
|
+
product = Product.find(1)
|
63
|
+
assert !product.description.blank?
|
64
|
+
product.update_attributes!(:description => "Description")
|
65
|
+
product.reload
|
66
|
+
assert !product.description.nil?
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_not_null_column_is_not_nullified_when_assigned_an_empty_string
|
70
|
+
product = Product.find(1)
|
71
|
+
assert !product.name.blank?
|
72
|
+
product.update_attributes!(:name => "")
|
73
|
+
product.reload
|
74
|
+
assert !product.name.nil?
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_not_null_column_is_not_nullified_when_assigned_a_non_empty_string
|
78
|
+
product = Product.find(1)
|
79
|
+
assert !product.name.blank?
|
80
|
+
product.update_attributes!(:name => "Name")
|
81
|
+
product.reload
|
82
|
+
assert !product.name.nil?
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nullify_blanks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew White
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-05 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Ruby on Rails plugin to ensure nullable databases columns are set to null rather than empty strings.
|
17
|
+
email: andyw@pixeltrix.co.uk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- CHANGELOG
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- init.rb
|
32
|
+
- lib/nullify_blanks.rb
|
33
|
+
- nullify_blanks.gemspec
|
34
|
+
- rails/init.rb
|
35
|
+
- tasks/nullify_blanks_tasks.rake
|
36
|
+
- test/fixtures/products.yml
|
37
|
+
- test/nullify_blanks_test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/pixeltrix/nullify_blanks/
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Nullify blank columns in ActiveRecord
|
66
|
+
test_files:
|
67
|
+
- test/nullify_blanks_test.rb
|