A website template with lots of features, built with ruby on rails.

avatar_uploader.rb 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # encoding: utf-8
  2. class AvatarUploader < CarrierWave::Uploader::Base
  3. # Include RMagick or MiniMagick support:
  4. # include CarrierWave::RMagick
  5. include CarrierWave::MiniMagick
  6. include CarrierWave::MimeTypes
  7. include ::CarrierWave::Backgrounder::Delay
  8. # Choose what kind of storage to use for this uploader:
  9. if Rails.env.test? or Rails.env.cucumber?
  10. storage :file
  11. end
  12. if Rails.env.development?
  13. storage :file
  14. end
  15. if Rails.env.production?
  16. # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog:
  17. # include Sprockets::Rails::Helper
  18. include Sprockets::Helpers
  19. storage :fog
  20. end
  21. process :set_content_type
  22. # Override the directory where uploaded files will be stored.
  23. # This is a sensible default for uploaders that are meant to be mounted:
  24. def store_dir
  25. "uploads/#{mounted_as}/#{model.id}"
  26. end
  27. def root
  28. "#{Rails.root}/public"
  29. end
  30. def cache_dir
  31. " ./tmp/uploads/#{mounted_as}/"
  32. end
  33. # Provide a default URL as a default if there hasn't been a file uploaded:
  34. # def default_url
  35. # # For Rails 3.1+ asset pipeline compatibility:
  36. # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  37. #
  38. # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  39. # end
  40. # Process files as they are uploaded:
  41. # process :scale => [200, 300]
  42. #
  43. # def scale(width, height)
  44. # # do something
  45. # end
  46. # Create different versions of your uploaded files:
  47. version :thumb do
  48. process :resize_to_fit => [50, 50]
  49. end
  50. # Add a white list of extensions which are allowed to be uploaded.
  51. # For images you might use something like this:
  52. # def extension_white_list
  53. # %w(jpg jpeg gif png)
  54. # end
  55. # Override the filename of the uploaded files:
  56. # Avoid using model.id or version_name here, see uploader/store.rb for details.
  57. # def filename
  58. # "something.jpg" if original_filename
  59. # end
  60. end