Skip to content
This repository
Browse code

Specify default options in Rails 3.x configuration, README updated

  • Loading branch information...
commit efead1a179383e049a996f9d4ed6ae1d7ed8e9e7 1 parent 02f16c9
Mark Kremer authored February 24, 2012 mike-burns committed May 31, 2012
27  README.md
Source Rendered
@@ -180,6 +180,33 @@ Lastly, you can also define multiple validations on a single attachment using `v
180 180
       :content_type => { :content_type => "image/jpg" },
181 181
       :size => { :in => 0..10.kilobytes }
182 182
 
  183
+Defaults
  184
+--------
  185
+Global defaults for all your paperclip attachments can be defined by changing the Paperclip::Attachment.default_options Hash, this can be useful for setting your default storage settings per example so you won't have to define them in every has_attached_file definition.
  186
+
  187
+If you're using Rails 3.x you can define a Hash with default options in config/application.rb or in any of the config/environments/*.rb files on config.paperclip_defaults, these well get merged into Paperclip::Attachment.default_options as your Rails app boots. An example: 
  188
+
  189
+```ruby
  190
+module YourApp
  191
+  class Application < Rails::Application
  192
+    # Other code...
  193
+
  194
+    config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/public"}, :fog_directory => "", :fog_host => "localhost"}
  195
+  end
  196
+end
  197
+```
  198
+
  199
+In earlier version of Rails you can also directly modify the Paperclip::Attachment.default_options Hash in an initializer, this method also works when you're not using Rails (and still works in Rails 3).
  200
+
  201
+An example Rails initializer would look something like this:
  202
+
  203
+```ruby
  204
+Paperclip::Attachment.default_options[:storage] = :fog
  205
+Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
  206
+Paperclip::Attachment.default_options[:fog_directory] = ""
  207
+Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"}
  208
+```
  209
+
183 210
 Storage
184 211
 -------
185 212
 
3  lib/paperclip/railtie.rb
@@ -5,10 +5,11 @@ module Paperclip
5 5
   require 'rails'
6 6
 
7 7
   class Railtie < Rails::Railtie
8  
-    initializer 'paperclip.insert_into_active_record' do
  8
+    initializer 'paperclip.insert_into_active_record' do |app|
9 9
       ActiveSupport.on_load :active_record do
10 10
         Paperclip::Railtie.insert
11 11
       end
  12
+      Paperclip::Attachment.default_options.merge!(app.config.paperclip_defaults) if app.config.respond_to?(:paperclip_defaults)
12 13
     end
13 14
 
14 15
     rake_tasks { load "tasks/paperclip.rake" }

1 note on commit efead1a

Sasha

the best commit ever :+1:

Please sign in to comment.
Something went wrong with that request. Please try again.