Skip to content

Commit

Permalink
Updated to Rails 3, converted to RSpec, gemified.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiety committed Oct 14, 2010
1 parent 9944c89 commit fa14a94
Show file tree
Hide file tree
Showing 22 changed files with 248 additions and 123 deletions.
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--colour
--format nested
--backtrace
2 changes: 0 additions & 2 deletions CHANGELOG

This file was deleted.

9 changes: 9 additions & 0 deletions CHANGELOG.rdoc
@@ -0,0 +1,9 @@
== [2010-10-15] 1.0.0 Major Updates

* Finally updated to be a Gem
* Internal refactoring
* Converted tests to RSpec
* Added support for Rails 3.0

== 0.1.2 Fixed issue with table not existing.
== 0.1.0 Initial Version
File renamed without changes.
17 changes: 0 additions & 17 deletions Manifest

This file was deleted.

5 changes: 3 additions & 2 deletions README.rdoc
Expand Up @@ -6,8 +6,9 @@ Only attributes responding to empty? with a value of true will be converted to n

== Install

# In environment.rb
config.gem 'railsgarden-nilify_blanks', :lib => 'nilify_blanks', :source => 'http://gems.github.com'
Include the gem using bundler in your Gemfile:

gem "nilify_blanks"

== Basic Examples

Expand Down
37 changes: 12 additions & 25 deletions Rakefile
@@ -1,36 +1,23 @@
require 'rubygems'
require 'echoe'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'spec/rake/spectask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the nilify_blanks plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
desc "Run specs"
Spec::Rake::SpecTask.new do |t|
t.spec_files = Rake::FileList["spec/**/*_spec.rb"]
t.spec_opts = ["-c"]
end

desc 'Generate documentation for the nilify_blanks plugin.'
task :default => :spec

desc "Generate documentation for the plugin."
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'NilifyBlanks'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_dir = "rdoc"
rdoc.title = "nilify_blanks"
rdoc.options << "--line-numbers" << "--inline-source"
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

Echoe.new('nilify_blanks', '0.1.2') do |p|
p.description = "Auto-convert blank fields to nil."
p.url = "http://github.com/railsgarden/nilify_blanks"
p.author = "Ben Hughes"
p.email = "ben@railsgarden.com"
p.ignore_pattern = ["tmp/*", "script/*"]
p.development_dependencies = []
end

Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }

Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
4 changes: 3 additions & 1 deletion init.rb
@@ -1 +1,3 @@
require 'nilify_blanks'
require "nilify_blanks"

NilifyBlanks::Railtie.insert
1 change: 0 additions & 1 deletion install.rb

This file was deleted.

84 changes: 41 additions & 43 deletions lib/nilify_blanks.rb
@@ -1,50 +1,48 @@
module Rubiety
module NilifyBlanks
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def nilify_blanks(options = {})
return if self.included_modules.include?(NilifyBlanks::InstanceMethods)
return unless self.table_exists?

include NilifyBlanks::InstanceMethods

if options[:only]
options[:only] = [options[:only]] unless options[:only].is_a?(Array)
options[:only] = options[:only].map(&:to_s)
end

if options[:except]
options[:except] = [options[:except]] unless options[:except].is_a?(Array)
options[:except] = options[:except].map(&:to_s)
end

cattr_accessor :nilify_blanks_columns

self.nilify_blanks_columns = self.content_columns.reject {|c| !c.null }.map {|c| c.name.to_s }
self.nilify_blanks_columns = self.nilify_blanks_columns.select {|c| options[:only].include?(c) } if options[:only]
self.nilify_blanks_columns -= options[:except] if options[:except]
self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)

options[:before] ||= :save
class_eval "before_#{options[:before]} :nilify_blanks"
module NilifyBlanks
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def nilify_blanks(options = {})
return if self.included_modules.include?(NilifyBlanks::InstanceMethods)
return unless self.table_exists?

include NilifyBlanks::InstanceMethods

if options[:only]
options[:only] = [options[:only]] unless options[:only].is_a?(Array)
options[:only] = options[:only].map(&:to_s)
end

if options[:except]
options[:except] = [options[:except]] unless options[:except].is_a?(Array)
options[:except] = options[:except].map(&:to_s)
end

cattr_accessor :nilify_blanks_columns

self.nilify_blanks_columns = self.content_columns.reject {|c| !c.null }.map {|c| c.name.to_s }
self.nilify_blanks_columns = self.nilify_blanks_columns.select {|c| options[:only].include?(c) } if options[:only]
self.nilify_blanks_columns -= options[:except] if options[:except]
self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)

options[:before] ||= :save
class_eval "before_#{options[:before]} :nilify_blanks"
end
module InstanceMethods
def nilify_blanks
(self.nilify_blanks_columns || []).each do |column|
value = read_attribute(column)
next unless value.is_a?(String)
next unless value.nil? or !value.respond_to?(:blank)
write_attribute(column, nil) if value.blank?
end
end

module InstanceMethods
def nilify_blanks
(self.nilify_blanks_columns || []).each do |column|
value = read_attribute(column)
next unless value.is_a?(String)
next unless value.nil? or !value.respond_to?(:blank)

write_attribute(column, nil) if value.blank?
end
end
end
end

ActiveRecord::Base.send(:include, Rubiety::NilifyBlanks)
require "nilify_blanks/railtie"
19 changes: 19 additions & 0 deletions lib/nilify_blanks/railtie.rb
@@ -0,0 +1,19 @@
module NilifyBlanks
if defined?(Rails::Railtie)
require "rails"

class Railtie < Rails::Railtie
initializer "nilify_blanks.extend_active_record" do
ActiveSupport.on_load(:active_record) do
NilifyBlanks::Railtie.insert
end
end
end
end

class Railtie
def self.insert
ActiveRecord::Base.send(:include, NilifyBlanks)
end
end
end
3 changes: 3 additions & 0 deletions lib/nilify_blanks/version.rb
@@ -0,0 +1,3 @@
module NilifyBlanks
VERSION = "1.0.0"
end
45 changes: 18 additions & 27 deletions nilify_blanks.gemspec
@@ -1,31 +1,22 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "nilify_blanks/version"

Gem::Specification.new do |s|
s.name = %q{nilify_blanks}
s.version = "0.1.2"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Ben Hughes"]
s.date = %q{2009-09-10}
s.description = %q{Auto-convert blank fields to nil.}
s.email = %q{ben@railsgarden.com}
s.extra_rdoc_files = ["CHANGELOG", "lib/nilify_blanks.rb", "README.rdoc", "tasks/nilify_blanks_tasks.rake"]
s.files = ["CHANGELOG", "init.rb", "install.rb", "lib/nilify_blanks.rb", "Manifest", "MIT-LICENSE", "nilify_blanks.gemspec", "nilify_blanks_plugin.sqlite3", "Rakefile", "README.rdoc", "tasks/nilify_blanks_tasks.rake", "test/config/database.yml", "test/nilify_blanks_plugin.sqlite3", "test/nilify_blanks_test.rb", "test/schema.rb", "test/test_helper.rb", "uninstall.rb"]
s.homepage = %q{http://github.com/railsgarden/nilify_blanks}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nilify_blanks", "--main", "README.rdoc"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{nilify_blanks}
s.rubygems_version = %q{1.3.4}
s.summary = %q{Auto-convert blank fields to nil.}
s.test_files = ["test/nilify_blanks_test.rb", "test/test_helper.rb"]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
s.name = "nilify_blanks"
s.version = "1.0.0"
s.author = "Ben Hughes"
s.email = "ben@railsgarden.com"
s.homepage = "http://github.com/rubiety/nilify_blanks"
s.summary = "Auto-convert blank fields to nil."
s.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."

s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"]
s.require_path = "lib"

s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4"

s.add_dependency("activesupport", [">= 3.0.0"])
s.add_dependency("activerecord", [">= 3.0.0"])
end
3 changes: 3 additions & 0 deletions rails/init.rb
@@ -0,0 +1,3 @@
require "nilify_blanks"

NilifyBlanks::Railtie.insert
21 changes: 21 additions & 0 deletions spec/db/database.yml
@@ -0,0 +1,21 @@
sqlite:
adapter: sqlite
database: spec/db/test.sqlite

sqlite3:
adapter: sqlite3
database: spec/db/test.sqlite3

postgresql:
adapter: postgresql
username: postgres
password: postgres
database: nilify_blanks_plugin_test
min_messages: ERROR

mysql:
adapter: mysql
host: localhost
username: root
password:
database: nilify_blanks_plugin_test
12 changes: 12 additions & 0 deletions spec/db/schema.rb
@@ -0,0 +1,12 @@
ActiveRecord::Schema.define(:version => 0) do

create_table :posts, :force => true do |t|
t.string :first_name
t.string :last_name, :null => false
t.string :title
t.text :summary
t.text :body
t.integer :views
end

end
Binary file not shown.

0 comments on commit fa14a94

Please sign in to comment.