github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jystewart / image-associations

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

ActiveRecord extensions to make it easier to manage attachments — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Make methods more flexible so they either take a file directly or a hash 
with a key of uploaded_data 
James Stewart (author)
Tue Nov 11 07:45:59 -0800 2008
commit  1a9be06effa003da29481929c7ce1211f4cc012f
tree    b3d9179484545e334236c9d78f921ad9ab6b0c25
parent  50ca6b0ff5c3951c9042091eedb326edbf04ff14
image-associations /
name age
history
message
file MIT-LICENSE Loading commit data...
file README
file Rakefile
file init.rb
file install.rb
directory lib/
directory spec/
directory tasks/
file 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/
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server