nullify 0.2.1
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/.gitignore +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +39 -0
- data/Rakefile +40 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/nullify.rb +36 -0
- data/nullify.gemspec +52 -0
- data/tasks/nullify_tasks.rake +26 -0
- data/test/nullify_test.rb +86 -0
- data/test/test_helper.rb +3 -0
- metadata +67 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Steve Richert
|
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
|
+
= nullify
|
2
|
+
|
3
|
+
<tt>nullify</tt> makes it easy to clean up blank attributes that result from creating ActiveRecord objects directly from form params. You may specify any number of attributes (including non-column attributes) to nullify.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
script/plugin install git://github.com/laserlemon/nullify.git
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
In your model:
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
validates_presence_of :first_name, :last_name
|
15
|
+
nullify :middle_name
|
16
|
+
|
17
|
+
def name
|
18
|
+
[first_name, middle_name, last_name].compact.join(' ')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
In your "new" view:
|
23
|
+
|
24
|
+
<% form_for @user do |f| %>
|
25
|
+
<%= f.text_field :first_name %>
|
26
|
+
<%= f.text_field :middle_name %>
|
27
|
+
<%= f.text_field :last_name %>
|
28
|
+
<%= submit_tag 'Save' %>
|
29
|
+
<% end %>
|
30
|
+
|
31
|
+
In your controller:
|
32
|
+
|
33
|
+
def create
|
34
|
+
@user = User.create(params[:user])
|
35
|
+
end
|
36
|
+
|
37
|
+
In your "show" view:
|
38
|
+
|
39
|
+
<%=h @user.name %>
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |g|
|
9
|
+
g.name = 'nullify'
|
10
|
+
g.summary = %(Nuke blank values in ActiveRecord models)
|
11
|
+
g.description = %(Nuke blank values in ActiveRecord models)
|
12
|
+
g.email = 'steve@laserlemon.com'
|
13
|
+
g.homepage = 'http://github.com/laserlemon/nullify'
|
14
|
+
g.authors = %w(laserlemon)
|
15
|
+
g.rubyforge_project = 'laser-lemon'
|
16
|
+
end
|
17
|
+
Jeweler::RubyforgeTasks.new do |r|
|
18
|
+
r.doc_task = 'rdoc'
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::TestTask.new do |t|
|
26
|
+
t.libs = %w(test)
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :test
|
31
|
+
|
32
|
+
Rake::RDocTask.new do |r|
|
33
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : nil
|
34
|
+
r.rdoc_dir = 'rdoc'
|
35
|
+
r.title = ['nullify', version].compact.join(' ')
|
36
|
+
r.rdoc_files.include('README*')
|
37
|
+
r.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
39
|
+
|
40
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks', '*.rake')].sort.each{|t| load t }
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'nullify'
|
data/lib/nullify.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module LaserLemon
|
2
|
+
module Nullify
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def nullify(*columns)
|
9
|
+
write_inheritable_array :nullified_columns, columns.map(&:to_sym)
|
10
|
+
class_inheritable_reader :nullified_columns
|
11
|
+
|
12
|
+
columns.each do |column|
|
13
|
+
define_method "#{column}=" do |value|
|
14
|
+
write_attribute(column, value.blank? ? nil : value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
include InstanceMethods
|
19
|
+
|
20
|
+
before_validation :nullify_columns
|
21
|
+
before_save :nullify_columns
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def nullify_columns
|
27
|
+
self.class.nullified_columns.each do |column|
|
28
|
+
value = read_attribute(column)
|
29
|
+
write_attribute(column, nil) if !value.nil? && value.blank?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
ActiveRecord::Base.send(:include, LaserLemon::Nullify)
|
data/nullify.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{nullify}
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["laserlemon"]
|
12
|
+
s.date = %q{2009-10-07}
|
13
|
+
s.description = %q{Nuke blank values in ActiveRecord models}
|
14
|
+
s.email = %q{steve@laserlemon.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/nullify.rb",
|
27
|
+
"nullify.gemspec",
|
28
|
+
"tasks/nullify_tasks.rake",
|
29
|
+
"test/nullify_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/laserlemon/nullify}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubyforge_project = %q{laser-lemon}
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Nuke blank values in ActiveRecord models}
|
38
|
+
s.test_files = [
|
39
|
+
"test/nullify_test.rb",
|
40
|
+
"test/test_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
else
|
49
|
+
end
|
50
|
+
else
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
def get_class
|
2
|
+
klass = ENV['CLASS'] || ENV['class']
|
3
|
+
raise 'Must specify CLASS' unless klass
|
4
|
+
@class = Object.const_get(klass)
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_columns
|
8
|
+
raise "Class #{@class.name} has no nullified columns" unless @class.respond_to?(:nullified_columns)
|
9
|
+
@columns = if column = ENV['COLUMN'] || ENV['column']
|
10
|
+
raise "Class #{@class.name} has no nullified column #{column}" unless @class.nullified_columns.include?(column.to_sym)
|
11
|
+
[column.to_sym]
|
12
|
+
else @class.nullified_columns
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Sets blank columns to nil for a given CLASS (and optional COLUMN)'
|
17
|
+
task :nullify => :environment do
|
18
|
+
get_class and get_columns
|
19
|
+
@class.all.each do |instance|
|
20
|
+
@columns.each do |column|
|
21
|
+
value = instance[column]
|
22
|
+
instance[column] = nil if !value.nil? && value.blank?
|
23
|
+
end
|
24
|
+
instance.save
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
|
8
|
+
|
9
|
+
def setup_db
|
10
|
+
ActiveRecord::Schema.define(:version => 1) do
|
11
|
+
create_table :addresses do |t|
|
12
|
+
t.string :street_1, :null => false
|
13
|
+
t.string :street_2, :null => true
|
14
|
+
t.string :city, :null => false
|
15
|
+
t.string :state, :null => false
|
16
|
+
t.string :zip, :null => false
|
17
|
+
t.string :zip_4, :null => true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_db
|
23
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
24
|
+
ActiveRecord::Base.connection.drop_table(table)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Address < ActiveRecord::Base
|
29
|
+
nullify :street_2, :zip_4
|
30
|
+
|
31
|
+
validates_presence_of :street_1, :city, :zip
|
32
|
+
validates_format_of :zip, :with => /^\d{5}$/, :allow_nil => false
|
33
|
+
validates_format_of :zip_4, :with => /^\d{4}/, :allow_nil => true
|
34
|
+
end
|
35
|
+
|
36
|
+
class AddressTest < Test::Unit::TestCase
|
37
|
+
def setup
|
38
|
+
setup_db
|
39
|
+
Address.create!(
|
40
|
+
:street_1 => 'Department of the Teasury',
|
41
|
+
:street_2 => 'Internal Revenue Service',
|
42
|
+
:city => 'Kansas City',
|
43
|
+
:state => 'Missouri',
|
44
|
+
:zip => '64999',
|
45
|
+
:zip_4 => '0102'
|
46
|
+
)
|
47
|
+
Address.create!(
|
48
|
+
:street_1 => '1 Infinite Loop',
|
49
|
+
:city => 'Cupertino',
|
50
|
+
:state => 'California',
|
51
|
+
:zip => '95014'
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def irs
|
56
|
+
Address.first
|
57
|
+
end
|
58
|
+
|
59
|
+
def apple
|
60
|
+
Address.last
|
61
|
+
end
|
62
|
+
|
63
|
+
def teardown
|
64
|
+
teardown_db
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_no_nullification_needed
|
68
|
+
before = irs.attributes
|
69
|
+
irs.save
|
70
|
+
after = irs.attributes
|
71
|
+
assert_equal(before, after)
|
72
|
+
before = apple.attributes
|
73
|
+
apple.save
|
74
|
+
after = apple.attributes
|
75
|
+
assert_equal(before, after)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_nullification_needed
|
79
|
+
assert(irs.update_attributes(:zip_4 => ''))
|
80
|
+
assert_nil(irs.zip_4)
|
81
|
+
assert(irs.update_attributes(:zip_4 => ' '))
|
82
|
+
assert_nil(irs.zip_4)
|
83
|
+
assert(irs.update_attributes(:zip_4 => '1939'))
|
84
|
+
assert_equal('1939', irs.zip_4)
|
85
|
+
end
|
86
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nullify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- laserlemon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-07 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Nuke blank values in ActiveRecord models
|
17
|
+
email: steve@laserlemon.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- init.rb
|
32
|
+
- lib/nullify.rb
|
33
|
+
- nullify.gemspec
|
34
|
+
- tasks/nullify_tasks.rake
|
35
|
+
- test/nullify_test.rb
|
36
|
+
- test/test_helper.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/laserlemon/nullify
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: laser-lemon
|
61
|
+
rubygems_version: 1.3.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Nuke blank values in ActiveRecord models
|
65
|
+
test_files:
|
66
|
+
- test/nullify_test.rb
|
67
|
+
- test/test_helper.rb
|