railsgarden-nilify_blanks 0.1.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/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ * [0.1.0] Initial Version
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
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,39 @@
1
+ == Nilify Blanks
2
+
3
+ In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.
4
+
5
+ Only attributes responding to empty? with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example. Usage is best shown through examples:
6
+
7
+ == Install
8
+
9
+ # In environment.rb
10
+ config.gem 'railsgarden-nilify_blanks', :lib => 'nilify_blanks', :source => 'http://gems.github.com'
11
+
12
+ == Basic Examples
13
+
14
+ # Checks and converts all fields in the model
15
+ class Post < ActiveRecord::Base
16
+ nilify_blanks
17
+ end
18
+
19
+ # Checks and converts only the title and author fields
20
+ class Post < ActiveRecord::Base
21
+ nilify_blanks :only => [:author, :title]
22
+ end
23
+
24
+ # Checks and converts all fields except for title and author
25
+ class Post < ActiveRecord::Base
26
+ nilify_blanks :except => [:author, :title]
27
+ end
28
+
29
+ == Specifying a Callback
30
+
31
+ Checking uses an ActiveRecord before_save filter by default, but you can specify a different filter with the :before option. Any filter will work - just first remove the "before_" prefix from the name.
32
+
33
+ class Post < ActiveRecord::Base
34
+ nilify_blanks :before => :create
35
+ end
36
+
37
+ class Post < ActiveRecord::Before
38
+ nilify_blanks :before => :validation_on_update
39
+ end
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'echoe'
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the nilify_blanks plugin.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the nilify_blanks plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'NilifyBlanks'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ Echoe.new('nilify_blanks', '0.1.0') do |p|
27
+ p.description = "Auto-convert blank fields to nil."
28
+ p.url = "http://github.com/railsgarden/nilify_blanks"
29
+ p.author = "Ben Hughes"
30
+ p.email = "ben@railsgarden.com"
31
+ p.ignore_pattern = ["tmp/*", "script/*"]
32
+ p.development_dependencies = []
33
+ end
34
+
35
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
36
+
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'nilify_blanks'
2
+
3
+ ActiveRecord::Base.send(:include, Rubiety::NilifyBlanks)
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,46 @@
1
+ module Rubiety
2
+ module NilifyBlanks
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def nilify_blanks(options = {})
9
+ return if self.included_modules.include?(NilifyBlanks::InstanceMethods)
10
+ include NilifyBlanks::InstanceMethods
11
+
12
+ if options[:only]
13
+ options[:only] = [options[:only]] unless options[:only].is_a?(Array)
14
+ options[:only] = options[:only].map(&:to_s)
15
+ end
16
+
17
+ if options[:except]
18
+ options[:except] = [options[:except]] unless options[:except].is_a?(Array)
19
+ options[:except] = options[:except].map(&:to_s)
20
+ end
21
+
22
+ cattr_accessor :nilify_blanks_columns
23
+
24
+ self.nilify_blanks_columns = self.content_columns.reject {|c| !c.null }.map {|c| c.name.to_s }
25
+ self.nilify_blanks_columns = self.nilify_blanks_columns.select {|c| options[:only].include?(c) } if options[:only]
26
+ self.nilify_blanks_columns -= options[:except] if options[:except]
27
+ self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)
28
+
29
+ options[:before] ||= :save
30
+ class_eval "before_#{options[:before]} :nilify_blanks"
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ def nilify_blanks
36
+ (self.nilify_blanks_columns || []).each do |column|
37
+ value = read_attribute(column)
38
+ next unless value.is_a?(String)
39
+ next unless value.nil? or !value.respond_to?(:blank)
40
+
41
+ write_attribute(column, nil) if value.blank?
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{nilify_blanks}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ben Hughes"]
9
+ s.date = %q{2009-01-16}
10
+ s.description = %q{Auto-convert blank fields to nil.}
11
+ s.email = %q{ben@railsgarden.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/nilify_blanks.rb", "README.rdoc", "tasks/nilify_blanks_tasks.rake"]
13
+ s.files = ["CHANGELOG", "init.rb", "install.rb", "lib/nilify_blanks.rb", "Manifest", "MIT-LICENSE", "nilify_blanks.gemspec", "nilify_blanks_plugin.sqlite3", "Rakefile", "README.rdoc", "tasks/nilify_blanks_tasks.rake", "test/config/database.yml", "test/nilify_blanks_plugin.sqlite3", "test/nilify_blanks_test.rb", "test/schema.rb", "test/test_helper.rb", "uninstall.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/railsgarden/nilify_blanks}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nilify_blanks", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{nilify_blanks}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Auto-convert blank fields to nil.}
21
+ s.test_files = ["test/nilify_blanks_test.rb", "test/test_helper.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :nilify_blanks do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,18 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: nilify_blanks_plugin.sqlite
4
+ sqlite3:
5
+ :adapter: sqlite3
6
+ :dbfile: nilify_blanks_plugin.sqlite3
7
+ postgresql:
8
+ :adapter: postgresql
9
+ :username: postgres
10
+ :password: postgres
11
+ :database: nilify_blanks_plugin_test
12
+ :min_messages: ERROR
13
+ mysql:
14
+ :adapter: mysql
15
+ :host: localhost
16
+ :username: root
17
+ :password:
18
+ :database: nilify_blanks_plugin_test
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class NilifyBlanksTest < Test::Unit::TestCase
4
+ context "Model with nilify_blanks" do
5
+ setup do
6
+ class Post < ActiveRecord::Base
7
+ nilify_blanks
8
+ end
9
+
10
+ @post = Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
11
+ @post.save
12
+ end
13
+
14
+ should "recognize all non-null columns" do
15
+ assert_equal ['first_name', 'title', 'summary', 'body', 'views'], Post.nilify_blanks_columns
16
+ end
17
+
18
+ should "convert all blanks to nils" do
19
+ assert_equal nil, @post.first_name
20
+ assert_equal nil, @post.title
21
+ assert_equal nil, @post.summary
22
+ assert_equal nil, @post.body
23
+ end
24
+
25
+ should "leave not-null last name field alone" do
26
+ assert_equal '', @post.last_name
27
+ end
28
+
29
+ should "leave integer views field alone" do
30
+ assert_equal 0, @post.views
31
+ end
32
+ end
33
+
34
+ context "Model with nilify_blanks :only => [:first_name, :title]" do
35
+ setup do
36
+ class PostOnlyFirstNameAndTitle < ActiveRecord::Base
37
+ set_table_name :posts
38
+ nilify_blanks :only => [:first_name, :title]
39
+ end
40
+
41
+ @post = PostOnlyFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
42
+ @post.save
43
+ end
44
+
45
+ should "recognize only first_name and title" do
46
+ assert_equal ['first_name', 'title'], PostOnlyFirstNameAndTitle.nilify_blanks_columns
47
+ end
48
+
49
+ should "convert first_name and title blanks to nils" do
50
+ assert_equal nil, @post.first_name
51
+ assert_equal nil, @post.title
52
+ end
53
+
54
+ should "leave other fields alone" do
55
+ assert_equal '', @post.summary
56
+ assert_equal '', @post.body
57
+ end
58
+ end
59
+
60
+ context "Model with nilify_blanks :except => [:first_name, :title]" do
61
+ setup do
62
+ class PostExceptFirstNameAndTitle < ActiveRecord::Base
63
+ set_table_name :posts
64
+ nilify_blanks :except => [:first_name, :title]
65
+ end
66
+
67
+ @post = PostExceptFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
68
+ @post.save
69
+ end
70
+
71
+ should "recognize only summary, body, and views" do
72
+ assert_equal ['summary', 'body', 'views'], PostExceptFirstNameAndTitle.nilify_blanks_columns
73
+ end
74
+
75
+ should "convert summary and body blanks to nils" do
76
+ assert_equal nil, @post.summary
77
+ assert_equal nil, @post.body
78
+ end
79
+
80
+ should "leave other fields alone" do
81
+ assert_equal '', @post.first_name
82
+ assert_equal '', @post.title
83
+ end
84
+ end
85
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,12 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table :posts, :force => true do |t|
4
+ t.string :first_name
5
+ t.string :last_name, :null => false
6
+ t.string :title
7
+ t.text :summary
8
+ t.text :body
9
+ t.integer :views
10
+ end
11
+
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'active_record/fixtures'
5
+ require 'shoulda/rails'
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/nilify_blanks'))
7
+
8
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'config', '/database.yml')))
9
+ ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
10
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
11
+
12
+ load(File.dirname(__FILE__) + "/schema.rb")
13
+
14
+ require File.expand_path(File.join(File.dirname(__FILE__), '../init'))
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railsgarden-nilify_blanks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Hughes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-16 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Auto-convert blank fields to nil.
17
+ email: ben@railsgarden.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - lib/nilify_blanks.rb
25
+ - README.rdoc
26
+ - tasks/nilify_blanks_tasks.rake
27
+ files:
28
+ - CHANGELOG
29
+ - init.rb
30
+ - install.rb
31
+ - lib/nilify_blanks.rb
32
+ - Manifest
33
+ - MIT-LICENSE
34
+ - nilify_blanks.gemspec
35
+ - nilify_blanks_plugin.sqlite3
36
+ - Rakefile
37
+ - README.rdoc
38
+ - tasks/nilify_blanks_tasks.rake
39
+ - test/config/database.yml
40
+ - test/nilify_blanks_plugin.sqlite3
41
+ - test/nilify_blanks_test.rb
42
+ - test/schema.rb
43
+ - test/test_helper.rb
44
+ - uninstall.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/railsgarden/nilify_blanks
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --line-numbers
50
+ - --inline-source
51
+ - --title
52
+ - Nilify_blanks
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "1.2"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: nilify_blanks
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Auto-convert blank fields to nil.
76
+ test_files:
77
+ - test/nilify_blanks_test.rb
78
+ - test/test_helper.rb