nilly_vanilly 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/README.markdown +51 -0
- data/Rakefile +63 -0
- data/VERSION +1 -0
- data/lib/nilly_vanilly.rb +6 -0
- data/lib/nilly_vanilly/nillify.rb +28 -0
- data/lib/tasks/nilly_vanilly.rake +21 -0
- data/rails/init.rb +1 -0
- data/spec/database.yml +17 -0
- data/spec/models.rb +7 -0
- data/spec/nilly_vanilly_spec.rb +20 -0
- data/spec/schema.rb +10 -0
- data/spec/spec_helper.rb +32 -0
- metadata +127 -0
data/README.markdown
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
NillyVanilly
|
2
|
+
============
|
3
|
+
|
4
|
+
This plugin stores NULL in your database when you try to store an empty string.
|
5
|
+
|
6
|
+
It only works for columns you explicitly mention inside your model. It comes
|
7
|
+
with a rake task which prints out all the columns eligible for nillification.
|
8
|
+
|
9
|
+
Since HTTP parameters are always strings, an empty string is sent by form
|
10
|
+
values for which you did not mean to enter any value. This plugin makes sure
|
11
|
+
to save NULL in database varchar columns that are defined with DEFAULT NULL.
|
12
|
+
|
13
|
+
|
14
|
+
Example
|
15
|
+
=======
|
16
|
+
|
17
|
+
class Comment < ActiveRecord::Base
|
18
|
+
nillify :author_url
|
19
|
+
end
|
20
|
+
|
21
|
+
comment = Comment.create :author_url => ""
|
22
|
+
comment.author_url # -> nil
|
23
|
+
|
24
|
+
|
25
|
+
Installation
|
26
|
+
============
|
27
|
+
|
28
|
+
./script/plugin install git://github.com/tilsammans/nilly_vanilly.git
|
29
|
+
|
30
|
+
|
31
|
+
Inspection
|
32
|
+
==========
|
33
|
+
|
34
|
+
To see what columns are eligible for nillification, run the following:
|
35
|
+
|
36
|
+
rake nilly:vanilly:inspect
|
37
|
+
|
38
|
+
This will print a list of all tables in your database, cross-referenced with
|
39
|
+
the models in your application. All text columns which may be NULL and are
|
40
|
+
nil by default are shown, i.e. all these columns should be safe to nillify.
|
41
|
+
This does not take into account any validations you might have.
|
42
|
+
|
43
|
+
When a column has already been nillified, it will be indicated with [OK].
|
44
|
+
|
45
|
+
|
46
|
+
Author
|
47
|
+
======
|
48
|
+
|
49
|
+
Joost Baaij
|
50
|
+
<joost@spacebabies.nl>
|
51
|
+
[www.spacebabies.nl](http://www.spacebabies.nl/)
|
data/Rakefile
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
desc 'Default: run specs.'
|
5
|
+
task :default => :spec
|
6
|
+
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "nilly_vanilly"
|
10
|
+
gem.homepage = "http://github.com/tilsammans/nilly_vanilly"
|
11
|
+
gem.summary = %Q{This plugin stores NULL in your database when you try to store an empty string.}
|
12
|
+
gem.email = "joost@spacebabies.nl"
|
13
|
+
gem.authors = ["Joost Baaij"]
|
14
|
+
gem.add_runtime_dependency 'activerecord', '~> 2.3'
|
15
|
+
gem.add_development_dependency 'rspec', '~> 1.3.1'
|
16
|
+
gem.add_development_dependency 'jeweler', '~> 0.5.2'
|
17
|
+
end
|
18
|
+
Jeweler::RubygemsDotOrgTasks.new
|
19
|
+
|
20
|
+
begin
|
21
|
+
# Rspec 1.3.0
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
|
24
|
+
desc 'Default: run specs'
|
25
|
+
task :default => :spec
|
26
|
+
Spec::Rake::SpecTask.new do |t|
|
27
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
31
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
32
|
+
t.rcov = true
|
33
|
+
t.rcov_opts = ['--exclude', 'spec']
|
34
|
+
end
|
35
|
+
|
36
|
+
rescue LoadError
|
37
|
+
# Rspec 2.0
|
38
|
+
require 'rspec/core/rake_task'
|
39
|
+
|
40
|
+
desc 'Default: run specs'
|
41
|
+
task :default => :spec
|
42
|
+
Rspec::Core::RakeTask.new do |t|
|
43
|
+
t.pattern = "spec/**/*_spec.rb"
|
44
|
+
end
|
45
|
+
|
46
|
+
Rspec::Core::RakeTask.new('rcov') do |t|
|
47
|
+
t.pattern = "spec/**/*_spec.rb"
|
48
|
+
t.rcov = true
|
49
|
+
t.rcov_opts = ['--exclude', 'spec']
|
50
|
+
end
|
51
|
+
|
52
|
+
rescue LoadError
|
53
|
+
puts "Rspec not available. Install it with: gem install rspec"
|
54
|
+
end
|
55
|
+
|
56
|
+
desc 'Generate documentation for the nilly_vanilly plugin.'
|
57
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
58
|
+
rdoc.rdoc_dir = 'rdoc'
|
59
|
+
rdoc.title = 'NillyVanilly'
|
60
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
61
|
+
rdoc.rdoc_files.include('README')
|
62
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
63
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module NillyVanilly
|
2
|
+
module Nillify
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def nillify(*attributes)
|
9
|
+
class_eval do
|
10
|
+
before_save :nillification
|
11
|
+
end
|
12
|
+
|
13
|
+
cattr_accessor :nillify_attributes
|
14
|
+
self.nillify_attributes = attributes
|
15
|
+
|
16
|
+
include InstanceMethods
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
def nillification
|
22
|
+
for attribute in self.class.nillify_attributes
|
23
|
+
self.send("#{attribute}=", nil) if self.send(attribute) == ""
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
namespace :nilly do
|
2
|
+
namespace :vanilly do
|
3
|
+
task :inspect => :environment do
|
4
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
5
|
+
model = table.classify.constantize rescue next
|
6
|
+
|
7
|
+
model.columns.each do |column|
|
8
|
+
if model.respond_to?(:nillify_attributes) && model.nillify_attributes.include?(column.name.to_sym)
|
9
|
+
present = "[OK]"
|
10
|
+
else
|
11
|
+
present = "[ ]"
|
12
|
+
end
|
13
|
+
|
14
|
+
if column.text? && column.null && column.default.nil?
|
15
|
+
puts [present, model.name.ljust(20), column.name].join(" ")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'nilly_vanilly'
|
data/spec/database.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: nilly_vanilly.sqlite3
|
4
|
+
|
5
|
+
mysql:
|
6
|
+
adapter: mysql
|
7
|
+
hostname: localhost
|
8
|
+
username: root
|
9
|
+
password:
|
10
|
+
database: nilly_vanilly
|
11
|
+
|
12
|
+
postgresql:
|
13
|
+
adapter: postgresql
|
14
|
+
hostname: localhost
|
15
|
+
username: postgres
|
16
|
+
password:
|
17
|
+
database: nilly_vanilly
|
data/spec/models.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NillyVanilly do
|
4
|
+
describe "model with one column" do
|
5
|
+
it "stores class methods" do
|
6
|
+
Post.nillify_attributes.should == [:title]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "stores nil for empty string" do
|
10
|
+
post = Post.create! :title => ""
|
11
|
+
post.title.should be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "model with two columns" do
|
16
|
+
it "stores class methods" do
|
17
|
+
Comment.nillify_attributes.should == [:author, :author_email]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :posts, :force => true do |t|
|
3
|
+
t.string :title, :default => nil, :null => true
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :comments, :force => true do |t|
|
7
|
+
t.string :author, :default => nil, :null => true
|
8
|
+
t.string :author_email, :default => nil, :null => true
|
9
|
+
end
|
10
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH << "." unless $LOAD_PATH.include?(".")
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require File.dirname(__FILE__) + '/../rails/init.rb'
|
5
|
+
|
6
|
+
ENV['DB'] ||= 'sqlite3'
|
7
|
+
|
8
|
+
database_yml = File.expand_path('../database.yml', __FILE__)
|
9
|
+
if File.exists?(database_yml)
|
10
|
+
active_record_configuration = YAML.load_file(database_yml)[ENV['DB']]
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(active_record_configuration)
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
14
|
+
|
15
|
+
ActiveRecord::Base.silence do
|
16
|
+
ActiveRecord::Migration.verbose = false
|
17
|
+
|
18
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
19
|
+
load(File.dirname(__FILE__) + '/models.rb')
|
20
|
+
end
|
21
|
+
|
22
|
+
else
|
23
|
+
raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
|
24
|
+
end
|
25
|
+
|
26
|
+
def clean_database!
|
27
|
+
[Post, Comment].each do |model|
|
28
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
clean_database!
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nilly_vanilly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Joost Baaij
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-21 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 3
|
33
|
+
version: "2.3"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 25
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 3
|
48
|
+
- 1
|
49
|
+
version: 1.3.1
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: jeweler
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 15
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
- 5
|
64
|
+
- 2
|
65
|
+
version: 0.5.2
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id003
|
68
|
+
description:
|
69
|
+
email: joost@spacebabies.nl
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files:
|
75
|
+
- README.markdown
|
76
|
+
files:
|
77
|
+
- README.markdown
|
78
|
+
- Rakefile
|
79
|
+
- VERSION
|
80
|
+
- lib/nilly_vanilly.rb
|
81
|
+
- lib/nilly_vanilly/nillify.rb
|
82
|
+
- lib/tasks/nilly_vanilly.rake
|
83
|
+
- rails/init.rb
|
84
|
+
- spec/database.yml
|
85
|
+
- spec/models.rb
|
86
|
+
- spec/nilly_vanilly_spec.rb
|
87
|
+
- spec/schema.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
has_rdoc: true
|
90
|
+
homepage: http://github.com/tilsammans/nilly_vanilly
|
91
|
+
licenses: []
|
92
|
+
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
version: "0"
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.3.7
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: This plugin stores NULL in your database when you try to store an empty string.
|
123
|
+
test_files:
|
124
|
+
- spec/models.rb
|
125
|
+
- spec/nilly_vanilly_spec.rb
|
126
|
+
- spec/schema.rb
|
127
|
+
- spec/spec_helper.rb
|