github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

krobertson / dm-paperclip

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 73
    • 19
  • Source
  • Commits
  • Network (19)
  • Issues (4)
  • Downloads (3)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (3)
    • 2.1.4
    • 2.1.2.1
    • 2.1.2
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

DataMapper port of the Paperclip plugin by Thoughtbot — Read more

  cancel

http://invalidlogic.com/dm-paperclip/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Updating dm-paperclip root paths to support both merb/rails 
merbjedi (author)
Mon Nov 09 23:07:54 -0800 2009
krobertson (committer)
Wed Nov 25 09:51:34 -0800 2009
commit  fc06dc9bd2026224bf9c316ae50eecfb0569ffc8
tree    a2e16563ec49d54ac82d8f63396a75f668171472
parent  f544279884fb6792cd22485a57d88df7f03aebdf
dm-paperclip /
name age
history
message
file .gitignore Wed May 14 23:31:01 -0700 2008 Added rake tasks for generating/installing the ... [krobertson]
file LICENSE Wed May 14 23:31:01 -0700 2008 Added rake tasks for generating/installing the ... [krobertson]
file README.rdoc Sun Jun 14 22:32:23 -0700 2009 idomatic and *correct* datamapper code in READM... [snusnu]
file Rakefile Sun Jun 14 22:32:15 -0700 2009 explicitly require dm-core * fixes an issue wi... [snusnu]
file init.rb Thu May 15 22:11:20 -0700 2008 Fix stupid mistake with the destroy handler, 2.... [krobertson]
directory lib/ Wed Nov 25 09:51:34 -0800 2009 Updating dm-paperclip root paths to support bot... [merbjedi]
directory tasks/ Wed May 14 23:51:28 -0700 2008 Updated to match up to Paperclip 2.1.2 [krobertson]
directory test/ Sun Jun 14 22:32:22 -0700 2009 merged git://github.com/cheba/dm-paperclip.git ... [snusnu]
README.rdoc

DataMapper Paperclip

DM-Paperclip is a port of Thoughtbot’s Paperclip plugin to work with DataMapper 0.9. This plugin is fully compatible with the original ActiveRecord-oriented Paperclip. You could take an existing ActiveRecord database and use it with DataMapper. The module also includes updates validation handling and automatic including of the necessary ‘property’ fields into your model.

To use it within your models, you need to ensure the three database fields are included. They are {name}_file_name, {name}_content_type, and {name}_file_size. The first two are strings, the final _file_size column is an integer. So if your user model has an avatar field, then you would add avatar_file_name, avatar_content_type, and avatar_file_size.

As with the original Paperclip plugin, it allows processing of thumbnails at the time the record is saved though ImageMagick. It processes the thumbnails through the command-line applications instead of using RMagick.

See the documentation for the has_attached_file method for options.

Code

The code DM-Paperclip is available at Github:

  git clone git://github.com/krobertson/dm-paperclip.git

It is regularly updated to keep in sync with the latest from Thoughtbot.

Releases are tagged within the repository and versioned the same as the original model. You can also get the latest release packaged as a gem through Rubyforge:

  sudo gem install dm-paperclip

Usage

In your model:

  class User
    include DataMapper::Resource
    include Paperclip::Resource
    property :id, Serial
    property :username, String
    has_attached_file :avatar,
                      :styles => { :medium => "300x300>",
                                   :thumb => "100x100>" }
  end

Your database will need to add four columns, avatar_file_name (varchar), avatar_content_type (varchar), and avatar_file_size (integer), and avatar_updated_at (datetime). You can either add these manually, auto- migrate, or use the following migration:

  migration( 1, :add_user_paperclip_fields ) do
  up do
      modify_table :users do
        add_column :avatar_file_name, "varchar(255)"
        add_column :avatar_content_type, "varchar(255)"
        add_column :avatar_file_size, "integer"
        add_column :avatar_updated_at, "datetime"
      end
    end
    down do
      modify_table :users do
        drop_columns :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at
      end
    end
  end

In your edit and new views:

  <% form_for @user, { :action => url(:user), :multipart => true } do %>
    <%= file_field :name => 'avatar' %>
  <% end %>

In your controller:

  def create
    ...
    @user.avatar = params[:avatar]
  end

In your show view:

  <%= image_tag @user.avatar.url %>
  <%= image_tag @user.avatar.url(:medium) %>
  <%= image_tag @user.avatar.url(:thumb) %>

The following validations are available:

  validates_attachment_presence :avatar
  validates_attachment_content_type :avatar, :content_type => "image/png"
  validates_attachment_size :avatar, :in => 1..10240
  validates_attachment_thumbnails :avatar

In order to use validations, you must have loaded the ‘dm-validations’ gem into your app (available as a part of dm-more). If the gem isn’t loaded before DM-Paperclip is loaded, the validation methods will be excluded. You will also need to include DataMapper::Validate into your mode:

  class User
    include DataMapper::Resource
    include DataMapper::Validate
    include Paperclip::Resource
    property :id, Serial
    property :username, String
    has_attached_file :avatar,
                      :styles => { :medium => "300x300>",
                                   :thumb => "100x100>" }
    validates_attachment_size :avatar, :in => 1..5120
  end
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server