github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

thoughtbot / when

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 27
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Conditional checks on Rails filters. The same functionality was added to Rails 2.1, so this is only necessary for pre-Rails 2.1 apps. — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

made the string change to constants 
dcroak (author)
Mon Feb 18 22:14:25 -0800 2008
commit  ec9acc9dc58d2621052beb4cfa02550cd751328d
tree    b55ef3faafa2f1d2d53925e2ed8b92711e1c123d
parent  bdf498d8987f665fb0104364558bec7cfe02558e
when /
name age
history
message
file MIT-LICENSE Wed Feb 13 16:25:45 -0800 2008 updated the README git-svn-id: https://svn.tho... [jcarroll]
file README Fri Feb 15 08:07:43 -0800 2008 README message about Obsoleteness git-svn-id: ... [dcroak]
file Rakefile Mon Feb 11 18:43:52 -0800 2008 test and README files git-svn-id: https://svn.... [dcroak]
file init.rb Mon Feb 11 22:32:55 -0800 2008 refactoring tests git-svn-id: https://svn.thou... [dcroak]
file install.rb Mon Feb 11 18:23:48 -0800 2008 added when plugin skeleton git-svn-id: https:/... [dcroak]
directory lib/ Thu Feb 14 21:36:09 -0800 2008 fixed callbacks String condition git-svn-id: h... [jcarroll]
directory test/ Mon Feb 18 22:14:25 -0800 2008 made the string change to constants git-svn-id... [dcroak]
file uninstall.rb Mon Feb 11 18:23:48 -0800 2008 added when plugin skeleton git-svn-id: https:/... [dcroak]
README
When
====

When adds :if and :unless conditions to ActiveRecord callbacks 
and validations and ActionController filters. It works exactly 
the way as the current implementation of #validates_acceptance_of.

It works on the 12 regular callbacks:

before_validation
before_validation_on_create
after_validation
after_validation_on_create
before_save
before_create
before_update
after_create
after_update
after_save
before_destroy
after_destroy

3 validations:

validate
validate_on_create
validate_on_update

and 1 filter:

before_filter

It works when :if or :unless is passed a Symbol, a Proc or a String.
They return or evaluate to a true or false value.

Example
=======

class Address < ActiveRecord::Base

  before_save :geolocate

  def geolocate
    if complete?
      ...
    end
  end

  def complete?
    street? && city? && state? && zip?
  end

end

In this case, we want to find the latitude and longitude of an address only if 
the address is complete.

Wrapping the entirety of a callback method with conditional logic is bad form.
The callback should execute WHEN the model's life cycle reaches its 
"before_save" point and WHEN its address is "complete."

With When, the WHEN responsibility is moved to where it belongs:
as part of the callback.

class Address < ActiveRecord::Base

  before_save :geolocate, 
    :if => :complete?

  def geolocate
    ...
  end

  def complete?
    street? && city? && state? && zip?
  end

end

before_create's single responsibility is to execute code WHEN certain conditions are met.
geolocate's single responsibility is to ... geolocate. It should not contain its own
preconditions. 

More Examples
=============

before_create :encrypt_password,
  :unless => lambda {|user| user.password_confirmation.blank?}

before_filter :log_in!,
  :only => [:new, :create],
  :unless => :logged_in?

What When does NOT support
==========================

# ActiveRecord Class callbacks
before_create PasswordEncryptor,
  :unless => lambda {|user| user.password_confirmation.blank?}

# ActionController Class filters
before_filter Authorizer,
  :unless => :logged_in?

When will not work if your code contains any of these.

In our experience we've never used class callbacks or filters and find them to be overkill.

Obsolete after Rails 2.0.3
==========================

Whenever the next release after Rails 2.0.2 comes out, this feature will be baked into Rails
via the ActiveSupport::Callbacks module. Happy coding!

Installation
============

piston import https://svn.thoughtbot.com/plugins/when/trunk vendor/plugins/when

Copyright (c) 2008 Jared Carroll, Dan Croak, and thoughtbot, inc. released under the MIT license
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server