Giter Club home page Giter Club logo

when-1's Introduction

DEPRECATED

This plugin is deprecated. Please upgrade your Rails installation to at least 2.1 to get this functionality, maintained, as well as all the other joys that come with a Rails upgrade.

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 svn.thoughtbot.com/plugins/when/trunk vendor/plugins/when

Copyright © 2008 Jared Carroll, Dan Croak, and thoughtbot, inc. released under the MIT license

when-1's People

Contributors

mike-burns 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.