Skip to content

Instantly share code, notes, and snippets.

@voldern
Created January 26, 2009 22:27
Show Gist options
  • Save voldern/53006 to your computer and use it in GitHub Desktop.
Save voldern/53006 to your computer and use it in GitHub Desktop.
require 'ftools'
class Appartment
include DataMapper::Resource
attr_accessor :app_images
property :id, Serial
property :name, String, :nullable => false, :unique => true
property :bedrooms, Integer, :nullable => false
property :bathrooms, Integer, :nullable => false
property :beds, Integer, :nullable => false
property :description, Text, :nullable => false
has n, :images
after :create, :create_images
private
# Loops over all the images and runs create_image on them
def create_images
self.app_images.each { |image| create_image(image) }
end
# Creates a image row in the associated image table for the image,
# and moves the image into the right folder
def create_image(image)
appartment_image = Image.create(:appartment_id => self.id,
:filename => image[:filename],
:content_type => image[:content_type],
:size => image[:size]
)
File.makedirs("public/uploads/#{self.id}")
File.move(image[:tempfile].path, "public/uploads/#{self.id}/#{image[:filename]}")
end
end
class Image
include DataMapper::Resource
property :id, Serial
property :filename, String, :nullable => false
property :content_type, String, :nullable => false
property :size, Integer, :nullable => false
property :appartment_id, Integer, :nullable => false
belongs_to :appartment
def url
"/uploads/#{self.attachable_id}/#{self.filename}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment