grosser-clear_empty_attributes 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +28 -0
- data/Rakefile +22 -0
- data/clear_empty_attributes.gemspec +34 -0
- data/init.rb +1 -0
- data/lib/clear_empty_attributes.rb +9 -0
- data/tasks/clear_empty_attributes.rake +17 -0
- metadata +74 -0
data/README.markdown
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Problem
|
2
|
+
=======
|
3
|
+
When AR objects are saved, empty fields are saved as '' instead of nil.
|
4
|
+
|
5
|
+
- Complicates queries for empty fields (`WHERE field IS NULL OR field = ''`)
|
6
|
+
- Makes the use of `unless field.blank?` necessary (opposed to only `if field`)
|
7
|
+
- Can lead to late-detected bugs because most of the time strings were `filled or ''` and suddenly they are `nil`
|
8
|
+
- Some validations do not support `:allow_blank=>true`
|
9
|
+
- Datebases can handle `NULL` better & faster than `''` (especially when using `LIKE`)
|
10
|
+
|
11
|
+
Solution
|
12
|
+
========
|
13
|
+
Defines a AR `before_validation` that sets empty Strings to nil.
|
14
|
+
|
15
|
+
Install
|
16
|
+
=======
|
17
|
+
script/plugin install git://github.com/grosser/clear_empty_attributes.git
|
18
|
+
OR
|
19
|
+
sudo gem install grosser-clear_empty_attributes
|
20
|
+
|
21
|
+
|
22
|
+
Migration
|
23
|
+
=========
|
24
|
+
When you are switching to `clear_empty_attributes`, run this task
|
25
|
+
to remove any `''` strings/texts from your database.
|
26
|
+
|
27
|
+
rake clear_empty_attributes:clear_all_blank_strings MODELS=User,Movie,...
|
28
|
+
(only works when checked out or installed as plugin)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'echoe'
|
3
|
+
|
4
|
+
porject_name = 'clear_empty_attributes'
|
5
|
+
|
6
|
+
Echoe.new(porject_name , '0.1') do |p|
|
7
|
+
p.description = "Save empty strings as nil to avoid lots of problems"
|
8
|
+
p.url = "http://github.com/grosser/#{porject_name}"
|
9
|
+
p.author = "Michael Grosser"
|
10
|
+
p.email = "grosser.michael@gmail.com"
|
11
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
12
|
+
p.dependencies = %w[activerecord]
|
13
|
+
p.development_dependencies = []
|
14
|
+
end
|
15
|
+
|
16
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
17
|
+
|
18
|
+
task :update_gemspec do
|
19
|
+
puts "updating..."
|
20
|
+
`rake manifest`
|
21
|
+
`rake build_gemspec`
|
22
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{clear_empty_attributes}
|
5
|
+
s.version = "0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Michael Grosser"]
|
9
|
+
s.date = %q{2009-01-02}
|
10
|
+
s.description = %q{Save empty strings as nil to avoid lots of problems}
|
11
|
+
s.email = %q{grosser.michael@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["lib/clear_empty_attributes.rb", "README.markdown", "tasks/clear_empty_attributes.rake"]
|
13
|
+
s.files = ["Manifest", "lib/clear_empty_attributes.rb", "init.rb", "Rakefile", "README.markdown", "tasks/clear_empty_attributes.rake", "clear_empty_attributes.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/grosser/clear_empty_attributes}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Clear_empty_attributes", "--main", "README.markdown"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{clear_empty_attributes}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Save empty strings as nil to avoid lots of problems}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'clear_empty_attributes'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
namespace :clear_empty_attributes do
|
2
|
+
desc "clear all blank strings of the given MODELS"
|
3
|
+
task :clear_all_blank_strings do
|
4
|
+
raise "rake clear_empty_attributes:clear_all_blank_strings MODELS=User,Movie,... to clear blank strings of those models" unless ENV['MODELS']
|
5
|
+
models = ENV['MODELS'].split(',').map(&:constantize)
|
6
|
+
|
7
|
+
models.each do |model|
|
8
|
+
puts "#{model}:"
|
9
|
+
model.columns.select(&:text?).each do |column|
|
10
|
+
name = column.name
|
11
|
+
puts " #{name}"
|
12
|
+
model.update_all("#{name} = NULL","#{name} = ''")
|
13
|
+
end
|
14
|
+
puts ''
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grosser-clear_empty_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Grosser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-02 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: Save empty strings as nil to avoid lots of problems
|
25
|
+
email: grosser.michael@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- lib/clear_empty_attributes.rb
|
32
|
+
- README.markdown
|
33
|
+
- tasks/clear_empty_attributes.rake
|
34
|
+
files:
|
35
|
+
- Manifest
|
36
|
+
- lib/clear_empty_attributes.rb
|
37
|
+
- init.rb
|
38
|
+
- Rakefile
|
39
|
+
- README.markdown
|
40
|
+
- tasks/clear_empty_attributes.rake
|
41
|
+
- clear_empty_attributes.gemspec
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/grosser/clear_empty_attributes
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --line-numbers
|
47
|
+
- --inline-source
|
48
|
+
- --title
|
49
|
+
- Clear_empty_attributes
|
50
|
+
- --main
|
51
|
+
- README.markdown
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "1.2"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: clear_empty_attributes
|
69
|
+
rubygems_version: 1.2.0
|
70
|
+
signing_key:
|
71
|
+
specification_version: 2
|
72
|
+
summary: Save empty strings as nil to avoid lots of problems
|
73
|
+
test_files: []
|
74
|
+
|