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

avatar_uploader.rb 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. # Choose what kind of storage to use for this uploader:
  8. if Rails.env.test? or Rails.env.cucumber?
  9. storage :file
  10. end
  11. if Rails.env.development?
  12. storage :file
  13. end
  14. if Rails.env.production?
  15. # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog:
  16. # include Sprockets::Rails::Helper
  17. include Sprockets::Helpers::RailsHelper
  18. include Sprockets::Helpers::IsolatedHelper
  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 cache_dir
  28. " ./tmp/uploads/#{mounted_as}/#{model.id}"
  29. end
  30. # Provide a default URL as a default if there hasn't been a file uploaded:
  31. # def default_url
  32. # # For Rails 3.1+ asset pipeline compatibility:
  33. # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  34. #
  35. # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  36. # end
  37. # Process files as they are uploaded:
  38. # process :scale => [200, 300]
  39. #
  40. # def scale(width, height)
  41. # # do something
  42. # end
  43. # Create different versions of your uploaded files:
  44. version :thumb do
  45. process :resize_to_fit => [50, 50]
  46. end
  47. # Add a white list of extensions which are allowed to be uploaded.
  48. # For images you might use something like this:
  49. # def extension_white_list
  50. # %w(jpg jpeg gif png)
  51. # end
  52. # Override the filename of the uploaded files:
  53. # Avoid using model.id or version_name here, see uploader/store.rb for details.
  54. # def filename
  55. # "something.jpg" if original_filename
  56. # end
  57. end