• Stars
    star
    322
  • Rank 130,398 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides a framework for saving incoming blank values as nil in the database in instances where you'd rather use DB NULL than simply a blank string.

<img src=“https://secure.travis-ci.org/rubiety/nilify_blanks.svg?branch=master” alt=“Build Status” />

Nilify Blanks

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.

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.

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:

Requirements

As of v1.4.0, this gem requires Rails 4 and Ruby 2.2 or higher.

Install

Include the gem using bundler in your Gemfile:

gem "nilify_blanks"

Basic Usage

# Checks and converts all content fields in the model
class Post < ActiveRecord::Base
  nilify_blanks
end

# Checks and converts only text fields in the model
class Post < ActiveRecord::Base
  nilify_blanks types: [:text]
end

# Checks and converts only the title and author fields
class Post < ActiveRecord::Base
  nilify_blanks only: [:author, :title]
end

# Checks and converts all fields except for title and author
class Post < ActiveRecord::Base
  nilify_blanks except: [:author, :title]
end

# Checks and converts any fields, regardless of their null constraint
class Post < ActiveRecord::Base
  nilify_blanks nullables_only: false
end

Global Usage

You can also apply nilify_blanks to all models inheriting from ActiveRecord::Base:

ActiveRecord::Base.nilify_blanks

Or perhaps just a model namespace base class:

Inventory::Base.nilify_blanks

Specifying a Callback

Checking uses an ActiveRecord before_validation callback by default, but you can specify a different callback with the :before option. Any callback will work - just first remove the “before_” prefix from the name.

class Post < ActiveRecord::Base
  nilify_blanks before: :create
end

class Post < ActiveRecord::Base
  nilify_blanks before: :validation_on_update
end

RSpec Matcher

First, include the matchers:

require "nilify_blanks/matchers"

To ensure for a given column:

describe City do
  it { should nilify_blanks_for(:name) }
end

To ensure for all applicable content columns:

describe City do
  it { should nilify_blanks }
end

You can optionally match on options also:

describe City do
  it { should nilify_blanks_for(:name, before: :create) }
end

describe City do
  it { should nilify_blanks(before: :create) }
end

Running Tests

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:

$ rake appraisal install
$ rake appraisal test

More Repositories

1

jazz_model

Jazz Model - A data model for Jazz theory and other cool stuff.
Ruby
180
star
2

message_block

A replacement for error_messages_for that is much more powerful/flexible.
Ruby
160
star
3

validates_lengths_from_database

Introspects your database string field maximum lengths and automatically defines length validations.
Ruby
83
star
4

has_draft

Allows for your ActiveRecord models to have "drafts" which are stored in a separate duplicate table that can be edited without affecting the "live" copy.
Ruby
68
star
5

vexflow-json

A wrapper for the VexFlow staff engraving library to render staff notation from simple JSON instead complex API calls.
JavaScript
46
star
6

jazzity

The Jazz Knowledge Engine
Ruby
25
star
7

rspecify

Uses ruby_scribe to convert your Test::Unit (+ shoulda) tests into RSpecs
Ruby
14
star
8

cukigem

Trying to test your gems in the context of a test Rails application is hard. This gem makes it easy.
Ruby
13
star
9

ruby_transform

A series of useful Ruby AST transformations
Ruby
10
star
10

micro_sessions

The repository has been moved to
Ruby
10
star
11

chords-json

A JSON format for representing musical chord changes with related tools.
9
star
12

jquery-vexflow-json

A jquery library to encapsulate staff engraving via VexFlow. Relies on vexflow-json.
JavaScript
9
star
13

irealb_parser

Parses iReal B chord changes notation into the standard chords-json format.
Ruby
8
star
14

goplay

GoPlay! Game Application in Merb, DataMapper, and jQuery
Ruby
8
star
15

ruby_scribe

Generates nicely-formatted ruby source code given a ruby abstract syntax tree (from seattlerb's ruby_parser).
Ruby
8
star
16

has_meta_data

Allows for your ActiveRecord models to have meta data associated with them that don't require a separate model - primarily useful with STI classes to somewhat emulate Class Table Inheritance.
Ruby
6
star
17

rubify

A framework for AST translations from other languages to a Ruby AST. A foundation for true language conversion to Ruby.
Ruby
4
star
18

factory_girl_upgrader

Uses ruby_parser and ruby_scribe to dynamically convert factory_girl factories in the V1 DSL into the V2 DSL.
Ruby
4
star
19

coworking_nomads

Web application for facilitating ad-hoc coworking among independent workers at free venues around town.
Ruby
3
star
20

ben_hughes

Ben Hughes Personal Website
CSS
2
star
21

ups_shipping

A ruby library for interacting with the UPS Shipping API.
Ruby
2
star
22

magic_hangman

Hangman Project for Magic Night
Ruby
1
star
23

blue_passers

Website for BluePassers.com - Online community and leaderboard for JetBlue BluePass Holders
Ruby
1
star
24

ruby_pranks

Ruby is such a dynamic language, it can be dangerous if you're not careful...
1
star
25

where_in_the_world_is_ben

Simple app to show where I am, through Foursquare checkins
Ruby
1
star
26

jet_blue

Some screen scraping tools to automate the JetBlue AYCJ/BluePass booking engine.
Ruby
1
star
27

ruby_finance

Financial/Business calculations as a ruby library.
1
star
28

penguinoh

Photo album of Penguinoh Waddles
HTML
1
star
29

flight_leverage

Simple Middleman blog for Flight Leverage (Travel Hacking).
HTML
1
star
30

jetting_rubyist

Website for JettingRubyist.com - Nomadic Co-working & Pairing
JavaScript
1
star