README

Ok, so now I have a status, what’s the best way to check if an object is equal to that status? or How I learned to stop worrying and love the ‘acts_as_statused’ plugin

Great question, well, first make sure the object you want to check has a status! Remember, not all models in our system have statuses. If you’re confident that your model has a status then continue on.

In your model make sure you that you have the following near the top of your class definition:

acts_as_statused

That’s all the code you need to put in to have access to that objects status. You do NOT have to put the standard: belongs_to :status

As a matter of fact if you do this I’ll personally beat you with your own shoe! Well, you’re asking yourself, what does ‘acts_as_statused’ do? Great question! First thing is does it does the belongs_to assignment. The next thing it does is magic!

Several methods will be created that are similiar to the ones on Status, except these methods give you really great, and efficient, ways to check an objects status. Let’s look at some examples, shall we?

How to get an object’s status: bob = User.find(1) bob.status

That’s pretty straight forward, no surprises there. The .status method returns the status object for the that object. How do I assert on object’s status is equal to another status? bob = User.find(1) bob.status_is_active? bob.status_is_pending?

Each of these methods is generated in a very similar way to the methods described earlier on status. For each record in the status table a new .status_is_XXXXX? method is created. These methods are essentially doing the following: def status_is_active?

self.status_id == Status.active.id

end

This, again, makes for very easy to read code. Example: if bob.status_is_active?

puts "welcome back bob"

end

Don’t forget that as we add new statuses to the database, we get new methods without a single line of code! Yippie!! How NOT to check an object’s status is equal to another status? bob = User.find(1) if bob.status == Status.active

puts "welcome back bob"

end

Not only is this code not pretty to look at, but it can also be a fairly expensive call. Again, I will administer beatings if I find people doing this sort of code! How to tell if an objects status has changed? bob = User.find(1) assert bob.status_is_active? puts bob.has_status_changed? (false) bob.status = Status.pending bob.update puts bob.has_status_changed? (true) puts bob.previous_status (this should return Status.active)

The ability to known if a.) the status has changed and b.) what it’s previous state is extremely useful, especially in observers. Here’s an example: item = Item.find(1) item.status = Status.pending item.update

In the observer: def after_update(item)

if item.has_status_changed?
  if item.previous_status == Status.active
    # do something
  end
end

end

In the model there are call back hooks for before and after of each verb (create, update, save, destroy). Example: def before_save_status_changed

#do something

end def after_save_status_changed

#do something else

end

These call backs only get called if the status has changed.