pixeltrix-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.
@@ -0,0 +1,3 @@
1
+ *1.0.0 (July 13th, 2009)
2
+
3
+ * First release
@@ -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
@@ -0,0 +1,37 @@
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 = "Provides access to the Prowl API."
30
+ gemspec.email = "andyw@pixeltrix.co.uk"
31
+ gemspec.homepage = "http://github.com/pixeltrix/prowler/"
32
+ gemspec.description = "A simple wrapper class that provides basic access to the Prowl API."
33
+ gemspec.authors = ["Andrew White"]
34
+ end
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
37
+ 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,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{nullify_blanks}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Andrew White"]
9
+ s.date = %q{2009-07-13}
10
+ s.description = %q{A simple wrapper class that provides basic access to the Prowl API.}
11
+ s.email = %q{andyw@pixeltrix.co.uk}
12
+ s.extra_rdoc_files = [
13
+ "README"
14
+ ]
15
+ s.files = [
16
+ "CHANGELOG",
17
+ "MIT-LICENSE",
18
+ "README",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "init.rb",
22
+ "lib/nullify_blanks.rb",
23
+ "nullify_blanks.gemspec",
24
+ "rails/init.rb",
25
+ "tasks/nullify_blanks_tasks.rake",
26
+ "test/fixtures/products.yml",
27
+ "test/nullify_blanks_test.rb"
28
+ ]
29
+ s.has_rdoc = true
30
+ s.homepage = %q{http://github.com/pixeltrix/prowler/}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.1}
34
+ s.summary = %q{Provides access to the Prowl API.}
35
+ s.test_files = [
36
+ "test/nullify_blanks_test.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 2
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
@@ -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,4 @@
1
+ product:
2
+ id: 1
3
+ name: "Name"
4
+ description: "Description"
@@ -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,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixeltrix-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-07-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple wrapper class that provides basic access to the Prowl API.
17
+ email: andyw@pixeltrix.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - CHANGELOG
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - VERSION
30
+ - init.rb
31
+ - lib/nullify_blanks.rb
32
+ - nullify_blanks.gemspec
33
+ - rails/init.rb
34
+ - tasks/nullify_blanks_tasks.rake
35
+ - test/fixtures/products.yml
36
+ - test/nullify_blanks_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/pixeltrix/prowler/
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Provides access to the Prowl API.
63
+ test_files:
64
+ - test/nullify_blanks_test.rb