nilify_blanks 1.3.0 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 406a558d296d89cadd2f2e3a1b73ca9730757571
4
- data.tar.gz: c7df79461bf71561220e6f2820365e23f6a859a3
2
+ SHA256:
3
+ metadata.gz: 3182bde0ba670c955ffe32072da1d25ab4ac555ee6407ba0cb382176f180c3e5
4
+ data.tar.gz: 651f7c83d16b982e06fd10e942794c3e2692e3aa308ff3e601d785e3402b2b31
5
5
  SHA512:
6
- metadata.gz: 43f5d7c175b34385ae119e513cdc0915d0bb1357d9cdd6ddf7a54659bcbac59d30c36069d8aa88f3104ca260e097c2df8191e75ce998255811dcf891aeef7841
7
- data.tar.gz: 49303614f425476f5621ac72d2ef7b94fc25b2480de1f460b610aafc366ec146808437514b2abd27b98c616204f49f4076f43eb243f24715843d2b25bbc5ce9a
6
+ metadata.gz: d5402f67309e91fc294883f5c652d640de783eb034f2a37fcbb5f2cc85f1b820c0123ed0dac3c95ffcdb940129391497c36dfcb5e1ffa6ba69ad7e199a3e6514
7
+ data.tar.gz: 79d4faa8cfe40b20c01b6d41f35c1241bd3c9bccd57da6273933c31cb014dc527446a013d2e3d96e076e5284c83f0dc40623cf4fe005656fb9278dc992e0c6f5
@@ -1,44 +1,66 @@
1
- == [2017-12-23] 1.3.0
1
+ # Changelog
2
+
3
+ ## 2.0.0 (unreleased)
4
+
5
+ * Require Rails 6.1+ and Ruby 2.7+ (tested on Rails 6.1 through 8.1)
6
+ * RSpec matchers no longer define helper methods on every object, and drop RSpec 1/2 compatibility
7
+ * Remove legacy Rails 2 plugin files (`init.rb`, `rails/init.rb`)
8
+ * Replace Travis CI and Appraisal with GitHub Actions
9
+ * Only package library files in the gem, and add RubyGems metadata
10
+ * Convert README and CHANGELOG to Markdown, and document when Rails' built-in `normalizes` is a better fit
11
+
12
+ ## 1.4.0 (2020-04-18)
13
+
14
+ * Internally restructure NilifyBlanks, use modern tooling like ActiveSupport::Concern
15
+ * Drop support for Rails 3, expand Appraisals to include Rails 6
16
+ * Change default before callback to :validation (from :save)
17
+
18
+ ## 1.3.0 (2017-12-23)
2
19
 
3
20
  * Use all columns (not just content_columns)
4
21
  * Allow nullables_only: false option for including columns that don't allow NULLs
5
22
  * Support citext columns
6
23
 
7
- == [2015-04-12] 1.2.1
24
+ ## 1.2.1 (2015-04-12)
8
25
 
9
26
  * Fix inconsistency in nilify_blanks RSpec matcher: should filter by type
10
27
 
11
- == [2014-07-04] 1.2.0
28
+ ## 1.2.0 (2014-07-04)
12
29
 
13
30
  * Drop support for ruby 1.8.7 and 1.9.2 - too many issues with backwards-compatibility
14
31
  * Explicit support for Rails 4.1 and 4.2
15
32
  * Up-to-date Appraisals and dependencies
16
33
 
17
- == [2014-07-04] 1.1.0
34
+ ## 1.1.0 (2014-07-04)
18
35
 
19
36
  * Implement new mechanism to allow for global usage at ActiveRecord::Base level (or namespaced base model)
20
37
  * Make define_nilify_blanks thread safe
21
38
  * New rspec matchers with: require "nilify_blanks/matchers"
22
39
  * Only consider string and text columns, and allow types option to configure
23
40
 
24
- == [2013-11-09] 1.0.4
41
+ ## 1.0.4 (2013-11-09)
25
42
 
26
43
  * Fix issues with wrapping define_attribute_methods in jruby on Rails 4 (Thanks, tank157)
27
44
 
28
- == [2013-11-09] 1.0.3
45
+ ## 1.0.3 (2013-11-09)
29
46
 
30
47
  * Add rbx and JRuby compatibility (Thanks, petergoldstein)
31
48
 
32
- == [2013-08-25] 1.0.2
49
+ ## 1.0.2 (2013-08-25)
33
50
 
34
51
  * Hook on AR::Base.define_attribute_methods in order to prevent eager load of models [byroot]
35
52
 
36
- == [2010-10-15] 1.0.0 Major Updates
53
+ ## 1.0.0 (2010-10-15) - Major Updates
37
54
 
38
55
  * Finally updated to be a Gem
39
56
  * Internal refactoring
40
57
  * Converted tests to RSpec
41
58
  * Added support for Rails 3.0
42
59
 
43
- == 0.1.2 Fixed issue with table not existing.
44
- == 0.1.0 Initial Version
60
+ ## 0.1.2
61
+
62
+ * Fixed issue with table not existing.
63
+
64
+ ## 0.1.0
65
+
66
+ * Initial Version
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # Nilify Blanks
2
+
3
+ [![CI](https://github.com/rubiety/nilify_blanks/actions/workflows/ci.yml/badge.svg)](https://github.com/rubiety/nilify_blanks/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/nilify_blanks.svg)](https://rubygems.org/gems/nilify_blanks)
5
+
6
+ In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This gem converts blank attributes to nil before a model is saved, either for every applicable column or for a list you choose.
7
+
8
+ By default, columns set as NOT NULL will not be nilified, since that value cannot be persisted, and it might be better to catch that with a validation. If you want to nilify those columns anyway, you can use `only:` (see below) to explicitly mention them.
9
+
10
+ Only attributes responding to `blank?` with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example.
11
+
12
+ ## Do you need this gem?
13
+
14
+ Rails 7.1 added [`normalizes`](https://api.rubyonrails.org/classes/ActiveRecord/Normalization/ClassMethods.html), which handles the per-attribute case well:
15
+
16
+ ```ruby
17
+ class Post < ApplicationRecord
18
+ normalizes :title, :author, with: ->(value) { value.presence }
19
+ end
20
+ ```
21
+
22
+ `normalizes` applies on assignment (not just before save) and to finder queries like `where(title: "")`, so prefer it when you only care about a few attributes.
23
+
24
+ Use nilify_blanks when you want this behavior for **every** nullable string/text column without listing them, including columns added later, or across all models at once via `ApplicationRecord` or `ActiveRecord::Base`.
25
+
26
+ ## Requirements
27
+
28
+ * Rails (Active Record) 6.1 or newer
29
+ * Ruby 2.7 or newer
30
+
31
+ Version 1.4.0 remains available for older Rails versions.
32
+
33
+ ## Install
34
+
35
+ Add the gem to your Gemfile:
36
+
37
+ ```ruby
38
+ gem "nilify_blanks"
39
+ ```
40
+
41
+ ## Basic Usage
42
+
43
+ ```ruby
44
+ # Checks and converts all nullable string, text and citext columns
45
+ class Post < ApplicationRecord
46
+ nilify_blanks
47
+ end
48
+
49
+ # Checks and converts only text columns
50
+ class Post < ApplicationRecord
51
+ nilify_blanks types: [:text]
52
+ end
53
+
54
+ # Checks and converts only the title and author fields
55
+ class Post < ApplicationRecord
56
+ nilify_blanks only: [:author, :title]
57
+ end
58
+
59
+ # Checks and converts all fields except for title and author
60
+ class Post < ApplicationRecord
61
+ nilify_blanks except: [:author, :title]
62
+ end
63
+
64
+ # Checks and converts any fields, regardless of their null constraint
65
+ class Post < ApplicationRecord
66
+ nilify_blanks nullables_only: false
67
+ end
68
+ ```
69
+
70
+ ## Global Usage
71
+
72
+ You can apply nilify_blanks to every model in your application:
73
+
74
+ ```ruby
75
+ class ApplicationRecord < ActiveRecord::Base
76
+ self.abstract_class = true
77
+ nilify_blanks
78
+ end
79
+ ```
80
+
81
+ Or to all models inheriting from `ActiveRecord::Base`:
82
+
83
+ ```ruby
84
+ ActiveRecord::Base.nilify_blanks
85
+ ```
86
+
87
+ Or perhaps just a model namespace base class:
88
+
89
+ ```ruby
90
+ Inventory::Base.nilify_blanks
91
+ ```
92
+
93
+ Individual models can still call `nilify_blanks` with their own options to override the inherited configuration.
94
+
95
+ ## Specifying a Callback
96
+
97
+ Checking uses an Active Record `before_validation` callback by default, but you can specify a different callback with the `:before` option. Any `before_*` callback will work; just remove the `before_` prefix from the name.
98
+
99
+ ```ruby
100
+ class Post < ApplicationRecord
101
+ nilify_blanks before: :save
102
+ end
103
+
104
+ class Post < ApplicationRecord
105
+ nilify_blanks before: :create
106
+ end
107
+ ```
108
+
109
+ ## RSpec Matchers
110
+
111
+ First, include the matchers:
112
+
113
+ ```ruby
114
+ require "nilify_blanks/matchers"
115
+ ```
116
+
117
+ To ensure for a given column:
118
+
119
+ ```ruby
120
+ RSpec.describe City do
121
+ it { is_expected.to nilify_blanks_for(:name) }
122
+ end
123
+ ```
124
+
125
+ To ensure for all applicable content columns:
126
+
127
+ ```ruby
128
+ RSpec.describe City do
129
+ it { is_expected.to nilify_blanks }
130
+ end
131
+ ```
132
+
133
+ You can optionally match on options also:
134
+
135
+ ```ruby
136
+ RSpec.describe City do
137
+ it { is_expected.to nilify_blanks_for(:name, before: :create) }
138
+ it { is_expected.to nilify_blanks(before: :create) }
139
+ end
140
+ ```
141
+
142
+ ## Running Tests
143
+
144
+ ```
145
+ bundle install
146
+ bundle exec rake
147
+ ```
148
+
149
+ CI runs the suite against each supported Rails version using the gemfiles in `gemfiles/`. To run against one locally:
150
+
151
+ ```
152
+ BUNDLE_GEMFILE=gemfiles/rails_7.2.gemfile bundle install
153
+ BUNDLE_GEMFILE=gemfiles/rails_7.2.gemfile bundle exec rake
154
+ ```
@@ -1,57 +1,39 @@
1
- rspec_module = defined?(RSpec::Core) ? 'RSpec' : 'Spec' # for RSpec 1 compatability
1
+ require "rspec/expectations"
2
2
 
3
- def failure_message_method
4
- rspec_major_version >= 3 ? :failure_message : :failure_message_for_should
5
- rescue
6
- :failure_message_for_should
7
- end
8
-
9
- def negated_failure_message_method
10
- rspec_major_version >= 3 ? :failure_message_when_negated : :failure_message_for_should_not
11
- rescue
12
- :failure_message_for_should_not
13
- end
14
-
15
- def rspec_major_version
16
- RSpec::Core::Version::STRING.split('.').first.to_i
17
- end
18
-
19
- Kernel.const_get(rspec_module)::Matchers.define :nilify_blanks_for do |column_name, options = {}|
3
+ RSpec::Matchers.define :nilify_blanks_for do |column_name, options = {}|
20
4
  match do |model_instance|
21
5
  model_class = model_instance.class
22
6
  model_class.define_attribute_methods
23
- model_class.included_modules.include?(NilifyBlanks::InstanceMethods) &&
24
- model_class.respond_to?(:nilify_blanks_columns) &&
7
+ model_class.include?(NilifyBlanks) &&
25
8
  model_class.nilify_blanks_columns.include?(column_name.to_s) &&
26
- options.all? {|k, v| model_class.instance_variable_get(:@_nilify_blanks_options)[k] == v }
9
+ options.all? {|k, v| (model_class.nilify_blanks_options || {})[k] == v }
27
10
  end
28
11
 
29
- send(failure_message_method) do |_|
30
- "expected to nilify blanks for #{column_name} #{options.inspect unless options.empty?}"
12
+ failure_message do
13
+ "expected to nilify blanks for #{column_name}#{" #{options.inspect}" unless options.empty?}"
31
14
  end
32
15
 
33
- send(negated_failure_message_method) do |_|
34
- "expected to not nilify blanks for #{column_name} #{options.inspect unless options.empty?}"
16
+ failure_message_when_negated do
17
+ "expected to not nilify blanks for #{column_name}#{" #{options.inspect}" unless options.empty?}"
35
18
  end
36
19
  end
37
20
 
38
- Kernel.const_get(rspec_module)::Matchers.define :nilify_blanks do |options = {}|
21
+ RSpec::Matchers.define :nilify_blanks do |options = {}|
39
22
  match do |model_instance|
40
- options[:types] = options[:types] ? Array.wrap(options[:types]).map(&:to_sym) : NilifyBlanks::ClassMethods::DEFAULT_TYPES
23
+ expected_types = options[:types] ? Array.wrap(options[:types]).map(&:to_sym) : NilifyBlanks::ClassMethods::DEFAULT_TYPES
41
24
 
42
25
  model_class = model_instance.class
43
26
  model_class.define_attribute_methods
44
- model_class.included_modules.include?(NilifyBlanks::InstanceMethods) &&
45
- model_class.respond_to?(:nilify_blanks_columns) &&
46
- model_class.nilify_blanks_columns == model_class.content_columns.select(&:null).select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s) &&
47
- options.all? {|k, v| model_class.instance_variable_get(:@_nilify_blanks_options)[k] == v }
27
+ model_class.include?(NilifyBlanks) &&
28
+ model_class.nilify_blanks_columns == model_class.columns.select(&:null).select {|c| expected_types.include?(c.type) }.map(&:name).map(&:to_s) &&
29
+ options.all? {|k, v| (model_class.nilify_blanks_options || {})[k] == v }
48
30
  end
49
31
 
50
- send(failure_message_method) do |_|
51
- "expected to nilify blanks #{options.inspect unless options.empty?}"
32
+ failure_message do
33
+ "expected to nilify blanks#{" #{options.inspect}" unless options.empty?}"
52
34
  end
53
35
 
54
- send(negated_failure_message_method) do |_|
55
- "expected to not nilify blanks #{options.inspect unless options.empty?}"
36
+ failure_message_when_negated do
37
+ "expected to not nilify blanks#{" #{options.inspect}" unless options.empty?}"
56
38
  end
57
39
  end
@@ -1,7 +1,7 @@
1
1
  module NilifyBlanks
2
2
  if defined?(Rails::Railtie)
3
3
  require "rails"
4
-
4
+
5
5
  class Railtie < Rails::Railtie
6
6
  initializer "nilify_blanks.extend_active_record" do
7
7
  ActiveSupport.on_load(:active_record) do
@@ -10,7 +10,7 @@ module NilifyBlanks
10
10
  end
11
11
  end
12
12
  end
13
-
13
+
14
14
  class Railtie
15
15
  def self.insert
16
16
  ActiveRecord::Base.send(:include, NilifyBlanks)
@@ -1,3 +1,3 @@
1
1
  module NilifyBlanks
2
- VERSION = "1.3.0"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/nilify_blanks.rb CHANGED
@@ -1,10 +1,16 @@
1
+ require "active_support/concern"
2
+
1
3
  module NilifyBlanks
2
- def self.included(base)
3
- base.extend ClassMethods
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :nilify_blanks_columns, instance_writer: false, default: []
8
+ class_attribute :nilify_blanks_options, instance_writer: false, default: nil
4
9
  end
5
10
 
6
11
  module ClassMethods
7
12
  DEFAULT_TYPES = [:string, :text, :citext]
13
+ DEFAULT_CALLBACK = :validation
8
14
 
9
15
  @@define_nilify_blank_methods_lock = Mutex.new
10
16
 
@@ -17,78 +23,52 @@ module NilifyBlanks
17
23
  end
18
24
  end
19
25
 
20
- def inherited(child_class)
21
- if instance_variable_get(:@_nilify_blanks_options) and !child_class.instance_variable_get(:@_nilify_blanks_options)
22
- child_class.nilify_blanks @_nilify_blanks_options
23
- end
24
-
25
- super
26
- end
27
-
28
26
  def nilify_blanks(options = {})
29
- return if @_nilify_blanks_options
30
-
31
- unless included_modules.include?(NilifyBlanks::InstanceMethods)
32
- include NilifyBlanks::InstanceMethods
33
- end
34
-
35
- @_nilify_blanks_options = options
27
+ self.nilify_blanks_options = options
36
28
 
37
29
  # Normally we wait for rails to define attribute methods, but we could be calling this after this has already been done.
38
30
  # If so, let's just immediately generate nilify blanks methods.
39
31
  #
40
- if @attribute_methods_generated
41
- define_nilify_blank_methods
42
- end
43
-
44
- descendants.each do |subclass|
45
- subclass.nilify_blanks @_nilify_blanks_options
46
- end
32
+ define_nilify_blank_methods if @attribute_methods_generated
47
33
  end
48
34
 
49
35
 
50
36
  private
51
37
 
52
38
  def define_nilify_blank_methods
53
- return unless @_nilify_blanks_options
54
- return if @nilify_blank_methods_generated
39
+ return unless nilify_blanks_options
55
40
 
56
41
  @@define_nilify_blank_methods_lock.synchronize do
57
- options = @_nilify_blanks_options
42
+ options = nilify_blanks_options.dup
58
43
 
44
+ options[:before] ||= DEFAULT_CALLBACK
59
45
  options[:only] = Array.wrap(options[:only]).map(&:to_s) if options[:only]
60
46
  options[:except] = Array.wrap(options[:except]).map(&:to_s) if options[:except]
61
47
  options[:types] = options[:types] ? Array.wrap(options[:types]).map(&:to_sym) : DEFAULT_TYPES
62
48
 
63
- cattr_accessor :nilify_blanks_columns
64
-
65
49
  if options[:only]
66
- self.nilify_blanks_columns = options[:only].clone
50
+ columns_to_nilify = options[:only].clone
67
51
  elsif options[:nullables_only] == false
68
- self.nilify_blanks_columns = self.columns.select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
52
+ columns_to_nilify = self.columns.select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
69
53
  else
70
- self.nilify_blanks_columns = self.columns.select(&:null).select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
54
+ columns_to_nilify = self.columns.select(&:null).select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
71
55
  end
72
56
 
73
- self.nilify_blanks_columns -= options[:except] if options[:except]
74
- self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)
57
+ columns_to_nilify -= options[:except] if options[:except]
58
+
59
+ self.nilify_blanks_columns = columns_to_nilify.map(&:to_s)
75
60
 
76
- options[:before] ||= :save
77
61
  send("before_#{options[:before]}", :nilify_blanks)
78
- @nilify_blanks_methods_generated = true
79
62
  end
80
63
  end
81
64
  end
82
65
 
83
- module InstanceMethods
84
- def nilify_blanks
85
- (self.nilify_blanks_columns || []).each do |column|
86
- value = read_attribute(column)
87
- next unless value.is_a?(String)
88
- next unless value.respond_to?(:blank?)
66
+ def nilify_blanks
67
+ (nilify_blanks_columns || []).each do |column|
68
+ value = read_attribute(column)
69
+ next unless value.is_a?(String)
89
70
 
90
- write_attribute(column, nil) if value.blank?
91
- end
71
+ write_attribute(column, nil) if value.blank?
92
72
  end
93
73
  end
94
74
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nilify_blanks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2017-12-23 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activesupport
@@ -16,114 +15,51 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: 3.0.0
18
+ version: '6.1'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: 3.0.0
25
+ version: '6.1'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: activerecord
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: 3.0.0
32
+ version: '6.1'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: 3.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 10.0.3
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 10.0.3
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.13'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.13'
69
- - !ruby/object:Gem::Dependency
70
- name: appraisal
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.0.2
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.0.2
83
- - !ruby/object:Gem::Dependency
84
- name: sqlite3
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: 1.3.6
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: 1.3.6
39
+ version: '6.1'
97
40
  description: Often times you'll end up with empty strings where you really want nil
98
41
  at the database level. This plugin automatically converts blanks to nil and is
99
42
  configurable.
100
- email: ben@railsgarden.com
43
+ email: me@benhughes.name
101
44
  executables: []
102
45
  extensions: []
103
46
  extra_rdoc_files: []
104
47
  files:
105
- - Appraisals
106
- - CHANGELOG.rdoc
107
- - Gemfile
108
- - Gemfile.lock
48
+ - CHANGELOG.md
109
49
  - LICENSE
110
- - README.rdoc
111
- - Rakefile
112
- - init.rb
50
+ - README.md
113
51
  - lib/nilify_blanks.rb
114
52
  - lib/nilify_blanks/matchers.rb
115
53
  - lib/nilify_blanks/railtie.rb
116
54
  - lib/nilify_blanks/version.rb
117
- - spec/db/database.yml
118
- - spec/db/schema.rb
119
- - spec/db/test.sqlite3
120
- - spec/nilify_blanks_spec.rb
121
- - spec/spec_helper.rb
122
- homepage: http://github.com/rubiety/nilify_blanks
55
+ homepage: https://github.com/rubiety/nilify_blanks
123
56
  licenses:
124
57
  - MIT
125
- metadata: {}
126
- post_install_message:
58
+ metadata:
59
+ source_code_uri: https://github.com/rubiety/nilify_blanks
60
+ changelog_uri: https://github.com/rubiety/nilify_blanks/blob/master/CHANGELOG.md
61
+ bug_tracker_uri: https://github.com/rubiety/nilify_blanks/issues
62
+ rubygems_mfa_required: 'true'
127
63
  rdoc_options: []
128
64
  require_paths:
129
65
  - lib
@@ -131,16 +67,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
67
  requirements:
132
68
  - - ">="
133
69
  - !ruby/object:Gem::Version
134
- version: '0'
70
+ version: '2.7'
135
71
  required_rubygems_version: !ruby/object:Gem::Requirement
136
72
  requirements:
137
73
  - - ">="
138
74
  - !ruby/object:Gem::Version
139
- version: 1.3.4
75
+ version: '0'
140
76
  requirements: []
141
- rubyforge_project: nilify_blanks
142
- rubygems_version: 2.6.11
143
- signing_key:
77
+ rubygems_version: 3.6.7
144
78
  specification_version: 4
145
79
  summary: Auto-convert blank fields to nil.
146
80
  test_files: []
data/Appraisals DELETED
@@ -1,23 +0,0 @@
1
- appraise "3.0" do
2
- gem "rails", "~> 3.0.9"
3
- end
4
-
5
- appraise "3.1" do
6
- gem "rails", "~> 3.1.6"
7
- end
8
-
9
- appraise "3.2" do
10
- gem "rails", "~> 3.2.6"
11
- end
12
-
13
- appraise "4.0" do
14
- gem "rails", "~> 4.0.12"
15
- end
16
-
17
- appraise "4.1" do
18
- gem "rails", "~> 4.1.8"
19
- end
20
-
21
- appraise "4.2" do
22
- gem "rails", "~> 4.2.0"
23
- end
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem 'rubysl', '~> 2.0', :platforms => [:rbx]
4
-
5
- gem 'activerecord-jdbcsqlite3-adapter', :platforms => [:jruby]
6
- gem 'sqlite3', :platforms => [:ruby]
7
-
8
- gemspec
data/Gemfile.lock DELETED
@@ -1,263 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- nilify_blanks (1.2.1)
5
- activerecord (>= 3.0.0)
6
- activesupport (>= 3.0.0)
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- activemodel (5.1.4)
12
- activesupport (= 5.1.4)
13
- activerecord (5.1.4)
14
- activemodel (= 5.1.4)
15
- activesupport (= 5.1.4)
16
- arel (~> 8.0)
17
- activesupport (5.1.4)
18
- concurrent-ruby (~> 1.0, >= 1.0.2)
19
- i18n (~> 0.7)
20
- minitest (~> 5.1)
21
- tzinfo (~> 1.1)
22
- appraisal (1.0.2)
23
- bundler
24
- rake
25
- thor (>= 0.14.0)
26
- arel (8.0.0)
27
- concurrent-ruby (1.0.5)
28
- diff-lcs (1.2.1)
29
- ffi2-generators (0.1.1)
30
- i18n (0.9.1)
31
- concurrent-ruby (~> 1.0)
32
- minitest (5.10.3)
33
- rake (10.0.3)
34
- rspec (2.13.0)
35
- rspec-core (~> 2.13.0)
36
- rspec-expectations (~> 2.13.0)
37
- rspec-mocks (~> 2.13.0)
38
- rspec-core (2.13.0)
39
- rspec-expectations (2.13.0)
40
- diff-lcs (>= 1.1.3, < 2.0)
41
- rspec-mocks (2.13.0)
42
- rubysl (2.0.15)
43
- rubysl-abbrev (~> 2.0)
44
- rubysl-base64 (~> 2.0)
45
- rubysl-benchmark (~> 2.0)
46
- rubysl-bigdecimal (~> 2.0)
47
- rubysl-cgi (~> 2.0)
48
- rubysl-cgi-session (~> 2.0)
49
- rubysl-cmath (~> 2.0)
50
- rubysl-complex (~> 2.0)
51
- rubysl-continuation (~> 2.0)
52
- rubysl-coverage (~> 2.0)
53
- rubysl-csv (~> 2.0)
54
- rubysl-curses (~> 2.0)
55
- rubysl-date (~> 2.0)
56
- rubysl-delegate (~> 2.0)
57
- rubysl-digest (~> 2.0)
58
- rubysl-drb (~> 2.0)
59
- rubysl-e2mmap (~> 2.0)
60
- rubysl-english (~> 2.0)
61
- rubysl-enumerator (~> 2.0)
62
- rubysl-erb (~> 2.0)
63
- rubysl-etc (~> 2.0)
64
- rubysl-expect (~> 2.0)
65
- rubysl-fcntl (~> 2.0)
66
- rubysl-fiber (~> 2.0)
67
- rubysl-fileutils (~> 2.0)
68
- rubysl-find (~> 2.0)
69
- rubysl-forwardable (~> 2.0)
70
- rubysl-getoptlong (~> 2.0)
71
- rubysl-gserver (~> 2.0)
72
- rubysl-io-console (~> 2.0)
73
- rubysl-io-nonblock (~> 2.0)
74
- rubysl-io-wait (~> 2.0)
75
- rubysl-ipaddr (~> 2.0)
76
- rubysl-irb (~> 2.0)
77
- rubysl-logger (~> 2.0)
78
- rubysl-mathn (~> 2.0)
79
- rubysl-matrix (~> 2.0)
80
- rubysl-mkmf (~> 2.0)
81
- rubysl-monitor (~> 2.0)
82
- rubysl-mutex_m (~> 2.0)
83
- rubysl-net-ftp (~> 2.0)
84
- rubysl-net-http (~> 2.0)
85
- rubysl-net-imap (~> 2.0)
86
- rubysl-net-pop (~> 2.0)
87
- rubysl-net-protocol (~> 2.0)
88
- rubysl-net-smtp (~> 2.0)
89
- rubysl-net-telnet (~> 2.0)
90
- rubysl-nkf (~> 2.0)
91
- rubysl-observer (~> 2.0)
92
- rubysl-open-uri (~> 2.0)
93
- rubysl-open3 (~> 2.0)
94
- rubysl-openssl (~> 2.0)
95
- rubysl-optparse (~> 2.0)
96
- rubysl-ostruct (~> 2.0)
97
- rubysl-pathname (~> 2.0)
98
- rubysl-prettyprint (~> 2.0)
99
- rubysl-prime (~> 2.0)
100
- rubysl-profile (~> 2.0)
101
- rubysl-profiler (~> 2.0)
102
- rubysl-pstore (~> 2.0)
103
- rubysl-pty (~> 2.0)
104
- rubysl-rational (~> 2.0)
105
- rubysl-readline (~> 2.0)
106
- rubysl-resolv (~> 2.0)
107
- rubysl-rexml (~> 2.0)
108
- rubysl-rinda (~> 2.0)
109
- rubysl-rss (~> 2.0)
110
- rubysl-scanf (~> 2.0)
111
- rubysl-securerandom (~> 2.0)
112
- rubysl-set (~> 2.0)
113
- rubysl-shellwords (~> 2.0)
114
- rubysl-singleton (~> 2.0)
115
- rubysl-socket (~> 2.0)
116
- rubysl-stringio (~> 2.0)
117
- rubysl-strscan (~> 2.0)
118
- rubysl-sync (~> 2.0)
119
- rubysl-syslog (~> 2.0)
120
- rubysl-tempfile (~> 2.0)
121
- rubysl-thread (~> 2.0)
122
- rubysl-thwait (~> 2.0)
123
- rubysl-time (~> 2.0)
124
- rubysl-timeout (~> 2.0)
125
- rubysl-tmpdir (~> 2.0)
126
- rubysl-tsort (~> 2.0)
127
- rubysl-un (~> 2.0)
128
- rubysl-uri (~> 2.0)
129
- rubysl-weakref (~> 2.0)
130
- rubysl-webrick (~> 2.0)
131
- rubysl-xmlrpc (~> 2.0)
132
- rubysl-yaml (~> 2.0)
133
- rubysl-zlib (~> 2.0)
134
- rubysl-abbrev (2.0.4)
135
- rubysl-base64 (2.0.0)
136
- rubysl-benchmark (2.0.1)
137
- rubysl-bigdecimal (2.0.2)
138
- rubysl-cgi (2.0.1)
139
- rubysl-cgi-session (2.0.1)
140
- rubysl-cmath (2.0.0)
141
- rubysl-complex (2.0.0)
142
- rubysl-continuation (2.0.0)
143
- rubysl-coverage (2.0.3)
144
- rubysl-csv (2.0.2)
145
- rubysl-english (~> 2.0)
146
- rubysl-curses (2.0.1)
147
- rubysl-date (2.0.6)
148
- rubysl-delegate (2.0.1)
149
- rubysl-digest (2.0.3)
150
- rubysl-drb (2.0.1)
151
- rubysl-e2mmap (2.0.0)
152
- rubysl-english (2.0.0)
153
- rubysl-enumerator (2.0.0)
154
- rubysl-erb (2.0.1)
155
- rubysl-etc (2.0.3)
156
- ffi2-generators (~> 0.1)
157
- rubysl-expect (2.0.0)
158
- rubysl-fcntl (2.0.4)
159
- ffi2-generators (~> 0.1)
160
- rubysl-fiber (2.0.0)
161
- rubysl-fileutils (2.0.3)
162
- rubysl-find (2.0.1)
163
- rubysl-forwardable (2.0.1)
164
- rubysl-getoptlong (2.0.0)
165
- rubysl-gserver (2.0.0)
166
- rubysl-socket (~> 2.0)
167
- rubysl-thread (~> 2.0)
168
- rubysl-io-console (2.0.0)
169
- rubysl-io-nonblock (2.0.0)
170
- rubysl-io-wait (2.0.0)
171
- rubysl-ipaddr (2.0.0)
172
- rubysl-irb (2.0.4)
173
- rubysl-e2mmap (~> 2.0)
174
- rubysl-mathn (~> 2.0)
175
- rubysl-readline (~> 2.0)
176
- rubysl-thread (~> 2.0)
177
- rubysl-logger (2.0.0)
178
- rubysl-mathn (2.0.0)
179
- rubysl-matrix (2.1.0)
180
- rubysl-e2mmap (~> 2.0)
181
- rubysl-mkmf (2.0.1)
182
- rubysl-fileutils (~> 2.0)
183
- rubysl-shellwords (~> 2.0)
184
- rubysl-monitor (2.0.0)
185
- rubysl-mutex_m (2.0.0)
186
- rubysl-net-ftp (2.0.1)
187
- rubysl-net-http (2.0.4)
188
- rubysl-cgi (~> 2.0)
189
- rubysl-erb (~> 2.0)
190
- rubysl-singleton (~> 2.0)
191
- rubysl-net-imap (2.0.1)
192
- rubysl-net-pop (2.0.1)
193
- rubysl-net-protocol (2.0.1)
194
- rubysl-net-smtp (2.0.1)
195
- rubysl-net-telnet (2.0.0)
196
- rubysl-nkf (2.0.1)
197
- rubysl-observer (2.0.0)
198
- rubysl-open-uri (2.0.0)
199
- rubysl-open3 (2.0.0)
200
- rubysl-openssl (2.1.0)
201
- rubysl-optparse (2.0.1)
202
- rubysl-shellwords (~> 2.0)
203
- rubysl-ostruct (2.0.4)
204
- rubysl-pathname (2.0.0)
205
- rubysl-prettyprint (2.0.3)
206
- rubysl-prime (2.0.1)
207
- rubysl-profile (2.0.0)
208
- rubysl-profiler (2.0.1)
209
- rubysl-pstore (2.0.0)
210
- rubysl-pty (2.0.2)
211
- rubysl-rational (2.0.1)
212
- rubysl-readline (2.0.2)
213
- rubysl-resolv (2.1.0)
214
- rubysl-rexml (2.0.2)
215
- rubysl-rinda (2.0.1)
216
- rubysl-rss (2.0.0)
217
- rubysl-scanf (2.0.0)
218
- rubysl-securerandom (2.0.0)
219
- rubysl-set (2.0.1)
220
- rubysl-shellwords (2.0.0)
221
- rubysl-singleton (2.0.0)
222
- rubysl-socket (2.0.1)
223
- rubysl-stringio (2.0.0)
224
- rubysl-strscan (2.0.0)
225
- rubysl-sync (2.0.0)
226
- rubysl-syslog (2.0.1)
227
- ffi2-generators (~> 0.1)
228
- rubysl-tempfile (2.0.1)
229
- rubysl-thread (2.0.2)
230
- rubysl-thwait (2.0.0)
231
- rubysl-time (2.0.3)
232
- rubysl-timeout (2.0.0)
233
- rubysl-tmpdir (2.0.1)
234
- rubysl-tsort (2.0.1)
235
- rubysl-un (2.0.0)
236
- rubysl-fileutils (~> 2.0)
237
- rubysl-optparse (~> 2.0)
238
- rubysl-uri (2.0.0)
239
- rubysl-weakref (2.0.0)
240
- rubysl-webrick (2.0.0)
241
- rubysl-xmlrpc (2.0.0)
242
- rubysl-yaml (2.0.4)
243
- rubysl-zlib (2.0.1)
244
- sqlite3 (1.3.13)
245
- thor (0.19.1)
246
- thread_safe (0.3.6)
247
- tzinfo (1.2.4)
248
- thread_safe (~> 0.1)
249
-
250
- PLATFORMS
251
- ruby
252
-
253
- DEPENDENCIES
254
- activerecord-jdbcsqlite3-adapter
255
- appraisal (~> 1.0.2)
256
- nilify_blanks!
257
- rake (~> 10.0.3)
258
- rspec (~> 2.13)
259
- rubysl (~> 2.0)
260
- sqlite3
261
-
262
- BUNDLED WITH
263
- 1.15.4
data/README.rdoc DELETED
@@ -1,107 +0,0 @@
1
- {<img src="https://secure.travis-ci.org/rubiety/nilify_blanks.png?branch=master" alt="Build Status" />}[http://travis-ci.org/rubiety/nilify_blanks]
2
- {<img src="https://gemnasium.com/rubiety/nilify_blanks.png" alt="Dependency Status" />}[https://gemnasium.com/rubiety/nilify_blanks]
3
- {<img src="https://codeclimate.com/github/rubiety/nilify_blanks.png" />}[https://codeclimate.com/github/rubiety/nilify_blanks]
4
-
5
- == Nilify Blanks
6
-
7
- In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.
8
-
9
- By default, columns set as NOT NULL will not be nilified, since that value cannot be persisted, and it might be better to catch that with a validation. If you want to nilify those columns anyway, you can use `only:` (see below) to explicitly mention them.
10
-
11
- Only attributes responding to blank? with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example. Usage is best shown through examples:
12
-
13
- == Requirements
14
-
15
- Rails 3.0 or later is required (ActiveRecord implementation is very different in Rails 2 and not easy to support both).
16
-
17
- Due to issues maintaining backwards compatibility, as of v1.2.0 this gem requires ruby 1.9.3 or higher.
18
- If you are still using 1.8.7 or 1.9.2, please use the v1.1.0 release.
19
-
20
- == Install
21
-
22
- Include the gem using bundler in your Gemfile:
23
-
24
- gem "nilify_blanks"
25
-
26
- == Basic Usage
27
-
28
- # Checks and converts all content fields in the model
29
- class Post < ActiveRecord::Base
30
- nilify_blanks
31
- end
32
-
33
- # Checks and converts only text fields in the model
34
- class Post < ActiveRecord::Base
35
- nilify_blanks :types => [:text]
36
- end
37
-
38
- # Checks and converts only the title and author fields
39
- class Post < ActiveRecord::Base
40
- nilify_blanks :only => [:author, :title]
41
- end
42
-
43
- # Checks and converts all fields except for title and author
44
- class Post < ActiveRecord::Base
45
- nilify_blanks :except => [:author, :title]
46
- end
47
-
48
- # Checks and converts any fields, regardless of thier null constraint
49
- class Post < ActiveRecord::Base
50
- nilify_blanks :nullables_only => false
51
- end
52
-
53
- == Global Usage
54
-
55
- You can also apply nilify_blanks to all models inheriting from ActiveRecord::Base:
56
-
57
- ActiveRecord::Base.nilify_blanks
58
-
59
- Or perhaps just a model namespace base class:
60
-
61
- Inventory::Base.nilify_blanks
62
-
63
- == Specifying a Callback
64
-
65
- Checking uses an ActiveRecord before_save filter by default, but you can specify a different filter with the :before option. Any filter will work - just first remove the "before_" prefix from the name.
66
-
67
- class Post < ActiveRecord::Base
68
- nilify_blanks :before => :create
69
- end
70
-
71
- class Post < ActiveRecord::Before
72
- nilify_blanks :before => :validation_on_update
73
- end
74
-
75
- == RSpec Matcher
76
-
77
- First, include the matchers:
78
-
79
- require "nilify_blanks/matchers"
80
-
81
- To ensure for a given column:
82
-
83
- describe City do
84
- it { should nilify_blanks_for(:name) }
85
- end
86
-
87
- To ensure for all applicable content columns:
88
-
89
- describe City do
90
- it { should nilify_blanks }
91
- end
92
-
93
- You can optionally match on options also:
94
-
95
- describe City do
96
- it { should nilify_blanks_for(:name, :before => :create) }
97
- end
98
-
99
- describe City do
100
- it { should nilify_blanks(:before => :create) }
101
- end
102
-
103
- == Running Tests
104
-
105
- This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested, then run to test all appraisals:
106
-
107
- $ rake appraisal test
data/Rakefile DELETED
@@ -1,39 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
-
4
- require 'rake'
5
- require 'rspec/core/rake_task'
6
-
7
- desc 'Default: run unit tests.'
8
- task :default => [:clean, :test]
9
-
10
- desc "Run Specs"
11
- RSpec::Core::RakeTask.new(:spec) do |t|
12
- end
13
-
14
- task :test => :spec
15
-
16
- desc "Clean up files."
17
- task :clean do |t|
18
- FileUtils.rm_rf "tmp"
19
- Dir.glob("nilify_blanks-*.gem").each {|f| FileUtils.rm f }
20
- Dir.glob("spec/db/*.sqlite3").each {|f| FileUtils.rm f }
21
- end
22
-
23
- begin
24
- require 'rdoc/task'
25
-
26
- desc "Generate documentation for the plugin."
27
- Rake::RDocTask.new(:rdoc) do |rdoc|
28
- rdoc.rdoc_dir = "rdoc"
29
- rdoc.title = "nilify_blanks"
30
- rdoc.options << "--line-numbers" << "--inline-source"
31
- rdoc.rdoc_files.include('README')
32
- rdoc.rdoc_files.include('lib/**/*.rb')
33
- end
34
- rescue LoadError
35
- puts 'RDocTask is not supported for this platform'
36
- end
37
-
38
- Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
39
-
data/init.rb DELETED
@@ -1,3 +0,0 @@
1
- require "nilify_blanks"
2
-
3
- NilifyBlanks::Railtie.insert
data/spec/db/database.yml DELETED
@@ -1,21 +0,0 @@
1
- sqlite:
2
- adapter: sqlite
3
- database: spec/db/test.sqlite
4
-
5
- sqlite3:
6
- adapter: sqlite3
7
- database: spec/db/test.sqlite3
8
-
9
- postgresql:
10
- adapter: postgresql
11
- username: postgres
12
- password: postgres
13
- database: nilify_blanks_plugin_test
14
- min_messages: ERROR
15
-
16
- mysql:
17
- adapter: mysql
18
- host: localhost
19
- username: root
20
- password:
21
- database: nilify_blanks_plugin_test
data/spec/db/schema.rb DELETED
@@ -1,15 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 0) do
2
-
3
- create_table :posts, :force => true do |t|
4
- t.string :first_name
5
- t.string :last_name, :null => false
6
- t.string :title
7
- t.text :summary
8
- t.text :body
9
- t.column :slug, :citext
10
- t.integer :views
11
- t.integer :category_id
12
- t.string :blog_id
13
- end
14
-
15
- end
data/spec/db/test.sqlite3 DELETED
Binary file
@@ -1,206 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe NilifyBlanks do
4
-
5
- context "Model with nilify_blanks" do
6
- before(:all) do
7
- class Post < ActiveRecord::Base
8
- nilify_blanks
9
- end
10
-
11
- @post = Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0, :blog_id => '')
12
- @post.save
13
- end
14
-
15
- it "should recognize all non-null string, text, citext columns" do
16
- Post.nilify_blanks_columns.should == ['first_name', 'title', 'summary', 'body', 'slug', 'blog_id']
17
- end
18
-
19
- it "should convert all blanks to nils" do
20
- @post.first_name.should be_nil
21
- @post.title.should be_nil
22
- @post.summary.should be_nil
23
- @post.body.should be_nil
24
- @post.slug.should be_nil
25
- @post.blog_id.should be_nil
26
- end
27
-
28
- it "should leave not-null last name field alone" do
29
- @post.last_name.should == ""
30
- end
31
-
32
- it "should leave integer views field alone" do
33
- @post.views.should == 0
34
- end
35
- end
36
-
37
- context "Model with nilify_blanks :nullables_only => false" do
38
- before(:all) do
39
- class PostWithNullables < ActiveRecord::Base
40
- self.table_name = "posts"
41
- nilify_blanks :nullables_only => false
42
- end
43
-
44
- @post = PostWithNullables.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0, :blog_id => '')
45
- end
46
-
47
- it "should recognize all (even null) string, text, citext columns" do
48
- PostWithNullables.nilify_blanks_columns.should == ['first_name', 'last_name', 'title', 'summary', 'body', 'slug', 'blog_id']
49
- end
50
- end
51
-
52
- context "Model with nilify_blanks :types => [:text]" do
53
- def citext_supported
54
- PostOnlyText.content_columns.detect {|c| c.type == :citext}
55
- end
56
-
57
- before(:all) do
58
- class PostOnlyText < ActiveRecord::Base
59
- self.table_name = "posts"
60
- nilify_blanks :types => [:text]
61
- end
62
-
63
- @post = PostOnlyText.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
64
- @post.save
65
- end
66
-
67
- it "should recognize all non-null text only columns" do
68
- expected_types = ['summary', 'body']
69
- expected_types << 'slug' unless citext_supported
70
- PostOnlyText.nilify_blanks_columns.should == expected_types
71
- end
72
-
73
- it "should convert all blanks to nils" do
74
- @post.summary.should be_nil
75
- @post.body.should be_nil
76
- @post.slug.should be_nil unless citext_supported
77
- end
78
-
79
- it "should leave not-null string fields alone" do
80
- @post.first_name.should == ""
81
- @post.last_name.should == ""
82
- @post.title.should == ""
83
- @post.slug.should == "" if citext_supported
84
- end
85
- end
86
-
87
- context "Model with nilify_blanks :only => [:first_name, :title]" do
88
- before(:all) do
89
- class PostOnlyFirstNameAndTitle < ActiveRecord::Base
90
- self.table_name = "posts"
91
- nilify_blanks :only => [:first_name, :title]
92
- end
93
-
94
- @post = PostOnlyFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
95
- @post.save
96
- end
97
-
98
- it "should recognize only first_name and title" do
99
- PostOnlyFirstNameAndTitle.nilify_blanks_columns.should == ['first_name', 'title']
100
- end
101
-
102
- it "should convert first_name and title blanks to nils" do
103
- @post.first_name.should be_nil
104
- @post.title.should be_nil
105
- end
106
-
107
- it "should leave other fields alone" do
108
- @post.summary.should == ""
109
- @post.body.should == ""
110
- @post.slug.should == ""
111
- end
112
- end
113
-
114
- context "Model with nilify_blanks :except => [:first_name, :title]" do
115
- before(:all) do
116
- class PostExceptFirstNameAndTitle < ActiveRecord::Base
117
- self.table_name = "posts"
118
- nilify_blanks :except => [:first_name, :title, :blog_id]
119
- end
120
-
121
- @post = PostExceptFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
122
- @post.save
123
- end
124
-
125
- it "should recognize only summary, body, and views" do
126
- PostExceptFirstNameAndTitle.nilify_blanks_columns.should == ['summary', 'body', 'slug']
127
- end
128
-
129
- it "should convert summary and body blanks to nils" do
130
- @post.summary.should be_nil
131
- @post.body.should be_nil
132
- @post.slug.should be_nil
133
- end
134
-
135
- it "should leave other fields alone" do
136
- @post.first_name.should == ""
137
- @post.title.should == ""
138
- end
139
- end
140
-
141
-
142
- context "Global Usage" do
143
- context "Namespaced Base Class with nilify_blanks inline" do
144
- before(:all) do
145
- module Admin1
146
- class Base < ActiveRecord::Base
147
- self.abstract_class = true
148
- nilify_blanks
149
- end
150
- end
151
-
152
- class Admin1::Post < Admin1::Base
153
- self.table_name = "posts"
154
- end
155
-
156
- @post = Admin1::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
157
- @post.save
158
- end
159
-
160
- it "should convert all blanks to nils" do
161
- @post.first_name.should be_nil
162
- end
163
- end
164
-
165
- context "Namespaced Base Class with nilify_blanks applied after definition" do
166
- before(:all) do
167
- module Admin2
168
- class Base < ActiveRecord::Base
169
- self.abstract_class = true
170
- end
171
- end
172
-
173
- class Admin2::Post < Admin2::Base
174
- self.table_name = "posts"
175
- end
176
-
177
- Admin2::Base.nilify_blanks
178
-
179
- @post = Admin2::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
180
- @post.save
181
- end
182
-
183
- it "should convert all blanks to nils" do
184
- @post.first_name.should be_nil
185
- end
186
- end
187
-
188
- context "Namespaced Base Class with nilify_blanks applied after definition" do
189
- before(:all) do
190
- ActiveRecord::Base.nilify_blanks
191
-
192
- class InheritedPost < ActiveRecord::Base
193
- self.table_name = "posts"
194
- end
195
-
196
- @post = InheritedPost.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
197
- @post.save
198
- end
199
-
200
- it "should convert all blanks to nils" do
201
- @post.first_name.should be_nil
202
- end
203
- end
204
-
205
- end
206
- end
data/spec/spec_helper.rb DELETED
@@ -1,15 +0,0 @@
1
- require "rubygems"
2
- require "rspec"
3
- require "active_support"
4
- require "active_record"
5
- require "yaml"
6
-
7
- # Establish DB Connection
8
- config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
9
- ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
10
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
11
-
12
- # Load Test Schema into the Database
13
- load(File.dirname(__FILE__) + "/db/schema.rb")
14
-
15
- require File.dirname(__FILE__) + '/../init'