Announcing image_associations
Discussions of multi-model forms and nested models in Rails has been revived recently, with various changes appearing in Edge Rails, plugins like attribute_fu getting a lot of attention, and the release of ActivePresenter. It looks like when the dust settles we’ll have a nice new set of ways to simplify our code.
One thing I frequently find myself doing is associating multiple images with a given model. My news story might have a banner image, and a series of other attachments, which I could specify with:
class Story < ActiveRecord::Base belongs_to :banner_image has_many :story_attachments end class BannerImage < ActiveRecord::Base has_attachment # I'm using attachment_fu end class StoryAttachment < ActiveRecord::Base has_attachment # I'm using attachment_fu end
(or I could use has_one in place of belongs_to there, your tastes/requirements may vary)
What quickly becomes a pain is assigning the images to the models, and having rejected fat controllers I often end up writing accessors on my models to manage that for me:
class Story < ActiveRecord::Base belongs_to :banner_image has_many :story_attachments def banner_image(data) if valid_file?(data) self.banner_image = BannerImage.create(:uploaded_data => data) end end def valid_file? # etc. end end
That quickly gets dull, so I've wrapped it up in a plugin I'm calling image_associations. With that I can write:
class Story < ActiveRecord::Base belongs_to_image :banner_image has_many_images :story_attachments end
and get the accessors for free.
It may only be of use to me, and it's pretty crude (there's no support for deleting attachments, for example) but it's been working nicely and has DRYed up my code in a satisfying way, so I thought I'd throw it out there. You can find it over on github.
(I'm not promising to keep supporting it, but it is used in an active project so chances are bug fixes will make it in. And of course, anyone is welcome to branch the project and twist it in their own ways)
More notes from a Rails 3.0pre upgrade
about 2 months ago - No comments
This is a follow-on from my piece on how I got the (development version of) Catapult Magazine up and running with Rails 3.0pre. If you haven’t already done so, I’d recommend you read that first.
Catapult makes use of the permalink_fu plugin which fails in Rails 3. It fails because of a reliance on the evaluate_attribute_method More >
Upgrading an app to Rails 3.0pre
about 2 months ago - 1 comment
I used to be a strong adherent to tracking edge rails. Up until the release of rails 2.3 I let most of my frequently updated projects track edge with a vendored copy of rails, and it rarely caused me any trouble. When 2.3 hit I rethought all that.
With Rails 3 development ramping up I More >
Cucumber, wordpress and database_cleaner
about 7 months ago - Comments Off
I’m up to my usual using-ruby-tools-to-test-other-environments tricks, using cucumber and my wordpress activerecord classes to do acceptance testing against a highly-customised wordpress install.
I’m hoping to write a bit more about that soon, once I’ve put it through its paces a little more and cleaned up some of the code, but I wanted to quickly mention More >
Talking to Wordpress with ActiveRecord
about 7 months ago - Comments Off
As mentioned in yesterday’s announcement I’m pulling some content across from this blog (running on wordpress) into the new Ket Lai site (a merb app). I’ve found myself doing similar things a few times lately, such as on Only Connect (on which more, soon) and so have built out a selection of ActiveRecord models to More >
Selected (belated, extended) Saturday Links
about 11 months ago - Comments Off
The past two weeks haven’t really left time to compile my selected links, though there have been many. A few days at SxSWi (on which more, later) followed by travelling with the family and the inevitable work backlog moved blogging way down the priority list. So here’s a mammoth selection to get me caught up. More >
Rails 2.3 final and theme_support updates
about 11 months ago - Comments Off
Somewhere between Ruby on Rails versions 2.3.0 (RC1) and 2.3.2 (final) a change was made to the arguments required for one of the methods the theme_support plugin requires. I must confess I hadn’t spotted it, but github user knapo kindly sent me a message with a patch. That patch is now applied in the main More >
Selected Saturday Links
about 1 year ago - Comments Off
Big themes this week have mostly revolved around twitter, facebook, and openness. Some have focussed on facebook redesigning to embrace a more twitter-like “web of flow” approach, and others on the fact that they’re jumping on various open web bandwagons. It’s been interesting to see some tie in with the government transparency thinking going around, More >
Rails 2.3 and theme_support part 3: Layouts
about 1 year ago - 11 comments
In my ongoing efforts to bring my fork of theme_support in line with Rails 2.3 I’ve covered the core views and email, but when I left off earlier today layouts still weren’t working.
The key problem with overriding layouts is that the process of identifying them relies on some class methods on ActionController::Base (provided in the More >
Rails 2.3 and theme_support part 2: ActionMailer
about 1 year ago - 6 comments
Stage 2 of fixing up theme_support for Rails 2.3 was making sure that ActionMailer picked up themed templates (for stage 1 information see here). That’s something I’d not quite cracked in the 2.2 version, so starting afresh with 2.3 forced me to spend the time to look through the full render path and figure out More >
Rails 2.3 and theme_support
about 1 year ago - 1 comment
A couple of months back, I realised that two of my projects (Generous and Catapult) could do with the help of the theme_support Rails plugin. Discovering that it didn’t play nicely with Rails 2.1, I created a fork on github and hacked at the _pick_template method to get it to do what I wanted. It More >
Comments are closed.
about 1 year ago
I wouldn’t mind adding this to attachment_fu with method names like belongs_to_attachment and has_many_attachments… This was actually how attachment_fu worked originally.
about 1 year ago
That’d be great, rick.
I’m headed out of town tomorrow but could look at what it’d take to turn my plugin into a patch some time next week?
about 1 year ago
Have you looked at the rewrite branch of attachment_fu at all? I’ve been wanting to make it master and push the current master to a legacy branch. It’s a *huge* change from the current attachment_fu though. But IMO it’s a much-needed simplification and reorg of the code.
about 1 year ago
I haven’t had a chance, but will definitely check it out and explore next week.