avatar 0.0.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/{README.txt → README.rdoc} +12 -4
- data/init.rb +5 -0
- data/lib/avatar.rb +3 -2
- data/lib/avatar/source/pavatar_source.rb +109 -0
- data/lib/avatar/source/wrapper/rails_asset_source_wrapper.rb +3 -1
- data/lib/avatar/version.rb +2 -2
- data/rails/init.rb +1 -0
- metadata +28 -125
- data/.gitignore +0 -8
- data/Manifest.txt +0 -104
- data/Rakefile +0 -10
- data/config/hoe.rb +0 -70
- data/config/requirements.rb +0 -17
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/script/txt2html +0 -74
- data/setup.rb +0 -1585
- data/tasks/deployment.rake +0 -34
- data/tasks/environment.rake +0 -7
- data/tasks/testing.rake +0 -37
- data/tasks/website.rake +0 -17
- data/test/.gitignore +0 -3
- data/test/lib/database.rb +0 -2
- data/test/lib/database.yml +0 -10
- data/test/lib/file_column/CHANGELOG +0 -69
- data/test/lib/file_column/README +0 -54
- data/test/lib/file_column/Rakefile +0 -36
- data/test/lib/file_column/TODO +0 -6
- data/test/lib/file_column/init.rb +0 -13
- data/test/lib/file_column/lib/file_column.rb +0 -720
- data/test/lib/file_column/lib/file_column_helper.rb +0 -150
- data/test/lib/file_column/lib/file_compat.rb +0 -28
- data/test/lib/file_column/lib/magick_file_column.rb +0 -260
- data/test/lib/file_column/lib/rails_file_column.rb +0 -19
- data/test/lib/file_column/lib/test_case.rb +0 -124
- data/test/lib/file_column/lib/validations.rb +0 -112
- data/test/lib/file_column/test/abstract_unit.rb +0 -63
- data/test/lib/file_column/test/connection.rb +0 -17
- data/test/lib/file_column/test/file_column_helper_test.rb +0 -97
- data/test/lib/file_column/test/file_column_test.rb +0 -650
- data/test/lib/file_column/test/fixtures/entry.rb +0 -32
- data/test/lib/file_column/test/fixtures/invalid-image.jpg +0 -1
- data/test/lib/file_column/test/fixtures/kerb.jpg +0 -0
- data/test/lib/file_column/test/fixtures/mysql.sql +0 -25
- data/test/lib/file_column/test/fixtures/schema.rb +0 -10
- data/test/lib/file_column/test/fixtures/skanthak.png +0 -0
- data/test/lib/file_column/test/magick_test.rb +0 -380
- data/test/lib/file_column/test/magick_view_only_test.rb +0 -21
- data/test/lib/paperclip/README +0 -32
- data/test/lib/paperclip/Rakefile +0 -41
- data/test/lib/paperclip/generators/paperclip/USAGE +0 -5
- data/test/lib/paperclip/generators/paperclip/paperclip_generator.rb +0 -27
- data/test/lib/paperclip/generators/paperclip/templates/paperclip_migration.rb +0 -17
- data/test/lib/paperclip/init.rb +0 -3
- data/test/lib/paperclip/lib/paperclip.rb +0 -197
- data/test/lib/paperclip/lib/paperclip/attachment.rb +0 -230
- data/test/lib/paperclip/lib/paperclip/geometry.rb +0 -109
- data/test/lib/paperclip/lib/paperclip/iostream.rb +0 -43
- data/test/lib/paperclip/lib/paperclip/thumbnail.rb +0 -80
- data/test/lib/paperclip/lib/paperclip/upfile.rb +0 -32
- data/test/lib/paperclip/tasks/paperclip_tasks.rake +0 -38
- data/test/lib/paperclip/test/database.yml +0 -5
- data/test/lib/paperclip/test/fixtures/12k.png +0 -0
- data/test/lib/paperclip/test/fixtures/5k.png +0 -0
- data/test/lib/paperclip/test/fixtures/bad.png +0 -1
- data/test/lib/paperclip/test/helper.rb +0 -38
- data/test/lib/paperclip/test/test_attachment.rb +0 -99
- data/test/lib/paperclip/test/test_geometry.rb +0 -142
- data/test/lib/paperclip/test/test_integration.rb +0 -76
- data/test/lib/paperclip/test/test_iostream.rb +0 -60
- data/test/lib/paperclip/test/test_paperclip.rb +0 -83
- data/test/lib/paperclip/test/test_thumbnail.rb +0 -76
- data/test/lib/schema.rb +0 -20
- data/test/lib/user_suit.png +0 -0
- data/test/test_abstract_view_support.rb +0 -22
- data/test/test_action_view_support.rb +0 -30
- data/test/test_avatar.rb +0 -12
- data/test/test_file_column_source.rb +0 -38
- data/test/test_gravatar_source.rb +0 -79
- data/test/test_helper.rb +0 -27
- data/test/test_nil_source.rb +0 -18
- data/test/test_paperclip_source.rb +0 -52
- data/test/test_rails_asset_source_wrapper.rb +0 -37
- data/test/test_source_chain.rb +0 -44
- data/test/test_static_url_source.rb +0 -18
- data/test/test_string_substitution_source_wrapper.rb +0 -25
- data/website/index.html +0 -105
- data/website/index.txt +0 -48
- data/website/javascripts/rounded_corners_lite.inc.js +0 -285
- data/website/stylesheets/screen.css +0 -138
- data/website/template.rhtml +0 -48
@@ -1,21 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/abstract_unit'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/entry'
|
3
|
-
|
4
|
-
class RMagickViewOnlyTest < Test::Unit::TestCase
|
5
|
-
include FileColumnHelper
|
6
|
-
|
7
|
-
def setup
|
8
|
-
Entry.file_column :image
|
9
|
-
@request = RequestMock.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
FileUtils.rm_rf File.dirname(__FILE__)+"/public/entry/"
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_url_for_image_column_without_model_versions
|
17
|
-
e = Entry.new(:image => upload(f("skanthak.png")))
|
18
|
-
|
19
|
-
assert_nothing_raised { url_for_image_column e, "image", "50x50" }
|
20
|
-
end
|
21
|
-
end
|
data/test/lib/paperclip/README
DELETED
@@ -1,32 +0,0 @@
|
|
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 edit and new views:
|
16
|
-
|
17
|
-
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
|
18
|
-
<%= form.file_field :avatar %>
|
19
|
-
<% end %>
|
20
|
-
|
21
|
-
In your controller:
|
22
|
-
|
23
|
-
def create
|
24
|
-
@user = User.create( params[:user] )
|
25
|
-
end
|
26
|
-
|
27
|
-
In your show view:
|
28
|
-
|
29
|
-
<%= image_tag @user.avatar.url %>
|
30
|
-
<%= image_tag @user.avatar.url(:medium) %>
|
31
|
-
<%= image_tag @user.avatar.url(:thumb) %>
|
32
|
-
|
data/test/lib/paperclip/Rakefile
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
|
5
|
-
desc 'Default: run unit tests.'
|
6
|
-
task :default => [:clean, :test]
|
7
|
-
|
8
|
-
desc 'Test the paperclip plugin.'
|
9
|
-
Rake::TestTask.new(:test) do |t|
|
10
|
-
t.libs << 'lib' << 'profile'
|
11
|
-
t.pattern = 'test/**/test_*.rb'
|
12
|
-
t.verbose = true
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Start an IRB session with all necessary files required.'
|
16
|
-
task :shell do |t|
|
17
|
-
chdir File.dirname(__FILE__)
|
18
|
-
exec 'irb -I lib/ -I lib/paperclip -r rubygems -r active_record -r tempfile -r init'
|
19
|
-
end
|
20
|
-
|
21
|
-
desc 'Generate documentation for the paperclip plugin.'
|
22
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
|
-
rdoc.rdoc_dir = 'doc'
|
24
|
-
rdoc.title = 'Paperclip'
|
25
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
26
|
-
rdoc.rdoc_files.include('README')
|
27
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
-
end
|
29
|
-
|
30
|
-
desc 'Update documentation on website'
|
31
|
-
task :sync_docs => 'rdoc' do
|
32
|
-
`rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/paperclip`
|
33
|
-
end
|
34
|
-
|
35
|
-
desc 'Clean up files.'
|
36
|
-
task :clean do |t|
|
37
|
-
FileUtils.rm_rf "doc"
|
38
|
-
FileUtils.rm_rf "tmp"
|
39
|
-
FileUtils.rm "test/debug.log" rescue nil
|
40
|
-
FileUtils.rm "test/paperclip.db" rescue nil
|
41
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
class PaperclipGenerator < Rails::Generator::NamedBase
|
2
|
-
attr_accessor :attachments, :migration_name
|
3
|
-
|
4
|
-
def initialize(args, options = {})
|
5
|
-
super
|
6
|
-
@class_name, @attachments = args[0], args[1..-1]
|
7
|
-
end
|
8
|
-
|
9
|
-
def manifest
|
10
|
-
file_name = generate_file_name
|
11
|
-
@migration_name = file_name.camelize
|
12
|
-
record do |m|
|
13
|
-
m.migration_template "paperclip_migration.rb",
|
14
|
-
File.join('db', 'migrate'),
|
15
|
-
:migration_file_name => file_name
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def generate_file_name
|
22
|
-
names = attachments.map{|a| a.underscore }
|
23
|
-
names = names[0..-2] + ["and", names[-1]] if names.length > 1
|
24
|
-
"add_attachments_#{names.join("_")}_to_#{@class_name.underscore}"
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
@@ -1,17 +0,0 @@
|
|
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
|
data/test/lib/paperclip/init.rb
DELETED
@@ -1,197 +0,0 @@
|
|
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:: Distrbutes under the same terms as Ruby
|
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/attachment'
|
34
|
-
|
35
|
-
# The base module that gets included in ActiveRecord::Base.
|
36
|
-
module Paperclip
|
37
|
-
class << self
|
38
|
-
# Provides configurability to Paperclip. There are a number of options available, such as:
|
39
|
-
# * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of
|
40
|
-
# an uploaded image. Defaults to true.
|
41
|
-
# * image_magick_path: Defines the path at which to find the +convert+ and +identify+
|
42
|
-
# programs if they are not visible to Rails the system's search path. Defaults to
|
43
|
-
# nil, which uses the first executable found in the search path.
|
44
|
-
def options
|
45
|
-
@options ||= {
|
46
|
-
:whiny_thumbnails => true,
|
47
|
-
:image_magick_path => nil
|
48
|
-
}
|
49
|
-
end
|
50
|
-
|
51
|
-
def path_for_command command #:nodoc:
|
52
|
-
path = [options[:image_magick_path], command].compact
|
53
|
-
File.join(*path)
|
54
|
-
end
|
55
|
-
|
56
|
-
def included base #:nodoc:
|
57
|
-
base.extend ClassMethods
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
class PaperclipError < StandardError #:nodoc:
|
62
|
-
end
|
63
|
-
|
64
|
-
module ClassMethods
|
65
|
-
attr_reader :attachment_definitions
|
66
|
-
|
67
|
-
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
|
68
|
-
# is typically a file stored somewhere on the filesystem and has been uploaded by a user.
|
69
|
-
# The attribute returns a Paperclip::Attachment object which handles the management of
|
70
|
-
# that file. The intent is to make the attachment as much like a normal attribute. The
|
71
|
-
# thumbnails will be created when the new file is assigned, but they will *not* be saved
|
72
|
-
# until +save+ is called on the record. Likewise, if the attribute is set to +nil+ is
|
73
|
-
# called on it, the attachment will *not* be deleted until +save+ is called. See the
|
74
|
-
# Paperclip::Attachment documentation for more specifics. There are a number of options
|
75
|
-
# you can set to change the behavior of a Paperclip attachment:
|
76
|
-
# * +url+: The full URL of where the attachment is publically accessible. This can just
|
77
|
-
# as easily point to a directory served directly through Apache as it can to an action
|
78
|
-
# that can control permissions. You can specify the full domain and path, but usually
|
79
|
-
# just an absolute path is sufficient. The leading slash must be included manually for
|
80
|
-
# absolute paths. The default value is "/:class/:attachment/:id/:style_:filename". See
|
81
|
-
# Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
82
|
-
# :url => "/:attachment/:id/:style_:basename:extension"
|
83
|
-
# :url => "http://some.other.host/stuff/:class/:id_:extension"
|
84
|
-
# * +default_url+: The URL that will be returned if there is no attachment assigned.
|
85
|
-
# This field is interpolated just as the url is. The default value is
|
86
|
-
# "/:class/:attachment/missing_:style.png"
|
87
|
-
# has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png"
|
88
|
-
# User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
|
89
|
-
# * +styles+: A hash of thumbnail styles and their geometries. You can find more about
|
90
|
-
# geometry strings at the ImageMagick website
|
91
|
-
# (http://www.imagemagick.org/script/command-line-options.php#resize). Paperclip
|
92
|
-
# also adds the "#" option (e.g. "50x50#"), which will resize the image to fit maximally
|
93
|
-
# inside the dimensions and then crop the rest off (weighted at the center). The
|
94
|
-
# default value is to generate no thumbnails.
|
95
|
-
# * +default_style+: The thumbnail style that will be used by default URLs.
|
96
|
-
# Defaults to +original+.
|
97
|
-
# has_attached_file :avatar, :styles => { :normal => "100x100#" },
|
98
|
-
# :default_style => :normal
|
99
|
-
# user.avatar.url # => "/avatars/23/normal_me.png"
|
100
|
-
# * +path+: The location of the repository of attachments on disk. This can be coordinated
|
101
|
-
# with the value of the +url+ option to allow files to be saved into a place where Apache
|
102
|
-
# can serve them without hitting your app. Defaults to
|
103
|
-
# ":rails_root/public/:class/:attachment/:id/:style_:filename".
|
104
|
-
# By default this places the files in the app's public directory which can be served
|
105
|
-
# directly. If you are using capistrano for deployment, a good idea would be to
|
106
|
-
# make a symlink to the capistrano-created system directory from inside your app's
|
107
|
-
# public directory.
|
108
|
-
# See Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
109
|
-
# :path => "/var/app/attachments/:class/:id/:style/:filename"
|
110
|
-
# * +whiny_thumbnails+: Will raise an error if Paperclip cannot process thumbnails of an
|
111
|
-
# uploaded image. This will ovrride the global setting for this attachment.
|
112
|
-
# Defaults to true.
|
113
|
-
def has_attached_file name, options = {}
|
114
|
-
include InstanceMethods
|
115
|
-
|
116
|
-
@attachment_definitions ||= {}
|
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.nil?
|
133
|
-
end
|
134
|
-
|
135
|
-
validates_each(name) do |record, attr, value|
|
136
|
-
value.send(:flush_errors)
|
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
|
-
def validates_attachment_size name, options = {}
|
146
|
-
@attachment_definitions[name][:validations] << lambda do |attachment, instance|
|
147
|
-
unless options[:greater_than].nil?
|
148
|
-
options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
|
149
|
-
end
|
150
|
-
unless options[:less_than].nil?
|
151
|
-
options[:in] = (0..options[:less_than])
|
152
|
-
end
|
153
|
-
unless options[:in].include? instance[:"#{name}_file_size"].to_i
|
154
|
-
"file size is not between #{options[:in].first} and #{options[:in].last} bytes."
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
# Places ActiveRecord-style validations on the presence of a file.
|
160
|
-
def validates_attachment_presence name
|
161
|
-
@attachment_definitions[name][:validations] << lambda do |attachment, instance|
|
162
|
-
if attachment.file.nil? || !File.exist?(attachment.file.path)
|
163
|
-
"must be set."
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
end
|
169
|
-
|
170
|
-
module InstanceMethods #:nodoc:
|
171
|
-
def attachment_for name
|
172
|
-
@attachments ||= {}
|
173
|
-
@attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
|
174
|
-
end
|
175
|
-
|
176
|
-
def each_attachment
|
177
|
-
self.class.attachment_definitions.each do |name, definition|
|
178
|
-
yield(name, attachment_for(name))
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
def save_attached_files
|
183
|
-
each_attachment do |name, attachment|
|
184
|
-
attachment.send(:flush_writes)
|
185
|
-
attachment.send(:flush_deletes)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def destroy_attached_files
|
190
|
-
each_attachment do |name, attachment|
|
191
|
-
attachment.send(:queue_existing_for_delete)
|
192
|
-
attachment.send(:flush_deletes)
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
@@ -1,230 +0,0 @@
|
|
1
|
-
module Paperclip
|
2
|
-
# The Attachment class manages the files for a given attachment. It saves when the model saves,
|
3
|
-
# deletes when the model is destroyed, and processes the file upon assignment.
|
4
|
-
class Attachment
|
5
|
-
|
6
|
-
attr_reader :name, :instance, :file, :styles, :default_style
|
7
|
-
|
8
|
-
# Creates an Attachment object. +name+ is the name of the attachment, +instance+ is the
|
9
|
-
# ActiveRecord object instance it's attached to, and +options+ is the same as the hash
|
10
|
-
# passed to +has_attached_file+.
|
11
|
-
def initialize name, instance, options
|
12
|
-
@name = name
|
13
|
-
@instance = instance
|
14
|
-
@url = options[:url] ||
|
15
|
-
"/:attachment/:id/:style/:basename.:extension"
|
16
|
-
@path = options[:path] ||
|
17
|
-
":rails_root/public/:attachment/:id/:style/:basename.:extension"
|
18
|
-
@styles = options[:styles] || {}
|
19
|
-
@default_url = options[:default_url] || "/:attachment/:style/missing.png"
|
20
|
-
@validations = options[:validations] || []
|
21
|
-
@default_style = options[:default_style] || :original
|
22
|
-
@queued_for_delete = []
|
23
|
-
@processed_files = {}
|
24
|
-
@errors = []
|
25
|
-
@validation_errors = nil
|
26
|
-
@dirty = false
|
27
|
-
|
28
|
-
normalize_style_definition
|
29
|
-
|
30
|
-
@file = File.new(path) if original_filename && File.exists?(path)
|
31
|
-
end
|
32
|
-
|
33
|
-
# What gets called when you call instance.attachment = File. It clears errors,
|
34
|
-
# assigns attributes, processes the file, and runs validations. It also queues up
|
35
|
-
# the previous file for deletion, to be flushed away on #save of its host.
|
36
|
-
def assign uploaded_file
|
37
|
-
queue_existing_for_delete
|
38
|
-
@errors = []
|
39
|
-
@validation_errors = nil
|
40
|
-
|
41
|
-
return nil unless valid_file?(uploaded_file)
|
42
|
-
|
43
|
-
@file = uploaded_file.to_tempfile
|
44
|
-
@instance[:"#{@name}_file_name"] = uploaded_file.original_filename
|
45
|
-
@instance[:"#{@name}_content_type"] = uploaded_file.content_type
|
46
|
-
@instance[:"#{@name}_file_size"] = uploaded_file.size
|
47
|
-
|
48
|
-
@dirty = true
|
49
|
-
|
50
|
-
post_process
|
51
|
-
ensure
|
52
|
-
validate
|
53
|
-
end
|
54
|
-
|
55
|
-
# Returns the public URL of the attachment, with a given style. Note that this
|
56
|
-
# does not necessarily need to point to a file that your web server can access
|
57
|
-
# and can point to an action in your app, if you need fine grained security.
|
58
|
-
# This is not recommended if you don't need the security, however, for
|
59
|
-
# performance reasons.
|
60
|
-
def url style = nil
|
61
|
-
@file ? interpolate(@url, style) : interpolate(@default_url, style)
|
62
|
-
end
|
63
|
-
|
64
|
-
# Alias to +url+
|
65
|
-
def to_s style = nil
|
66
|
-
url(style)
|
67
|
-
end
|
68
|
-
|
69
|
-
# Returns true if there are any errors on this attachment.
|
70
|
-
def valid?
|
71
|
-
errors.length == 0
|
72
|
-
end
|
73
|
-
|
74
|
-
# Returns an array containing the errors on this attachment.
|
75
|
-
def errors
|
76
|
-
@errors.compact.uniq
|
77
|
-
end
|
78
|
-
|
79
|
-
# Returns true if there are changes that need to be saved.
|
80
|
-
def dirty?
|
81
|
-
@dirty
|
82
|
-
end
|
83
|
-
|
84
|
-
# Saves the file, if there are no errors. If there are, it flushes them to
|
85
|
-
# the instance's errors and returns false, cancelling the save.
|
86
|
-
def save
|
87
|
-
if valid?
|
88
|
-
flush_deletes
|
89
|
-
flush_writes
|
90
|
-
true
|
91
|
-
else
|
92
|
-
flush_errors
|
93
|
-
false
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# Returns an +IO+ representing the data of the file assigned to the given
|
98
|
-
# style. Useful for streaming with +send_file+.
|
99
|
-
def to_io style = nil
|
100
|
-
begin
|
101
|
-
style ||= @default_style
|
102
|
-
@processed_files[style] || File.new(path(style))
|
103
|
-
rescue Errno::ENOENT
|
104
|
-
nil
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
# Returns the name of the file as originally assigned, and as lives in the
|
109
|
-
# <attachment>_file_name attribute of the model.
|
110
|
-
def original_filename
|
111
|
-
instance[:"#{name}_file_name"]
|
112
|
-
end
|
113
|
-
|
114
|
-
# A hash of procs that are run during the interpolation of a path or url.
|
115
|
-
# A variable of the format :name will be replaced with the return value of
|
116
|
-
# the proc named ":name". Each lambda takes the attachment and the current
|
117
|
-
# style as arguments. This hash can be added to with your own proc if
|
118
|
-
# necessary.
|
119
|
-
def self.interpolations
|
120
|
-
@interpolations ||= {
|
121
|
-
:rails_root => lambda{|attachment,style| RAILS_ROOT },
|
122
|
-
:class => lambda{|attachment,style| attachment.instance.class.to_s.downcase.pluralize },
|
123
|
-
:basename => lambda do |attachment,style|
|
124
|
-
attachment.original_filename.gsub(/\.(.*?)$/, "")
|
125
|
-
end,
|
126
|
-
:extension => lambda do |attachment,style|
|
127
|
-
((style = attachment.styles[style]) && style.last) ||
|
128
|
-
File.extname(attachment.original_filename).gsub(/^\.+/, "")
|
129
|
-
end,
|
130
|
-
:id => lambda{|attachment,style| attachment.instance.id },
|
131
|
-
:partition_id => lambda do |attachment, style|
|
132
|
-
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
|
133
|
-
end,
|
134
|
-
:attachment => lambda{|attachment,style| attachment.name.to_s.downcase.pluralize },
|
135
|
-
:style => lambda{|attachment,style| style || attachment.default_style },
|
136
|
-
}
|
137
|
-
end
|
138
|
-
|
139
|
-
private
|
140
|
-
|
141
|
-
def valid_file? file #:nodoc:
|
142
|
-
file.respond_to?(:original_filename) && file.respond_to?(:content_type)
|
143
|
-
end
|
144
|
-
|
145
|
-
def validate #:nodoc:
|
146
|
-
unless @validation_errors
|
147
|
-
@validation_errors = @validations.collect do |v|
|
148
|
-
v.call(self, instance)
|
149
|
-
end.flatten.compact.uniq
|
150
|
-
@errors += @validation_errors
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
def normalize_style_definition
|
155
|
-
@styles.each do |name, args|
|
156
|
-
dimensions, format = [args, nil].flatten[0..1]
|
157
|
-
format = nil if format == ""
|
158
|
-
@styles[name] = [dimensions, format]
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def post_process #:nodoc:
|
163
|
-
return nil if @file.nil?
|
164
|
-
@styles.each do |name, args|
|
165
|
-
begin
|
166
|
-
dimensions, format = args
|
167
|
-
@processed_files[name] = Thumbnail.make(self.file,
|
168
|
-
dimensions,
|
169
|
-
format,
|
170
|
-
@whiny_thumbnails)
|
171
|
-
rescue Errno::ENOENT => e
|
172
|
-
@errors << "could not be processed because the file does not exist."
|
173
|
-
rescue PaperclipError => e
|
174
|
-
@errors << e.message
|
175
|
-
end
|
176
|
-
end
|
177
|
-
@processed_files[:original] = @file
|
178
|
-
end
|
179
|
-
|
180
|
-
def interpolate pattern, style = nil #:nodoc:
|
181
|
-
style ||= @default_style
|
182
|
-
pattern = pattern.dup
|
183
|
-
self.class.interpolations.each do |tag, l|
|
184
|
-
pattern.gsub!(/:#{tag}/) do |match|
|
185
|
-
l.call( self, style )
|
186
|
-
end
|
187
|
-
end
|
188
|
-
pattern.gsub(%r{/+}, "/")
|
189
|
-
end
|
190
|
-
|
191
|
-
def path style = nil #:nodoc:
|
192
|
-
interpolate(@path, style)
|
193
|
-
end
|
194
|
-
|
195
|
-
def queue_existing_for_delete #:nodoc:
|
196
|
-
@queued_for_delete += @processed_files.values
|
197
|
-
@file = nil
|
198
|
-
@processed_files = {}
|
199
|
-
@instance[:"#{@name}_file_name"] = nil
|
200
|
-
@instance[:"#{@name}_content_type"] = nil
|
201
|
-
@instance[:"#{@name}_file_size"] = nil
|
202
|
-
end
|
203
|
-
|
204
|
-
def flush_errors #:nodoc:
|
205
|
-
@errors.each do |error|
|
206
|
-
instance.errors.add(name, error)
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
def flush_writes #:nodoc:
|
211
|
-
@processed_files.each do |style, file|
|
212
|
-
FileUtils.mkdir_p( File.dirname(path(style)) )
|
213
|
-
@processed_files[style] = file.stream_to(path(style)) unless file.path == path(style)
|
214
|
-
end
|
215
|
-
@file = @processed_files[@default_style]
|
216
|
-
end
|
217
|
-
|
218
|
-
def flush_deletes #:nodoc:
|
219
|
-
@queued_for_delete.compact.each do |file|
|
220
|
-
begin
|
221
|
-
FileUtils.rm(file.path)
|
222
|
-
rescue Errno::ENOENT => e
|
223
|
-
# ignore them
|
224
|
-
end
|
225
|
-
end
|
226
|
-
@queued_for_delete = []
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|