Coder Social home page Coder Social logo

cattekin / nilify_blanks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rubiety/nilify_blanks

0.0 1.0 0.0 86 KB

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.

Home Page: http://benhughes.name/

License: MIT License

Ruby 100.00%

nilify_blanks's Introduction

<img src=“https://secure.travis-ci.org/rubiety/nilify_blanks.png?branch=master” alt=“Build Status” /> <img src=“https://gemnasium.com/rubiety/nilify_blanks.png” alt=“Dependency Status” /> <img src=“https://codeclimate.com/github/rubiety/nilify_blanks.png” />

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

Rails 3.0 or later is required (ActiveRecord implementation is very different in Rails 2 and not easy to support both).

Due to issues maintaining backwards compatibility, as of v1.2.0 this gem requires ruby 1.9.3 or higher. If you are still using 1.8.7 or 1.9.2, please use the v1.1.0 release.

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 thier 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_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.

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

class Post < ActiveRecord::Before
  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 test

nilify_blanks's People

Contributors

byroot avatar cattekin avatar henrik avatar petergoldstein avatar pjungwir avatar reiz avatar rubiety avatar throne3d avatar todd avatar zeke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.