Rails 3.0′s ActiveModel: How To Give Ruby Classes Some ActiveRecord Magic
One of the biggest benefits of bringing Merb developer Yehuda Katz on board to work on Rails 3.0 has been his relentless pursuit of extracting out all of Rails' magical abilities from their monolithic encasings and into separate, manageable chunks. A case in point is ActiveModel, a new library that provides the model related parts of ActiveRecord but without the database requirements.
Get Rails-like Model Behavior on Any Ruby Class
In extracting the model-building parts of ActiveRecord, ActiveModel makes it possible to add model-like behavior to any Ruby class, with no Rails or databases required. In his latest blog post, ActiveModel: Make Any Ruby Object Feel Like ActiveRecord, Yehuda shows off how to get Rails-style models with validations, serialization, callbacks, dirty tracking, internationalization, attributes, observers and all the other Rails goodness.
Example Code
I've taken Yehuda's main example of using ActiveModel on a non Rails class and extended it with some code that actually uses the model:
require 'active_model' class Person include ActiveModel::Validations validates_presence_of :first_name, :last_name attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name, @last_name = first_name, last_name end end a = Person.new("Fred", nil) a.valid? # => false a.last_name = "Flintstone" a.valid? # => true
Installing ActiveModel
If you're interested in ActiveModel and not so much in Rails 3.0, installing it is reasonably easy (though not as easy as only installing a gem just as yet):
- Go to or make a temporary directory
git clone git://github.com/rails/rails.git
cd rails
rake gem
gem install activesupport/pkg/activesupport-3.0.pre.gem
gem install activemodel/pkg/activemodel-3.0.pre.gem
Once this is all done, the code example above will work.
As an aside, if you fancy having a go with the full pre-release (a.k.a. "pre") version of Rails 3.0, check out Dr Nic's slightly out of date but otherwise useful guide.
January 13, 2010 at 4:56 pm
Hi,
Looking forward to read such more articles of Rails 3. Your special ability as an author of Ruby book adds lots of charm in reading your blogs.
January 13, 2010 at 5:24 pm
Thanks!
Just as an added note.. be careful if you install these gems. I don't /think/ the ActiveSupport gem should clash with older versions but.. depending on how you're using it, it might. If other things fry, just remove the 3.0-pre version and you're back to where you were.
January 13, 2010 at 7:11 pm
Yehuda put out significantly more recent (and easier) instructions for Rails 3.0.pre 2 weeks ago: http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app/
January 13, 2010 at 7:40 pm
Yehuda Katz recently posted about spinning up a new Rails 3.0 app here:
http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app
It should provide more up-to-date instructions than Dr. Nic's.
January 13, 2010 at 9:08 pm
@Peter, isn't vendoring in Rails itself within Rails apps now standard practice?
January 13, 2010 at 9:33 pm
Also, you can use a separate gemset with rvm, to avoid messing with your dev env.
January 13, 2010 at 10:37 pm
OT: Where do you get the pictures you use for blog posts, and is the woman in this photo ok? (It looks like the weights are about to go behind her)
January 13, 2010 at 10:59 pm
I've been brainstorming about how to include ActiveRecord-like validations and more recent active_support logic into a library I need to be compatible with earlier versions of rails. This may be the answer I was looking for. Thanks Peter! This is why I come to this RubyInside first!
January 13, 2010 at 11:05 pm
@Andrew: Usually I make them myself or use legally sourced photos (iStockPhoto, Flickr Creative Commons, logos, etc). Sometimes, though, I just Photoshop images I don't really have any rights to, such as this one, and pray. Don't worry - she's fine, I'm sure. You can blame Photoshop's "content aware resizing" for the seemingly forthcoming disaster in the shot ;-)
This wasn't one of my best choices, to be fair. Since ActiveModel has no logo I wanted to go with something mildly amusing on the topic of a "model" doing something "active." First I had a busty chick on a Harley but thought that might attract some whining. So then I went for muscle chicks - same deal. This was a kinda last ditch "let's get it out there" image..
January 13, 2010 at 11:06 pm
@Daniel: Common practice, but not the standard practice, I believe.
@Stephen & @Konstantin: Awesome suggestions.
January 14, 2010 at 12:01 am
Just a minor note...Ezra was the creator of Merb, not Yehuda.
January 14, 2010 at 12:15 am
@Jim: I'd say that's quite a key point actually.. thanks for the correction!
January 14, 2010 at 8:20 am
To give credits where it's due, most of the ActiveModel work was done by Josh Peek and some by me and Rick Olson, and had been in making long before the merge :)
January 14, 2010 at 2:41 pm
Rails validations are a great way to codify some of the intentions of the designer of the domain model. That leads to better code overall.
Kudos to Yehuda for making them available to all Ruby objects.
January 14, 2010 at 8:36 pm
I was just thinking wouldn't it be nice to use ActiveModel in a StaticMatic like project. Very nice.