Skip to content

rubaidh/timestamped_booleans

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

= Timestamped Booleans

== Introduction

Sometimes you have an attribute on a model that stores a time stamp in the
database, because sometimes you want to know *when* an event happens. But
mostly what you want to know is whether it has actually happened at all or
not. This plugin gives us an easy way to specify that, rather than having to
explicitly write methods to implement the boolean methods on top of the
datetime attribute methods.

Tested to work with Rails 2.3

== Example

Let's say I have a `Book` model which records when a book has been published
in the `published_at` field. But for the most part, I'm just interested in
whether the book has been published already or not. We can set up the time
stamped boolean in the model as follows:

    class Book < ActiveRecord::Base
      timestamped_boolean :published_at
    end

Now, in addition to the `published_at` and `published_at=` methods to
manipulate the attribute, we have `published?` and `published=` which treats
the field as a boolean.  Let's see how that works:

    irb(main):001:0> b = Book.new
    => #<Book id: nil, published_at: nil, created_at: nil, updated_at: nil>
    irb(main):002:0> b.published?
    => false
    irb(main):003:0> b.published = true
    => true
    irb(main):004:0> b.published?
    => true
    irb(main):005:0> b.published_at
    => Thu, 10 Apr 2008 08:06:43 IST +01:00
    irb(main):006:0> b.published = false
    => false
    irb(main):007:0> b.published_at
    => nil
    irb(main):008:0> b.published?
    => false

== TODO

* Parameterize the value we set so that it isn't necessarily +Time.now+. I
  suspect some people will want to specify a different value, probably as a
  lambda. However, I don't need that functionality yet. :-)

Copyright (c) 2008-2009 Rubaidh Ltd, released under the MIT license

About

Rails plugin to allow allow particular datetime attributes to also be treated as booleans

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages