Navigation Menu

Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Specify default options in Rails 3.x configuration, README updated
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkcor authored and mike-burns committed May 31, 2012
1 parent 02f16c9 commit efead1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -180,6 +180,33 @@ Lastly, you can also define multiple validations on a single attachment using `v
:content_type => { :content_type => "image/jpg" }, :content_type => { :content_type => "image/jpg" },
:size => { :in => 0..10.kilobytes } :size => { :in => 0..10.kilobytes }


Defaults
--------
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.

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:

```ruby
module YourApp
class Application < Rails::Application
# Other code...

config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/public"}, :fog_directory => "", :fog_host => "localhost"}
end
end
```

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).

An example Rails initializer would look something like this:

```ruby
Paperclip::Attachment.default_options[:storage] = :fog
Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
Paperclip::Attachment.default_options[:fog_directory] = ""
Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"}
```

Storage Storage
------- -------


Expand Down
3 changes: 2 additions & 1 deletion lib/paperclip/railtie.rb
Expand Up @@ -5,10 +5,11 @@ module Paperclip
require 'rails' require 'rails'


class Railtie < Rails::Railtie class Railtie < Rails::Railtie
initializer 'paperclip.insert_into_active_record' do initializer 'paperclip.insert_into_active_record' do |app|
ActiveSupport.on_load :active_record do ActiveSupport.on_load :active_record do
Paperclip::Railtie.insert Paperclip::Railtie.insert
end end
Paperclip::Attachment.default_options.merge!(app.config.paperclip_defaults) if app.config.respond_to?(:paperclip_defaults)
end end


rake_tasks { load "tasks/paperclip.rake" } rake_tasks { load "tasks/paperclip.rake" }
Expand Down

1 comment on commit efead1a

@sasharevzin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best commit ever 👍

Please sign in to comment.