• This repository has been archived on 10/Jul/2022
  • Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    Ruby
  • License
    Other
  • Created over 16 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DataMapper port of the Paperclip plugin by Thoughtbot

DataMapper Paperclip

Build Status

DM-Paperclip is a port of Thoughtbot's Paperclip plugin that works with DataMapper. This plugin is fully compatible with the original ActiveRecord-oriented Paperclip. You can 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 application 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 Rubygems.org:

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

You will need to add an initializer to configure Paperclip. If on Rails, you can add a config/initializers/paperclip.rb, and on Merb, you can use config/init.rb and add it to the Merb::BootLoader.after_app_loads section. You can also use environment configs, a rackup file, a Rake task, or whatever.

Paperclip.configure do |config|
  config.root               = Rails.root # the application root to anchor relative urls (defaults to Dir.pwd)
  config.env                = Rails.env  # server env support, defaults to ENV['RACK_ENV'] or 'development'
  config.use_dm_validations = true       # validate attachment sizes and such, defaults to false
  config.processors_path    = 'lib/pc'   # relative path to look for processors, defaults to 'lib/paperclip_processors'
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 model:

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

More Repositories

1

deb-s3

Easily create and manage an APT repository on S3 -- NO LONGER MAINTAINED
Ruby
482
star
2

metis

An implementation of the Nagios NRPE daemon in Ruby to provide a framework for easy, testable monitors.
Ruby
90
star
3

python-ernie

Python implementation of the BERT-RPC server
Python
26
star
4

qilin

A lightweight framework for background processing across many child processes, inspired heavily by Unicorn.
Ruby
9
star
5

hopsharp

Hoptoad Notifier for .NET
C#
8
star
6

warehouse

Git/Subversion browser and changeset viewer
Ruby
8
star
7

laptop-setup

Personal laptop setup. ArchLinux + Dell XPS 13 9350
Python
8
star
8

chef-dev-setup

Set up a developers workstation with all the essentials
Ruby
7
star
9

branches

Branches - Simple private git repository hosting... Gitosis in Ruby
Ruby
7
star
10

dm-dbslayer

DBSlayer adapter for DataMapper 0.9
Ruby
6
star
11

resteasy

Simple generic client library for REST webservices
Ruby
5
star
12

tidbits

Little tidbits and modules I've written
C
3
star
13

kitchen

An alternative to chef-server for a git based workflow
Ruby
3
star
14

tigger

Tigger - Read only Git browser
3
star
15

resque-methodize

A Resque plugin that allows you to call individual methods on a Resque job
Ruby
2
star
16

gravy

Service for accessing and managing Git and Mercurial repositories over SSH
Ruby
2
star
17

learnivore

A lightweight framework for generating, solving, and verifying solutions of algebraic equations.
Haskell
2
star
18

skitchdav

SkitchDAV - Merb app for uploading images through Skitch using WebDAV
Ruby
2
star
19

processor-affinity

Set processor affinity from ruby
C
1
star
20

emacs.d

My .emacs.d folder
Emacs Lisp
1
star
21

tiny

ruby quiz
Ruby
1
star
22

rome

1
star
23

emacs-config

Emacs Lisp
1
star
24

krobertson.github.com

Ruby
1
star
25

kitchen-chef-test

Ruby
1
star
26

kens-twilio-resume

Ken's Twilio-enabled Resume
Ruby
1
star
27

net-geoip

C
1
star
28

sacruby-twilio-conf

SacRuby's Twilio Conference app
Ruby
1
star
29

vpnc-juniper

C
1
star
30

resque-simple-status

Ruby
1
star
31

docker-bitcoin-abc

Slim Bitcoin ABC Docker image
Shell
1
star