Skip to content

Instantly share code, notes, and snippets.

@pbraswell
Created January 13, 2011 15:42
Show Gist options
  • Save pbraswell/778066 to your computer and use it in GitHub Desktop.
Save pbraswell/778066 to your computer and use it in GitHub Desktop.
class Asset < ActiveRecord::Base
include Helpers::AssetStorage
before_validation :clear_asset
belongs_to :ad, :polymorphic => true
stores_file_as :image,
:styles => {:micro => "75x43", :thumb => "128x95", :medium => "300x300", :large => "500x500"}
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 1.megabytes
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/gif', 'image/png']
def delete_asset=(value)
@delete_asset = !value.to_i.zero?
end
def delete_asset
!!@delete_asset
end
alias_method :delete_asset?, :delete_asset
def clear_asset
self.image = nil if delete_asset? && !image.dirty?
end
end
# == Schema Information
#
# Table name: assets
#
# id :integer not null, primary key
# ad_id :integer
# created_at :datetime
# updated_at :datetime
# image_file_name :string(255)
# image_content_type :string(255)
# image_file_size :integer
# image_updated_at :datetime
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment