peterpunk-merb_paperclip 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +47 -0
- data/Rakefile +56 -0
- data/TODO +0 -0
- data/lib/generators/paperclip_generator.rb +53 -0
- data/lib/generators/templates/file_name.rb +17 -0
- data/lib/merb_paperclip.rb +19 -0
- data/lib/merb_paperclip/merbtasks.rb +38 -0
- data/lib/paperclip.rb +246 -0
- data/lib/paperclip/attachment.rb +287 -0
- data/lib/paperclip/geometry.rb +109 -0
- data/lib/paperclip/iostream.rb +47 -0
- data/lib/paperclip/storage.rb +204 -0
- data/lib/paperclip/thumbnail.rb +80 -0
- data/lib/paperclip/upfile.rb +37 -0
- data/merb_paperclip.gemspec +31 -0
- data/spec/merb_paperclip_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -0
- metadata +77 -0
data/README
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
=Paperclip
|
2
|
+
|
3
|
+
Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called. It manages validations based on size and presence, if required. It can transform its assigned image into thumbnails if needed, and the prerequisites are as simple as installing ImageMagick (which, for most modern Unix-based systems, is as easy as installing the right packages). Attached files are saved to the filesystem and referenced in the browser by an easily understandable specification, which has sensible and useful defaults.
|
4
|
+
|
5
|
+
See the documentation for the +has_attached_file+ method for options.
|
6
|
+
|
7
|
+
==Usage
|
8
|
+
|
9
|
+
In your model:
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
|
13
|
+
end
|
14
|
+
|
15
|
+
In your migrations:
|
16
|
+
|
17
|
+
class AddAvatarColumnsToUser < ActiveRecord::Migration
|
18
|
+
def self.up
|
19
|
+
add_column :users, :avatar_file_name, :string
|
20
|
+
add_column :users, :avatar_content_type, :string
|
21
|
+
add_column :users, :avatar_file_size, :integer
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.down
|
25
|
+
remove_column :users, :avatar_file_name
|
26
|
+
remove_column :users, :avatar_content_type
|
27
|
+
remove_column :users, :avatar_file_size
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
In your edit and new views:
|
32
|
+
|
33
|
+
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
|
34
|
+
<%= form.file_field :avatar %>
|
35
|
+
<% end %>
|
36
|
+
|
37
|
+
In your controller:
|
38
|
+
|
39
|
+
def create
|
40
|
+
@user = User.create( params[:user] )
|
41
|
+
end
|
42
|
+
|
43
|
+
In your show view:
|
44
|
+
|
45
|
+
<%= image_tag @user.avatar.url %>
|
46
|
+
<%= image_tag @user.avatar.url(:medium) %>
|
47
|
+
<%= image_tag @user.avatar.url(:thumb) %>
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'date'
|
5
|
+
require 'merb-core/version'
|
6
|
+
require 'merb-core/tasks/merb_rake_helper'
|
7
|
+
|
8
|
+
NAME = "merb_paperclip"
|
9
|
+
GEM_VERSION = "0.9.4"
|
10
|
+
AUTHOR = "Jeremy Durham"
|
11
|
+
EMAIL = "jeremydurham@gmail.com"
|
12
|
+
HOMEPAGE = "http://rubyforge.org/projects/merb_paperclip/"
|
13
|
+
SUMMARY = "A Merb plugin that is essentially a port of Jon Yurek's paperclip"
|
14
|
+
|
15
|
+
spec = Gem::Specification.new do |s|
|
16
|
+
s.rubyforge_project = 'merb'
|
17
|
+
s.name = NAME
|
18
|
+
s.version = GEM_VERSION
|
19
|
+
s.platform = Gem::Platform::RUBY
|
20
|
+
s.has_rdoc = true
|
21
|
+
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
22
|
+
s.summary = SUMMARY
|
23
|
+
s.description = s.summary
|
24
|
+
s.author = AUTHOR
|
25
|
+
s.email = EMAIL
|
26
|
+
s.homepage = HOMEPAGE
|
27
|
+
s.add_dependency('merb', '>= 0.9.4')
|
28
|
+
s.require_path = 'lib'
|
29
|
+
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
34
|
+
pkg.gem_spec = spec
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "install the plugin locally"
|
38
|
+
task :install => [:package] do
|
39
|
+
sh %{#{sudo} gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "create a gemspec file"
|
43
|
+
task :make_spec do
|
44
|
+
File.open("#{NAME}.gemspec", "w") do |file|
|
45
|
+
file.puts spec.to_ruby
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
namespace :jruby do
|
50
|
+
|
51
|
+
desc "Run :package and install the resulting .gem with jruby"
|
52
|
+
task :install => :package do
|
53
|
+
sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/TODO
ADDED
File without changes
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Merb::Generators
|
2
|
+
|
3
|
+
class PaperclipGenerator < NamespacedGenerator
|
4
|
+
|
5
|
+
def self.source_root
|
6
|
+
File.dirname(__FILE__) / 'templates'
|
7
|
+
end
|
8
|
+
|
9
|
+
desc <<-DESC
|
10
|
+
Generators a paperclip migration
|
11
|
+
DESC
|
12
|
+
|
13
|
+
first_argument :name, :required => true, :desc => "model name"
|
14
|
+
second_argument :attachments, :required => true, :as => :array, :default => [], :desc => "space separated list of fields"
|
15
|
+
|
16
|
+
template :paperclip do
|
17
|
+
source(File.dirname(__FILE__) / 'templates' / 'file_name.rb')
|
18
|
+
destination("schema/migrations/#{migration_file_name}.rb")
|
19
|
+
end
|
20
|
+
|
21
|
+
def version
|
22
|
+
format("%03d", current_migration_nr + 1)
|
23
|
+
end
|
24
|
+
|
25
|
+
def migration_file_name
|
26
|
+
names = migration_attachments
|
27
|
+
"#{version}_add_attachments_#{names.join("_")}_to_#{class_name.underscore}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_name
|
31
|
+
names = migration_attachments
|
32
|
+
"add_attachments_#{names.join("_")}_to_#{class_name.underscore}".classify
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def migration_attachments
|
38
|
+
names = attachments.map(&:underscore)
|
39
|
+
attachments.length == 1 ? names : names[0..-2] + ["and", names[-1]]
|
40
|
+
end
|
41
|
+
|
42
|
+
def destination_directory
|
43
|
+
File.join(destination_root, 'schema', 'migrations')
|
44
|
+
end
|
45
|
+
|
46
|
+
def current_migration_nr
|
47
|
+
Dir["#{destination_directory}/*"].map{|f| File.basename(f).match(/^(\d+)/)[0].to_i }.max.to_i
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
add :paperclip, PaperclipGenerator
|
53
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
<% attachments.each do |attachment| -%>
|
4
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
|
5
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
|
6
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
|
7
|
+
<% end -%>
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.down
|
11
|
+
<% attachments.each do |attachment| -%>
|
12
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
|
13
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
|
14
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
|
15
|
+
<% end -%>
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# make sure we're running inside Merb
|
2
|
+
if defined?(Merb::Plugins)
|
3
|
+
dependency "activerecord"
|
4
|
+
|
5
|
+
# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
|
6
|
+
Merb::Plugins.config[:merb_paperclip] = {
|
7
|
+
:chickens => false
|
8
|
+
}
|
9
|
+
|
10
|
+
Merb::BootLoader.before_app_loads do
|
11
|
+
require File.join(File.dirname(__FILE__), "paperclip")
|
12
|
+
Merb.add_generators(File.join(File.dirname(__FILE__), 'generators', 'paperclip_generator'))
|
13
|
+
end
|
14
|
+
|
15
|
+
Merb::BootLoader.after_app_loads do
|
16
|
+
end
|
17
|
+
|
18
|
+
Merb::Plugins.add_rakefiles "merb_paperclip/merbtasks"
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
def obtain_class
|
2
|
+
class_name = ENV['CLASS'] || ENV['class']
|
3
|
+
raise "Must specify CLASS" unless class_name
|
4
|
+
@klass = Object.const_get(class_name)
|
5
|
+
end
|
6
|
+
|
7
|
+
def obtain_attachments
|
8
|
+
name = ENV['ATTACHMENT'] || ENV['attachment']
|
9
|
+
raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_definitions)
|
10
|
+
if !name.blank? && @klass.attachment_definitions.keys.include?(name)
|
11
|
+
[ name ]
|
12
|
+
else
|
13
|
+
@klass.attachment_definitions.keys
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :paperclip do
|
18
|
+
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
|
19
|
+
task :refresh => :environment do
|
20
|
+
klass = obtain_class
|
21
|
+
names = obtain_attachments
|
22
|
+
instances = klass.find(:all)
|
23
|
+
|
24
|
+
puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
|
25
|
+
instances.each do |instance|
|
26
|
+
names.each do |name|
|
27
|
+
result = if instance.send("#{ name }?")
|
28
|
+
instance.send(name).reprocess!
|
29
|
+
instance.send(name).save
|
30
|
+
else
|
31
|
+
true
|
32
|
+
end
|
33
|
+
print result ? "." : "x"; $stdout.flush
|
34
|
+
end
|
35
|
+
end
|
36
|
+
puts " Done."
|
37
|
+
end
|
38
|
+
end
|
data/lib/paperclip.rb
ADDED
@@ -0,0 +1,246 @@
|
|
1
|
+
# Paperclip allows file attachments that are stored in the filesystem. All graphical
|
2
|
+
# transformations are done using the Graphics/ImageMagick command line utilities and
|
3
|
+
# are stored in Tempfiles until the record is saved. Paperclip does not require a
|
4
|
+
# separate model for storing the attachment's information, instead adding a few simple
|
5
|
+
# columns to your table.
|
6
|
+
#
|
7
|
+
# Author:: Jon Yurek
|
8
|
+
# Copyright:: Copyright (c) 2008 thoughtbot, inc.
|
9
|
+
# License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
|
10
|
+
#
|
11
|
+
# Paperclip defines an attachment as any file, though it makes special considerations
|
12
|
+
# for image files. You can declare that a model has an attached file with the
|
13
|
+
# +has_attached_file+ method:
|
14
|
+
#
|
15
|
+
# class User < ActiveRecord::Base
|
16
|
+
# has_attached_file :avatar, :styles => { :thumb => "100x100" }
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# user = User.new
|
20
|
+
# user.avatar = params[:user][:avatar]
|
21
|
+
# user.avatar.url
|
22
|
+
# # => "/users/avatars/4/original_me.jpg"
|
23
|
+
# user.avatar.url(:thumb)
|
24
|
+
# # => "/users/avatars/4/thumb_me.jpg"
|
25
|
+
#
|
26
|
+
# See the +has_attached_file+ documentation for more details.
|
27
|
+
|
28
|
+
require 'tempfile'
|
29
|
+
require 'paperclip/upfile'
|
30
|
+
require 'paperclip/iostream'
|
31
|
+
require 'paperclip/geometry'
|
32
|
+
require 'paperclip/thumbnail'
|
33
|
+
require 'paperclip/storage'
|
34
|
+
require 'paperclip/attachment'
|
35
|
+
|
36
|
+
# The base module that gets included in ActiveRecord::Base. See the
|
37
|
+
# documentation for Paperclip::ClassMethods for more useful information.
|
38
|
+
module Paperclip
|
39
|
+
|
40
|
+
VERSION = "2.1.2"
|
41
|
+
|
42
|
+
class << self
|
43
|
+
# Provides configurability to Paperclip. There are a number of options available, such as:
|
44
|
+
# * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of
|
45
|
+
# an uploaded image. Defaults to true.
|
46
|
+
# * image_magick_path: Defines the path at which to find the +convert+ and +identify+
|
47
|
+
# programs if they are not visible to Merb the system's search path. Defaults to
|
48
|
+
# nil, which uses the first executable found in the search path.
|
49
|
+
def options
|
50
|
+
@options ||= {
|
51
|
+
:whiny_thumbnails => true,
|
52
|
+
:image_magick_path => nil
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def path_for_command command #:nodoc:
|
57
|
+
path = [options[:image_magick_path], command].compact
|
58
|
+
File.join(*path)
|
59
|
+
end
|
60
|
+
|
61
|
+
def included base #:nodoc:
|
62
|
+
base.extend ClassMethods
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class PaperclipError < StandardError #:nodoc:
|
67
|
+
end
|
68
|
+
|
69
|
+
class NotIdentifiedByImageMagickError < PaperclipError #:nodoc:
|
70
|
+
end
|
71
|
+
|
72
|
+
module ClassMethods
|
73
|
+
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
|
74
|
+
# is typically a file stored somewhere on the filesystem and has been uploaded by a user.
|
75
|
+
# The attribute returns a Paperclip::Attachment object which handles the management of
|
76
|
+
# that file. The intent is to make the attachment as much like a normal attribute. The
|
77
|
+
# thumbnails will be created when the new file is assigned, but they will *not* be saved
|
78
|
+
# until +save+ is called on the record. Likewise, if the attribute is set to +nil+ is
|
79
|
+
# called on it, the attachment will *not* be deleted until +save+ is called. See the
|
80
|
+
# Paperclip::Attachment documentation for more specifics. There are a number of options
|
81
|
+
# you can set to change the behavior of a Paperclip attachment:
|
82
|
+
# * +url+: The full URL of where the attachment is publically accessible. This can just
|
83
|
+
# as easily point to a directory served directly through Apache as it can to an action
|
84
|
+
# that can control permissions. You can specify the full domain and path, but usually
|
85
|
+
# just an absolute path is sufficient. The leading slash must be included manually for
|
86
|
+
# absolute paths. The default value is "/:class/:attachment/:id/:style_:filename". See
|
87
|
+
# Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
88
|
+
# :url => "/:attachment/:id/:style_:basename:extension"
|
89
|
+
# :url => "http://some.other.host/stuff/:class/:id_:extension"
|
90
|
+
# * +default_url+: The URL that will be returned if there is no attachment assigned.
|
91
|
+
# This field is interpolated just as the url is. The default value is
|
92
|
+
# "/:class/:attachment/missing_:style.png"
|
93
|
+
# has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png"
|
94
|
+
# User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
|
95
|
+
# * +styles+: A hash of thumbnail styles and their geometries. You can find more about
|
96
|
+
# geometry strings at the ImageMagick website
|
97
|
+
# (http://www.imagemagick.org/script/command-line-options.php#resize). Paperclip
|
98
|
+
# also adds the "#" option (e.g. "50x50#"), which will resize the image to fit maximally
|
99
|
+
# inside the dimensions and then crop the rest off (weighted at the center). The
|
100
|
+
# default value is to generate no thumbnails.
|
101
|
+
# * +default_style+: The thumbnail style that will be used by default URLs.
|
102
|
+
# Defaults to +original+.
|
103
|
+
# has_attached_file :avatar, :styles => { :normal => "100x100#" },
|
104
|
+
# :default_style => :normal
|
105
|
+
# user.avatar.url # => "/avatars/23/normal_me.png"
|
106
|
+
# * +whiny_thumbnails+: Will raise an error if Paperclip cannot process thumbnails of an
|
107
|
+
# uploaded image. This will ovrride the global setting for this attachment.
|
108
|
+
# Defaults to true.
|
109
|
+
# * +storage+: Chooses the storage backend where the files will be stored. The current
|
110
|
+
# choices are :filesystem and :s3. The default is :filesystem. Make sure you read the
|
111
|
+
# documentation for Paperclip::Storage::Filesystem and Paperclip::Storage::S3
|
112
|
+
# for backend-specific options.
|
113
|
+
def has_attached_file name, options = {}
|
114
|
+
include InstanceMethods
|
115
|
+
|
116
|
+
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
|
117
|
+
attachment_definitions[name] = {:validations => []}.merge(options)
|
118
|
+
|
119
|
+
after_save :save_attached_files
|
120
|
+
before_destroy :destroy_attached_files
|
121
|
+
|
122
|
+
define_method name do |*args|
|
123
|
+
a = attachment_for(name)
|
124
|
+
(args.length > 0) ? a.to_s(args.first) : a
|
125
|
+
end
|
126
|
+
|
127
|
+
define_method "#{name}=" do |file|
|
128
|
+
attachment_for(name).assign(file)
|
129
|
+
end
|
130
|
+
|
131
|
+
define_method "#{name}?" do
|
132
|
+
attachment_for(name).file?
|
133
|
+
end
|
134
|
+
|
135
|
+
validates_each(name) do |record, attr, value|
|
136
|
+
value.send(:flush_errors) unless value.valid?
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Places ActiveRecord-style validations on the size of the file assigned. The
|
141
|
+
# possible options are:
|
142
|
+
# * +in+: a Range of bytes (i.e. +1..1.megabyte+),
|
143
|
+
# * +less_than+: equivalent to :in => 0..options[:less_than]
|
144
|
+
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
|
145
|
+
# * +message+: error message to display, use :min and :max as replacements
|
146
|
+
def validates_attachment_size name, options = {}
|
147
|
+
attachment_definitions[name][:validations] << lambda do |attachment, instance|
|
148
|
+
unless options[:greater_than].nil?
|
149
|
+
options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
|
150
|
+
end
|
151
|
+
unless options[:less_than].nil?
|
152
|
+
options[:in] = (0..options[:less_than])
|
153
|
+
end
|
154
|
+
|
155
|
+
if attachment.file? && !options[:in].include?(instance[:"#{name}_file_size"].to_i)
|
156
|
+
min = options[:in].first
|
157
|
+
max = options[:in].last
|
158
|
+
|
159
|
+
if options[:message]
|
160
|
+
options[:message].gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
|
161
|
+
else
|
162
|
+
"file size is not between #{min} and #{max} bytes."
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
|
169
|
+
def validates_attachment_thumbnails name, options = {}
|
170
|
+
attachment_definitions[name][:whiny_thumbnails] = true
|
171
|
+
end
|
172
|
+
|
173
|
+
# Places ActiveRecord-style validations on the presence of a file.
|
174
|
+
def validates_attachment_presence name, options = {}
|
175
|
+
attachment_definitions[name][:validations] << lambda do |attachment, instance|
|
176
|
+
unless attachment.file?
|
177
|
+
options[:message] || "must be set."
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Places ActiveRecord-style validations on the content type of the file assigned. The
|
183
|
+
# possible options are:
|
184
|
+
# * +content_type+: Allowed content types. Can be a single content type or an array.
|
185
|
+
# Each type can be a String or a Regexp. It should be noted that Internet Explorer uploads
|
186
|
+
# files with content_types that you may not expect. For example, JPEG images are given
|
187
|
+
# image/pjpeg and PNGs are image/x-png, so keep that in mind when determining how you match.
|
188
|
+
# Allows all by default.
|
189
|
+
# * +message+: The message to display when the uploaded file has an invalid content type.
|
190
|
+
def validates_attachment_content_type name, options = {}
|
191
|
+
attachment_definitions[name][:validations] << lambda do |attachment, instance|
|
192
|
+
valid_types = [options[:content_type]].flatten
|
193
|
+
|
194
|
+
unless attachment.original_filename.nil?
|
195
|
+
unless options[:content_type].blank?
|
196
|
+
content_type = instance[:"#{name}_content_type"]
|
197
|
+
unless valid_types.any?{|t| t === content_type }
|
198
|
+
options[:message] || "is not one of the allowed file types."
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# Returns the attachment definitions defined by each call to has_attached_file.
|
206
|
+
def attachment_definitions
|
207
|
+
read_inheritable_attribute(:attachment_definitions)
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
module InstanceMethods #:nodoc:
|
213
|
+
def attachment_for name
|
214
|
+
@attachments ||= {}
|
215
|
+
@attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
|
216
|
+
end
|
217
|
+
|
218
|
+
def each_attachment
|
219
|
+
self.class.attachment_definitions.each do |name, definition|
|
220
|
+
yield(name, attachment_for(name))
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def save_attached_files
|
225
|
+
logger.info("[paperclip] Saving attachments.")
|
226
|
+
each_attachment do |name, attachment|
|
227
|
+
attachment.send(:save)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def destroy_attached_files
|
232
|
+
logger.info("[paperclip] Deleting attachments.")
|
233
|
+
each_attachment do |name, attachment|
|
234
|
+
attachment.send(:queue_existing_for_delete)
|
235
|
+
attachment.send(:flush_deletes)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
# Set it all up.
|
243
|
+
if Object.const_defined?("ActiveRecord")
|
244
|
+
ActiveRecord::Base.send(:include, Paperclip)
|
245
|
+
File.send(:include, Paperclip::Upfile)
|
246
|
+
end
|