README.rdoc in uploader-0.1.3 vs README.rdoc in uploader-0.1.7
- old
+ new
@@ -15,32 +15,45 @@
=== Add the gem to environment.rb
config.gem 'uploader'
+=== Install jQuery
+uploader uses jQuery. You'll need to include it in your application. Download it here:
+http://jquery.com/
+
+Then include it in your layout:
+ <%= javascript_include_tag 'jquery/jquery.js' %>
+
+Another option is to use jRails
+http://ennerchi.com/projects/jrails
+
=== Create a model for uploads.
We recommend creating a model called upload.rb. acts_as_uploader accepts all valid options for paperclip via :has_attached_file => {}
class Upload < ActiveRecord::Base
- acts_as_uploader :enable_s3 => false,
- :has_attached_file => {
- :url => "/system/:attachment/:id_partition/:style/:basename.:extension",
- :path => ":rails_root/public/system/:attachment/:id_partition/:style/:basename.:extension",
- :styles => { :icon => "30x30!",
- :thumb => "100>",
- :small => "150>",
- :medium => "300>",
- :large => "660>"},
- :default_url => "/images/profile_default.jpg",
- :storage => :s3,
- :s3_credentials => File.join(RAILS_ROOT, 'config', 's3.yml'),
- :bucket => "assets.#{SITE[:domain]}",
- :s3_host_alias => "assets.#{SITE[:domain]}",
- :convert_options => {
- :all => '-quality 80'
- }
-
+
+ acts_as_uploader :enable_s3 => false,
+ :has_attached_file => {
+ :url => "/system/:attachment/:id_partition/:style/:basename.:extension",
+ :path => ":rails_root/public/system/:attachment/:id_partition/:style/:basename.:extension",
+ :styles => { :icon => "30x30!",
+ :thumb => "100>",
+ :small => "150>",
+ :medium => "300>",
+ :large => "660>" },
+ :default_url => "/images/profile_default.jpg",
+ :storage => :s3,
+ :s3_credentials => AMAZON_S3_CREDENTIALS,
+ :bucket => "assets.example.com",
+ :s3_host_alias => "assets.example.com",
+ :convert_options => {
+ :all => '-quality 80'
+ }
+ },
+ :s3_path => ':id_partition/:style/:basename.:extension'
+
# only allow images:
# validates_attachment_content_type :file, :content_type => ['image/jpeg', 'image/pjpeg', 'image/jpg']
# The following method is implemented in 'acts_as_uploader'. This is the method destroy will check to see if
# the user has permission to delete the object. Add additional logic as needed or if the existing logic
@@ -95,11 +108,11 @@
map.resources :uploads, :collection => { :swfupload => :post }
class UploadsController < Uploader::UploadsController
- before_filter :login_required
+ prepend_before_filter :login_required
protected
# The default 'get_upload_text' method throws an exception. You must override this method in your controller. It
# is used by the swf upload call to generate the html to be returned to the client.
@@ -121,10 +134,11 @@
end
end
# Simply attempts to redirect to the parent object. You might want to build something more sophisticated that
# redirect to different areas of you site depending on the type of object that was uploaded or on based on the parent.
+ # source can be :destroy_success, :create_success, :create_failure, :permission_denied
def get_redirect
@parent
end
# The default action is to call 'can_upload?' on the parent object. Be sure to implement 'can_upload?(check_user) on
@@ -141,23 +155,10 @@
@upload = Photo.find(params[:id])
end
end
-
-=== Other Methods
-
-uploader assumes that you have a method called 'redirect_back_or_default' which is common in many Rails projects. This method
-is called upon completion of destroy or created when the requested format is 'html'. A simple implementation of this method is listed
-below:
-
- def redirect_back_or_default(default)
- redirect_to(session[:return_to] || default)
- session[:return_to] = nil
- end
-
-
=== Configure your views.
You'll need something like this in your layout so that uploader can add in the required css and javascript files.
<%= yield :head -%>
@@ -207,10 +208,57 @@
=== Turn on the Daemon process
There are a number of timing issues that you will run into if you attempt to upload files directly to s3. To overcome that
problem uploader includes a daemon process which will send the files to Amazon asynchronously. Note that the uploader
will leave your local copy in place.
-TODO docs for Daemon
+
+Add the daemons gem and plugin:
+ sudo gem install daemons
+
+Then inside your Rails project:
+ script/plugin install git://github.com/dougal/daemon_generator.git
+ script/generate daemon amazonaws
+
+ RAILS_ENV=development lib/daemons/mailer_ctl start
+
+Learn more about the custom daemon gem with Ryan Bates screencast:
+ http://railscasts.com/episodes/129-custom-daemon
+
+
+== Use Rake to send files to s3
+uploader includes a task capable of sending files to s3 but it makes an assumption that the model you are interacting with
+is named 'Upload'.
+
+ rake uploader:upload_to_s3
+
+If you want to use a different model or several models just add a rake task to your project:
+
+ desc 'Send all uploads to S3. (Will only send uploads from a model named Upload)'
+ task :upload_to_s3 do
+
+ uploads = Upload.pending_s3_migration
+ uploads.each do |upload|
+ upload.remote = upload.local
+ upload.save!
+ end
+
+ photos = Photo.pending_s3_migration
+ photos.each do |photo|
+ photo.remote = photo.local
+ photo.save!
+ end
+
+ end
+
+== Setup Domains
+If you use Amazon's S3 service you can setup a cname to clean up your urls. Configure your s3 bucket as above:
+
+ :bucket => "assets.example.com"
+ :s3_host_alias => "assets.example.com"
+
+Your assets will be available at assets.example.com.s3.amazon.com. You can then create a CNAME in your DNS entries
+to point "assets.example.com" to "assets.example.com.s3.amazon.com". Your assets will then appear to be
+be served from assets.example.com even though they are loaded from Amazon.
== Other Stuff
If you'd like to add an ajax delete to your uploads page this code might come in handy.