nilify_blanks 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ == [2010-10-15] 1.0.0 Major Updates
2
+
3
+ * Finally updated to be a Gem
4
+ * Internal refactoring
5
+ * Converted tests to RSpec
6
+ * Added support for Rails 3.0
7
+
8
+ == 0.1.2 Fixed issue with table not existing.
9
+ == 0.1.0 Initial Version
data/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,40 @@
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
+ Include the gem using bundler in your Gemfile:
10
+
11
+ gem "nilify_blanks"
12
+
13
+ == Basic Examples
14
+
15
+ # Checks and converts all fields in the model
16
+ class Post < ActiveRecord::Base
17
+ nilify_blanks
18
+ end
19
+
20
+ # Checks and converts only the title and author fields
21
+ class Post < ActiveRecord::Base
22
+ nilify_blanks :only => [:author, :title]
23
+ end
24
+
25
+ # Checks and converts all fields except for title and author
26
+ class Post < ActiveRecord::Base
27
+ nilify_blanks :except => [:author, :title]
28
+ end
29
+
30
+ == Specifying a Callback
31
+
32
+ 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.
33
+
34
+ class Post < ActiveRecord::Base
35
+ nilify_blanks :before => :create
36
+ end
37
+
38
+ class Post < ActiveRecord::Before
39
+ nilify_blanks :before => :validation_on_update
40
+ end
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc "Run specs"
7
+ Spec::Rake::SpecTask.new do |t|
8
+ t.spec_files = Rake::FileList["spec/**/*_spec.rb"]
9
+ t.spec_opts = ["-c"]
10
+ end
11
+
12
+ task :default => :spec
13
+
14
+ desc "Generate documentation for the plugin."
15
+ Rake::RDocTask.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = "rdoc"
17
+ rdoc.title = "nilify_blanks"
18
+ rdoc.options << "--line-numbers" << "--inline-source"
19
+ rdoc.rdoc_files.include('README')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "nilify_blanks"
2
+
3
+ NilifyBlanks::Railtie.insert
@@ -0,0 +1,48 @@
1
+ module NilifyBlanks
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def nilify_blanks(options = {})
8
+ return if self.included_modules.include?(NilifyBlanks::InstanceMethods)
9
+ return unless self.table_exists?
10
+
11
+ include NilifyBlanks::InstanceMethods
12
+
13
+ if options[:only]
14
+ options[:only] = [options[:only]] unless options[:only].is_a?(Array)
15
+ options[:only] = options[:only].map(&:to_s)
16
+ end
17
+
18
+ if options[:except]
19
+ options[:except] = [options[:except]] unless options[:except].is_a?(Array)
20
+ options[:except] = options[:except].map(&:to_s)
21
+ end
22
+
23
+ cattr_accessor :nilify_blanks_columns
24
+
25
+ self.nilify_blanks_columns = self.content_columns.reject {|c| !c.null }.map {|c| c.name.to_s }
26
+ self.nilify_blanks_columns = self.nilify_blanks_columns.select {|c| options[:only].include?(c) } if options[:only]
27
+ self.nilify_blanks_columns -= options[:except] if options[:except]
28
+ self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)
29
+
30
+ options[:before] ||= :save
31
+ class_eval "before_#{options[:before]} :nilify_blanks"
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+ def nilify_blanks
37
+ (self.nilify_blanks_columns || []).each do |column|
38
+ value = read_attribute(column)
39
+ next unless value.is_a?(String)
40
+ next unless value.nil? or !value.respond_to?(:blank)
41
+
42
+ write_attribute(column, nil) if value.blank?
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ require "nilify_blanks/railtie"
@@ -0,0 +1,19 @@
1
+ module NilifyBlanks
2
+ if defined?(Rails::Railtie)
3
+ require "rails"
4
+
5
+ class Railtie < Rails::Railtie
6
+ initializer "nilify_blanks.extend_active_record" do
7
+ ActiveSupport.on_load(:active_record) do
8
+ NilifyBlanks::Railtie.insert
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ class Railtie
15
+ def self.insert
16
+ ActiveRecord::Base.send(:include, NilifyBlanks)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module NilifyBlanks
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ sqlite:
2
+ adapter: sqlite
3
+ database: spec/db/test.sqlite
4
+
5
+ sqlite3:
6
+ adapter: sqlite3
7
+ database: spec/db/test.sqlite3
8
+
9
+ postgresql:
10
+ adapter: postgresql
11
+ username: postgres
12
+ password: postgres
13
+ database: nilify_blanks_plugin_test
14
+ min_messages: ERROR
15
+
16
+ mysql:
17
+ adapter: mysql
18
+ host: localhost
19
+ username: root
20
+ password:
21
+ database: nilify_blanks_plugin_test
data/spec/db/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
Binary file
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ describe NilifyBlanks do
4
+
5
+ context "Model with nilify_blanks" do
6
+ before(:all) do
7
+ class Post < ActiveRecord::Base
8
+ nilify_blanks
9
+ end
10
+
11
+ @post = Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
12
+ @post.save
13
+ end
14
+
15
+ it "should recognize all non-null columns" do
16
+ ['first_name', 'title', 'summary', 'body', 'views'].should == Post.nilify_blanks_columns
17
+ end
18
+
19
+ it "should convert all blanks to nils" do
20
+ @post.first_name.should be_nil
21
+ @post.title.should be_nil
22
+ @post.summary.should be_nil
23
+ @post.body.should be_nil
24
+ end
25
+
26
+ it "should leave not-null last name field alone" do
27
+ @post.last_name.should == ""
28
+ end
29
+
30
+ it "should leave integer views field alone" do
31
+ @post.views.should == 0
32
+ end
33
+ end
34
+
35
+ context "Model with nilify_blanks :only => [:first_name, :title]" do
36
+ before(:all) do
37
+ class PostOnlyFirstNameAndTitle < ActiveRecord::Base
38
+ set_table_name :posts
39
+ nilify_blanks :only => [:first_name, :title]
40
+ end
41
+
42
+ @post = PostOnlyFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
43
+ @post.save
44
+ end
45
+
46
+ it "should recognize only first_name and title" do
47
+ ['first_name', 'title'].should == PostOnlyFirstNameAndTitle.nilify_blanks_columns
48
+ end
49
+
50
+ it "should convert first_name and title blanks to nils" do
51
+ @post.first_name.should be_nil
52
+ @post.title.should be_nil
53
+ end
54
+
55
+ it "should leave other fields alone" do
56
+ @post.summary.should == ""
57
+ @post.body.should == ""
58
+ end
59
+ end
60
+
61
+ context "Model with nilify_blanks :except => [:first_name, :title]" do
62
+ before(:all) do
63
+ class PostExceptFirstNameAndTitle < ActiveRecord::Base
64
+ set_table_name :posts
65
+ nilify_blanks :except => [:first_name, :title]
66
+ end
67
+
68
+ @post = PostExceptFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
69
+ @post.save
70
+ end
71
+
72
+ it "should recognize only summary, body, and views" do
73
+ ['summary', 'body', 'views'].should == PostExceptFirstNameAndTitle.nilify_blanks_columns
74
+ end
75
+
76
+ it "should convert summary and body blanks to nils" do
77
+ @post.summary.should be_nil
78
+ @post.body.should be_nil
79
+ end
80
+
81
+ it "should leave other fields alone" do
82
+ @post.first_name.should == ""
83
+ @post.title.should == ""
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "nilify_blanks"
3
+ require "active_support"
4
+ require "active_record"
5
+
6
+ # Establish DB Connection
7
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
8
+ ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
9
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
10
+
11
+ # Load Test Schema into the Database
12
+ load(File.dirname(__FILE__) + "/db/schema.rb")
13
+
14
+ require File.dirname(__FILE__) + '/../init'
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nilify_blanks
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Ben Hughes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-14 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ version: 3.0.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: Often times you'll end up with empty strings where you really want nil at the database level. This plugin automatically converts blanks to nil and is configurable.
54
+ email: ben@railsgarden.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - lib/nilify_blanks/railtie.rb
63
+ - lib/nilify_blanks/version.rb
64
+ - lib/nilify_blanks.rb
65
+ - spec/db/database.yml
66
+ - spec/db/schema.rb
67
+ - spec/db/test.sqlite3
68
+ - spec/nilify_blanks_spec.rb
69
+ - spec/spec_helper.rb
70
+ - CHANGELOG.rdoc
71
+ - LICENSE
72
+ - Rakefile
73
+ - README.rdoc
74
+ - init.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/rubiety/nilify_blanks
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 19
99
+ segments:
100
+ - 1
101
+ - 3
102
+ - 4
103
+ version: 1.3.4
104
+ requirements: []
105
+
106
+ rubyforge_project: nilify_blanks
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Auto-convert blank fields to nil.
111
+ test_files: []
112
+