laserlemon-nullify 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/MIT-LICENSE +20 -0
- data/Manifest +11 -0
- data/README.rdoc +45 -0
- data/Rakefile +14 -0
- data/VERSION.yml +4 -0
- data/init.rb +1 -0
- data/lib/nullify.rb +27 -0
- data/nullify.gemspec +32 -0
- data/tasks/nullify_tasks.rake +26 -0
- data/test/nullify_test.rb +89 -0
- data/test/test_helper.rb +3 -0
- metadata +71 -0
data/MIT-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/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
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 %>
|
40
|
+
|
41
|
+
== Tips
|
42
|
+
|
43
|
+
* Performs its nullification before validation, allowing for easy use of +allow_nil+ validation options
|
44
|
+
* Uses ActiveRecord's dynamic getters and setters, meaning it will also work for your own custom, non-column getters/setters
|
45
|
+
* Works for non-string attributes that respond to <tt>blank?</tt>
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('nullify', '0.1.0') do |g|
|
6
|
+
g.description = %(Nuke blank values in ActiveRecord models)
|
7
|
+
g.url = 'http://github.com/laserlemon/nullify'
|
8
|
+
g.author = 'Steve Richert'
|
9
|
+
g.email = 'steve@laserlemon.com'
|
10
|
+
g.ignore_pattern = %w(tmp/* script/*)
|
11
|
+
g.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|t| load t }
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'nullify'
|
data/lib/nullify.rb
ADDED
@@ -0,0 +1,27 @@
|
|
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
|
+
include InstanceMethods
|
12
|
+
before_validation :nullify_columns
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
def nullify_columns
|
18
|
+
self.class.nullified_columns.each do |column|
|
19
|
+
value = send(column)
|
20
|
+
send("#{column}=", nil) if value.blank? && !value.nil?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Base.send(:include, LaserLemon::Nullify)
|
data/nullify.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{nullify}
|
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 = ["Steve Richert"]
|
9
|
+
s.date = %q{2009-04-28}
|
10
|
+
s.description = %q{Nuke blank values in ActiveRecord models}
|
11
|
+
s.email = %q{steve@laserlemon.com}
|
12
|
+
s.extra_rdoc_files = ["lib/nullify.rb", "README.rdoc", "tasks/nullify_tasks.rake"]
|
13
|
+
s.files = ["init.rb", "lib/nullify.rb", "Manifest", "MIT-LICENSE", "nullify.gemspec", "Rakefile", "README.rdoc", "tasks/nullify_tasks.rake", "test/nullify_test.rb", "test/test_helper.rb", "VERSION.yml"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/laserlemon/nullify}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nullify", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{nullify}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Nuke blank values in ActiveRecord models}
|
21
|
+
s.test_files = ["test/nullify_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,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.send(column)
|
22
|
+
instance.send("#{column}=", nil) if value.blank? && !value.nil?
|
23
|
+
end
|
24
|
+
instance.save
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
require "#{File.dirname(__FILE__)}/../init"
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
9
|
+
|
10
|
+
def setup_db
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
create_table :addresses do |t|
|
13
|
+
t.string :street_1, :null => false
|
14
|
+
t.string :street_2, :null => true
|
15
|
+
t.string :city, :null => false
|
16
|
+
t.string :state, :null => false
|
17
|
+
t.string :zip, :null => false
|
18
|
+
t.string :zip_4, :null => true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown_db
|
24
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
25
|
+
ActiveRecord::Base.connection.drop_table(table)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Address < ActiveRecord::Base
|
30
|
+
|
31
|
+
nullify :street_2, :zip_4
|
32
|
+
|
33
|
+
validates_presence_of :street_1, :city, :zip
|
34
|
+
validates_format_of :zip, :with => /^\d{5}$/, :allow_nil => false
|
35
|
+
validates_format_of :zip_4, :with => /^\d{4}/, :allow_nil => true
|
36
|
+
end
|
37
|
+
|
38
|
+
class AddressTest < Test::Unit::TestCase
|
39
|
+
|
40
|
+
def setup
|
41
|
+
setup_db
|
42
|
+
Address.create!(
|
43
|
+
:street_1 => 'Department of the Teasury',
|
44
|
+
:street_2 => 'Internal Revenue Service',
|
45
|
+
:city => 'Kansas City',
|
46
|
+
:state => 'Missouri',
|
47
|
+
:zip => '64999',
|
48
|
+
:zip_4 => '0102'
|
49
|
+
)
|
50
|
+
Address.create!(
|
51
|
+
:street_1 => '1 Infinite Loop',
|
52
|
+
:city => 'Cupertino',
|
53
|
+
:state => 'California',
|
54
|
+
:zip => '95014'
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
def irs
|
59
|
+
Address.first
|
60
|
+
end
|
61
|
+
|
62
|
+
def apple
|
63
|
+
Address.last
|
64
|
+
end
|
65
|
+
|
66
|
+
def teardown
|
67
|
+
teardown_db
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_no_nullification_needed
|
71
|
+
before = irs.attributes
|
72
|
+
irs.save
|
73
|
+
after = irs.attributes
|
74
|
+
assert_equal(before, after)
|
75
|
+
before = apple.attributes
|
76
|
+
apple.save
|
77
|
+
after = apple.attributes
|
78
|
+
assert_equal(before, after)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_nullification_needed
|
82
|
+
assert(irs.update_attributes(:zip_4 => ''))
|
83
|
+
assert_nil(irs.zip_4)
|
84
|
+
assert(irs.update_attributes(:zip_4 => ' '))
|
85
|
+
assert_nil(irs.zip_4)
|
86
|
+
assert(irs.update_attributes(:zip_4 => '1939'))
|
87
|
+
assert_equal('1939', irs.zip_4)
|
88
|
+
end
|
89
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: laserlemon-nullify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Richert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-28 00:00:00 -07: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
|
+
- lib/nullify.rb
|
24
|
+
- README.rdoc
|
25
|
+
- tasks/nullify_tasks.rake
|
26
|
+
files:
|
27
|
+
- init.rb
|
28
|
+
- lib/nullify.rb
|
29
|
+
- Manifest
|
30
|
+
- MIT-LICENSE
|
31
|
+
- nullify.gemspec
|
32
|
+
- Rakefile
|
33
|
+
- README.rdoc
|
34
|
+
- tasks/nullify_tasks.rake
|
35
|
+
- test/nullify_test.rb
|
36
|
+
- test/test_helper.rb
|
37
|
+
- VERSION.yml
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/laserlemon/nullify
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --line-numbers
|
43
|
+
- --inline-source
|
44
|
+
- --title
|
45
|
+
- Nullify
|
46
|
+
- --main
|
47
|
+
- README.rdoc
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "1.2"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: nullify
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Nuke blank values in ActiveRecord models
|
69
|
+
test_files:
|
70
|
+
- test/nullify_test.rb
|
71
|
+
- test/test_helper.rb
|