This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
James Stewart (author)
Tue Nov 11 07:45:59 -0800 2008
commit 1a9be06effa003da29481929c7ce1211f4cc012f
tree b3d9179484545e334236c9d78f921ad9ab6b0c25
parent 50ca6b0ff5c3951c9042091eedb326edbf04ff14
tree b3d9179484545e334236c9d78f921ad9ab6b0c25
parent 50ca6b0ff5c3951c9042091eedb326edbf04ff14
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | ||
| |
README | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
install.rb | ||
| |
lib/ | ||
| |
spec/ | ||
| |
tasks/ | ||
| |
uninstall.rb |
README
ImageAssociations
=================
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.
Example
=======
class Story < ActiveRecord::Base
belongs_to_image :banner_image
has_many_images :story_attachments
end
s = Story.new
s.story_attachments = [file_one, file_two]
s.banner_image = file_tree
s.save
Copyright (c) 2008 James Stewart, released under the MIT license
http://jystewart.net/process/







