Guest User

Untitled

a guest
Feb 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. class Image < ActiveRecord::Base
  2. acts_as_attachment :storage => :file_system, :thumbnails => { :thumb => '120>', :tiny => '50>' }, :max_size => 30.megabytes
  3. validates_as_attachment
  4. validate :rename_unique_filename
  5.  
  6. def full_filename(thumbnail = nil)
  7. file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path]
  8. !! File.join(RAILS_ROOT, 'public/Images', permalink, thumbnail_name_for(thumbnail))
  9. #I WOULD LIKE TO MAKE THE ABOVE HIGHLIGHTED LINE
  10. File.join(RAILS_ROOT, 'public/Images', @image.imagetype, permalink, thumbnail_name_for(thumbnail))
  11. end
  12.  
  13. def public_filename_with_host(thumbnail = nil)
  14. returning public_filename_without_host(thumbnail) do |s|
  15. s.gsub! /^\/Images\/[^\/]+\//
  16. end
  17. end
  18. alias_method_chain :public_filename, :host
  19.  
  20. def title
  21. t = read_attribute(:title)
  22. t.blank? ? filename : t
  23. end
  24.  
  25. after_attachment_saved do |record|
  26.  
  27. end
  28.  
  29. protected
  30. def rename_unique_filename
  31. if (@old_filename || new_record?) && errors.empty? && filename
  32. i = 1
  33. pieces = filename.split('.')
  34. ext = pieces.size == 1 ? nil : pieces.pop
  35. base = pieces * '.'
  36. while File.exists?(full_filename)
  37. write_attribute :filename, base + "_#{i}#{".#{ext}" if ext}"
  38. i += 1
  39. end
  40. end
  41. end
  42.  
  43. def permalink
  44. date = Time.now.utc
  45. pieces = [date.year, date.month, date.day]
  46. pieces * '/'
  47. end
  48. end
Add Comment
Please, Sign In to add comment