Papermill

Asset management made easy.

Install the gem!

# Add github as a source for latest release of thoughtbot-paperclip gem who will be installed as a dependency and remove rubyforge's paperclip if needed (old 2.1.0 version)
$ sudo gem source -a http://gems.github.com
$ sudo gem uninstall paperclip
$ sudo gem install papermill

Try the demo!

$ sudo gem install sqlite3-ruby # unless you did it already
$ rails -m http://github.com/bbenezech/papermill/raw/master/installation-template.txt papermill-example
$ cd papermill-example
$ ./script/server
Open localhost:3000 in your browser and try to create an article with assets but without title

Papermill comes in 2 flavors:

Generic catch-all declaration

papermill my_option_hash                                # in your papermilled assetable model
assets_upload(:my_key, my_option_hash)                  # form helper call
@assetable.assets(:my_key)                              # data access in your view

Association specific declaration

papermill :my_association, my_option_hash               # in your papermilled assetable model
assets_upload(:my_association, my_option_hash)          # form helper call
@assetable.my_association                               # data access in your view

In both case, you can specify a PapermillAsset subclass to use with :class_name => MyPapermillAssetSubclass in the option hash.

You can have a catch-all declaration and as many specific association as you want in your model (as long as they use different keys).

It’s up to you. You can use the first one only, the second only or both.

See papermill_module.rb for the complete list of options.

Installation

Once you’ve installed the gem, generate a migration and copy a couple of static assets:

# Generate  the migration and migrate:
$ ./script/generate papermill_table PapermillMigration
$ rake db:migrate
# copy some needed static assets to your public directory:
$ ./script/generate papermill_assets

Then in environment.rb:

...

Rails::Initializer.run do |config|
  ...
  config.gem papermill

  # You can set options that will be set application-wide :
  module Papermill
    OPTIONS = {
      :thumbnail => {
        :width => 150,
        :height => 100
      },
      :aliases => {
        :big => "500x500>"
        :small => "100x100>"
      },
      :public_root => ":rails_root/public",         # already a default
      :papermill_prefix => "system/papermill"       # already a default
    }
  end
  # see lib/papermill/papermill_module.rb

  # You can use stringex's String#to_url (papermill will use its own String#to_url if none exists)
  config.gem 'stringex'
  ...
end

In your assetable model:

# You can set a catch-all papermill association : 
papermill :class_name => MyAssetClass

# or create an association for the specific :my_gallery key
papermill :my_gallery_assets, :class_name => MyGalleryAsset

In your layout:

<%= papermill_stylesheet_tag %>
<%= papermill_javascript_tag :with_jquery => "no_conflict" %>
# you won't need :with_jquery if you have it already.

In your edit form:

f.images_upload(:my_gallery)     # use specific papermill :my_gallery declaration
f.assets_upload(:my_assets)      # use catch-all
f.asset_upload(:my_other_asset)  # use catch-all

Access them with:

@assetable.my_gallery_assets.each{ |image| image_tag image.url("100x100") }
# equivalent to:
@assetable.assets(:my_gallery_assets).each{ |image| image_tag image.url("100x100") }
# also equivalent to:
@assetable.assets(:conditions => {:assetable_key => 'my_gallery_assets'}).each{ |image| image_tag image.url("100x100") }

@assetable.assets(:my_assets).each{ |asset| asset.url }
# if your association name is singularizable, you can do smtg like : 
@assetable.asset(:my_other_asset).try(:url)
# equivalent to:
@assetable.assets(:my_other_asset).first.try(:url)

Also see github.com/bbenezech/papermill/raw/master/installation-template.txt Have a look at the API here rdoc.info/projects/BBenezech/papermill

Translations:

Papermill is fully I18n-able. Copy config/locales/papermill.yml to your root config/locale folder to modify any wording in a any locale.

Word of caution:

Beta. Wait for gem 1.0.0 for the production ready thing. This is xNIX only (system(“rm …”)). Rails 2.3

Copyright © 2009 Benoit Bénézech, released under the MIT license