Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (89 sloc) 4.53 kB
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
1 =DataMapper Paperclip
54f546b @krobertson Added README, .gitignore, and changed _file_size field to Fixnum (oops)
krobertson authored
2
c1cc4c3 @krobertson Removed old reference to dm-paperclip being for datamapper 0.9
krobertson authored
3 DM-Paperclip is a port of Thoughtbot's Paperclip plugin to work with DataMapper. This plugin is fully compatible with
54f546b @krobertson Added README, .gitignore, and changed _file_size field to Fixnum (oops)
krobertson authored
4 the original ActiveRecord-oriented Paperclip. You could take an existing ActiveRecord database and use it with DataMapper.
5 The module also includes updates validation handling and automatic including of the necessary 'property' fields into
6 your model.
7
8 To use it within your models, you need to ensure the three database fields are included. They are {name}_file_name,
9 {name}_content_type, and {name}_file_size. The first two are strings, the final _file_size column is an integer. So
10 if your user model has an avatar field, then you would add avatar_file_name, avatar_content_type, and avatar_file_size.
11
12 As with the original Paperclip plugin, it allows processing of thumbnails at the time the record is saved though ImageMagick.
13 It processes the thumbnails through the command-line applications instead of using RMagick.
14
15 See the documentation for the +has_attached_file+ method for options.
16
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
17 ==Code
3f1927c @krobertson Final doc changes for the gem
krobertson authored
18
19 The code DM-Paperclip is available at Github:
20
300ea4d @krobertson Changed require in tests to 'dm-core' instead of 'data_mapper' and fi…
krobertson authored
21 git clone git://github.com/krobertson/dm-paperclip.git
3f1927c @krobertson Final doc changes for the gem
krobertson authored
22
23 It is regularly updated to keep in sync with the latest from Thoughtbot.
24
25 Releases are tagged within the repository and versioned the same as the original model. You can also get the latest release
26 packaged as a gem through Rubyforge:
27
28 sudo gem install dm-paperclip
29
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
30 ==Usage
54f546b @krobertson Added README, .gitignore, and changed _file_size field to Fixnum (oops)
krobertson authored
31
32 In your model:
33
34 class User
35 include DataMapper::Resource
36 include Paperclip::Resource
64f767c @snusnu idomatic and *correct* datamapper code in README
snusnu authored
37 property :id, Serial
54f546b @krobertson Added README, .gitignore, and changed _file_size field to Fixnum (oops)
krobertson authored
38 property :username, String
39 has_attached_file :avatar,
40 :styles => { :medium => "300x300>",
41 :thumb => "100x100>" }
42 end
43
195e523 @krobertson Added config to test helper, updated README with config info
krobertson authored
44 You will need to add an initializer to configure Paperclip. If on Rails, can add a config/initializers/paperclip.rb, on Merb
45 can use config/init.rb and add it to the Merb::BootLoader.after_app_loads section. Can also use environment configs, rackup
46 file, Rake task, wherever.
47
48 Paperclip.configure do |config|
49 config.root = Rails.root # the application root to anchor relative urls (defaults to Dir.pwd)
50 config.env = Rails.env # server env support, defaults to ENV['RACK_ENV'] or 'development'
51 config.use_dm_validations = true # validate attachment sizes and such, defaults to false
52 config.processors_path = 'lib/pc' # relative path to look for processors, defaults to 'lib/paperclip_processors'
53 end
54
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
55 Your database will need to add four columns, avatar_file_name (varchar), avatar_content_type (varchar), and
56 avatar_file_size (integer), and avatar_updated_at (datetime). You can either add these manually, auto-
57 migrate, or use the following migration:
6fcc5cc @krobertson Updated readme to mention validations and migrations,
krobertson authored
58
59 migration( 1, :add_user_paperclip_fields ) do
60 up do
61 modify_table :users do
62 add_column :avatar_file_name, "varchar(255)"
63 add_column :avatar_content_type, "varchar(255)"
64 add_column :avatar_file_size, "integer"
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
65 add_column :avatar_updated_at, "datetime"
6fcc5cc @krobertson Updated readme to mention validations and migrations,
krobertson authored
66 end
67 end
68 down do
69 modify_table :users do
b031296 @krobertson Updated version attributes, 2.1.4 now
krobertson authored
70 drop_columns :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at
6fcc5cc @krobertson Updated readme to mention validations and migrations,
krobertson authored
71 end
72 end
73 end
74
54f546b @krobertson Added README, .gitignore, and changed _file_size field to Fixnum (oops)
krobertson authored
75 In your edit and new views:
76
77 <% form_for @user, { :action => url(:user), :multipart => true } do %>
78 <%= file_field :name => 'avatar' %>
79 <% end %>
80
81 In your controller:
82
83 def create
84 ...
85 @user.avatar = params[:avatar]
86 end
87
88 In your show view:
89
90 <%= image_tag @user.avatar.url %>
91 <%= image_tag @user.avatar.url(:medium) %>
92 <%= image_tag @user.avatar.url(:thumb) %>
6fcc5cc @krobertson Updated readme to mention validations and migrations,
krobertson authored
93
94 The following validations are available:
95
96 validates_attachment_presence :avatar
97 validates_attachment_content_type :avatar, :content_type => "image/png"
98 validates_attachment_size :avatar, :in => 1..10240
46c2f3d @krobertson Updated to not require dm-validations, and only load validations if i…
krobertson authored
99 validates_attachment_thumbnails :avatar
100
101 In order to use validations, you must have loaded the 'dm-validations' gem into your app
102 (available as a part of dm-more). If the gem isn't loaded before DM-Paperclip is loaded,
103 the validation methods will be excluded. You will also need to include DataMapper::Validate
104 into your mode:
105
106 class User
107 include DataMapper::Resource
108 include DataMapper::Validate
109 include Paperclip::Resource
64f767c @snusnu idomatic and *correct* datamapper code in README
snusnu authored
110 property :id, Serial
46c2f3d @krobertson Updated to not require dm-validations, and only load validations if i…
krobertson authored
111 property :username, String
112 has_attached_file :avatar,
113 :styles => { :medium => "300x300>",
114 :thumb => "100x100>" }
115 validates_attachment_size :avatar, :in => 1..5120
116 end
Something went wrong with that request. Please try again.