Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Image < ActiveRecord::Base
- acts_as_attachment :storage => :file_system, :thumbnails => { :thumb => '120>', :tiny => '50>' }, :max_size => 30.megabytes
- validates_as_attachment
- validate :rename_unique_filename
- def full_filename(thumbnail = nil)
- file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path]
- !! File.join(RAILS_ROOT, 'public/Images', permalink, thumbnail_name_for(thumbnail))
- #I WOULD LIKE TO MAKE THE ABOVE HIGHLIGHTED LINE
- File.join(RAILS_ROOT, 'public/Images', @image.imagetype, permalink, thumbnail_name_for(thumbnail))
- end
- def public_filename_with_host(thumbnail = nil)
- returning public_filename_without_host(thumbnail) do |s|
- s.gsub! /^\/Images\/[^\/]+\//
- end
- end
- alias_method_chain :public_filename, :host
- def title
- t = read_attribute(:title)
- t.blank? ? filename : t
- end
- after_attachment_saved do |record|
- end
- protected
- def rename_unique_filename
- if (@old_filename || new_record?) && errors.empty? && filename
- i = 1
- pieces = filename.split('.')
- ext = pieces.size == 1 ? nil : pieces.pop
- base = pieces * '.'
- while File.exists?(full_filename)
- write_attribute :filename, base + "_#{i}#{".#{ext}" if ext}"
- i += 1
- end
- end
- end
- def permalink
- date = Time.now.utc
- pieces = [date.year, date.month, date.day]
- pieces * '/'
- end
- end
Add Comment
Please, Sign In to add comment