jmcnevin-paperclip 2.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README.md +414 -0
- data/Rakefile +86 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +4 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +33 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip.rb +480 -0
- data/lib/paperclip/attachment.rb +520 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +155 -0
- data/lib/paperclip/interpolations.rb +171 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers.rb +33 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/options.rb +78 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +26 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +163 -0
- data/lib/paperclip/storage/s3.rb +270 -0
- data/lib/paperclip/style.rb +95 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +62 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/tasks/paperclip.rake +101 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_test.rb +1161 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/animated.gif +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/double spaces in name.png +0 -0
- data/test/fixtures/fog.yml +8 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/spaced file.png +0 -0
- data/test/fixtures/text.txt +1 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/fog_test.rb +192 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +158 -0
- data/test/integration_test.rb +781 -0
- data/test/interpolations_test.rb +202 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/options_test.rb +75 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +340 -0
- data/test/processor_test.rb +10 -0
- data/test/storage/filesystem_test.rb +56 -0
- data/test/storage/s3_live_test.rb +88 -0
- data/test/storage/s3_test.rb +689 -0
- data/test/style_test.rb +180 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- metadata +294 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module CallbackCompatability
|
3
|
+
module Rails21
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(Defining)
|
6
|
+
base.send(:include, Running)
|
7
|
+
end
|
8
|
+
|
9
|
+
module Defining
|
10
|
+
def define_paperclip_callbacks(*args)
|
11
|
+
args.each do |callback|
|
12
|
+
define_callbacks("before_#{callback}")
|
13
|
+
define_callbacks("after_#{callback}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Running
|
19
|
+
def run_paperclip_callbacks(callback, opts = nil, &blk)
|
20
|
+
# The overall structure of this isn't ideal since after callbacks run even if
|
21
|
+
# befores return false. But this is how rails 3's callbacks work, unfortunately.
|
22
|
+
if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false
|
23
|
+
blk.call
|
24
|
+
end
|
25
|
+
run_callbacks(:"after_#{callback}"){ |result, object| result == false }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Rails3
|
31
|
+
def self.included(base)
|
32
|
+
base.extend(Defining)
|
33
|
+
base.send(:include, Running)
|
34
|
+
end
|
35
|
+
|
36
|
+
module Defining
|
37
|
+
def define_paperclip_callbacks(*callbacks)
|
38
|
+
define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
|
39
|
+
callbacks.each do |callback|
|
40
|
+
eval <<-end_callbacks
|
41
|
+
def before_#{callback}(*args, &blk)
|
42
|
+
set_callback(:#{callback}, :before, *args, &blk)
|
43
|
+
end
|
44
|
+
def after_#{callback}(*args, &blk)
|
45
|
+
set_callback(:#{callback}, :after, *args, &blk)
|
46
|
+
end
|
47
|
+
end_callbacks
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module Running
|
53
|
+
def run_paperclip_callbacks(callback, opts = nil, &block)
|
54
|
+
run_callbacks(callback, opts, &block)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
module Paperclip
|
2
|
+
|
3
|
+
# Defines the geometry of an image.
|
4
|
+
class Geometry
|
5
|
+
attr_accessor :height, :width, :modifier
|
6
|
+
|
7
|
+
# Gives a Geometry representing the given height and width
|
8
|
+
def initialize width = nil, height = nil, modifier = nil
|
9
|
+
@height = height.to_f
|
10
|
+
@width = width.to_f
|
11
|
+
@modifier = modifier
|
12
|
+
end
|
13
|
+
|
14
|
+
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
|
15
|
+
# File or path.
|
16
|
+
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
|
17
|
+
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
|
18
|
+
def self.from_file file
|
19
|
+
file_path = file.respond_to?(:path) ? file.path : file
|
20
|
+
raise(Paperclip::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
|
21
|
+
geometry = begin
|
22
|
+
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
|
23
|
+
rescue Cocaine::ExitStatusError
|
24
|
+
""
|
25
|
+
rescue Cocaine::CommandNotFoundError => e
|
26
|
+
raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
|
27
|
+
end
|
28
|
+
parse(geometry) ||
|
29
|
+
raise(NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
|
30
|
+
end
|
31
|
+
|
32
|
+
# Parses a "WxH" formatted string, where W is the width and H is the height.
|
33
|
+
def self.parse string
|
34
|
+
if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/i))
|
35
|
+
Geometry.new(*match[1,3])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# True if the dimensions represent a square
|
40
|
+
def square?
|
41
|
+
height == width
|
42
|
+
end
|
43
|
+
|
44
|
+
# True if the dimensions represent a horizontal rectangle
|
45
|
+
def horizontal?
|
46
|
+
height < width
|
47
|
+
end
|
48
|
+
|
49
|
+
# True if the dimensions represent a vertical rectangle
|
50
|
+
def vertical?
|
51
|
+
height > width
|
52
|
+
end
|
53
|
+
|
54
|
+
# The aspect ratio of the dimensions.
|
55
|
+
def aspect
|
56
|
+
width / height
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns the larger of the two dimensions
|
60
|
+
def larger
|
61
|
+
[height, width].max
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the smaller of the two dimensions
|
65
|
+
def smaller
|
66
|
+
[height, width].min
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns the width and height in a format suitable to be passed to Geometry.parse
|
70
|
+
def to_s
|
71
|
+
s = ""
|
72
|
+
s << width.to_i.to_s if width > 0
|
73
|
+
s << "x#{height.to_i}" if height > 0
|
74
|
+
s << modifier.to_s
|
75
|
+
s
|
76
|
+
end
|
77
|
+
|
78
|
+
# Same as to_s
|
79
|
+
def inspect
|
80
|
+
to_s
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns the scaling and cropping geometries (in string-based ImageMagick format)
|
84
|
+
# neccessary to transform this Geometry into the Geometry given. If crop is true,
|
85
|
+
# then it is assumed the destination Geometry will be the exact final resolution.
|
86
|
+
# In this case, the source Geometry is scaled so that an image containing the
|
87
|
+
# destination Geometry would be completely filled by the source image, and any
|
88
|
+
# overhanging image would be cropped. Useful for square thumbnail images. The cropping
|
89
|
+
# is weighted at the center of the Geometry.
|
90
|
+
def transformation_to dst, crop = false
|
91
|
+
if crop
|
92
|
+
ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
|
93
|
+
scale_geometry, scale = scaling(dst, ratio)
|
94
|
+
crop_geometry = cropping(dst, ratio, scale)
|
95
|
+
else
|
96
|
+
scale_geometry = dst.to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
[ scale_geometry, crop_geometry ]
|
100
|
+
end
|
101
|
+
|
102
|
+
# Adapted from attachment_fu.
|
103
|
+
# Attempts to get new dimensions for the current geometry string given these old dimensions.
|
104
|
+
# This doesn't implement the aspect flag (!) or the area flag (@). PDI
|
105
|
+
def new_dimensions_for orig_width, orig_height
|
106
|
+
new_width = orig_width
|
107
|
+
new_height = orig_height
|
108
|
+
|
109
|
+
case self.modifier
|
110
|
+
when '#'
|
111
|
+
new_width = self.width
|
112
|
+
new_height = self.height
|
113
|
+
when '%'
|
114
|
+
scale_x = self.width.zero? ? 100 : self.width
|
115
|
+
scale_y = self.height.zero? ? self.width : self.height
|
116
|
+
new_width = scale_x.to_f * (orig_width.to_f / 100.0)
|
117
|
+
new_height = scale_y.to_f * (orig_height.to_f / 100.0)
|
118
|
+
when '<', '>', nil
|
119
|
+
scale_factor =
|
120
|
+
if new_width.zero? || new_height.zero?
|
121
|
+
1.0
|
122
|
+
else
|
123
|
+
if self.width.nonzero? && self.height.nonzero?
|
124
|
+
[self.width.to_f / new_width.to_f, self.height.to_f / new_height.to_f].min
|
125
|
+
else
|
126
|
+
self.width.nonzero? ? (self.width.to_f / new_width.to_f) : (self.height.to_f / new_height.to_f)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
new_width = scale_factor * new_width.to_f
|
130
|
+
new_height = scale_factor * new_height.to_f
|
131
|
+
new_width = orig_width if self.modifier && new_width.send(self.modifier, orig_width)
|
132
|
+
new_height = orig_height if self.modifier && new_height.send(self.modifier, orig_height)
|
133
|
+
end
|
134
|
+
[new_width, new_height].collect! { |v| [v.round, 1].max }
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def scaling dst, ratio
|
140
|
+
if ratio.horizontal? || ratio.square?
|
141
|
+
[ "%dx" % dst.width, ratio.width ]
|
142
|
+
else
|
143
|
+
[ "x%d" % dst.height, ratio.height ]
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def cropping dst, ratio, scale
|
148
|
+
if ratio.horizontal? || ratio.square?
|
149
|
+
"%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
|
150
|
+
else
|
151
|
+
"%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
module Paperclip
|
2
|
+
# This module contains all the methods that are available for interpolation
|
3
|
+
# in paths and urls. To add your own (or override an existing one), you
|
4
|
+
# can either open this module and define it, or call the
|
5
|
+
# Paperclip.interpolates method.
|
6
|
+
module Interpolations
|
7
|
+
extend self
|
8
|
+
|
9
|
+
# Hash assignment of interpolations. Included only for compatibility,
|
10
|
+
# and is not intended for normal use.
|
11
|
+
def self.[]= name, block
|
12
|
+
define_method(name, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Hash access of interpolations. Included only for compatibility,
|
16
|
+
# and is not intended for normal use.
|
17
|
+
def self.[] name
|
18
|
+
method(name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a sorted list of all interpolations.
|
22
|
+
def self.all
|
23
|
+
self.instance_methods(false).sort
|
24
|
+
end
|
25
|
+
|
26
|
+
# Perform the actual interpolation. Takes the pattern to interpolate
|
27
|
+
# and the arguments to pass, which are the attachment and style name.
|
28
|
+
# You can pass a method name on your record as a symbol, which should turn
|
29
|
+
# an interpolation pattern for Paperclip to use.
|
30
|
+
def self.interpolate pattern, *args
|
31
|
+
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
|
32
|
+
all.reverse.inject(pattern) do |result, tag|
|
33
|
+
result.gsub(/:#{tag}/) do |match|
|
34
|
+
send( tag, *args )
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns the filename, the same way as ":basename.:extension" would.
|
40
|
+
def filename attachment, style_name
|
41
|
+
[ basename(attachment, style_name), extension(attachment, style_name) ].reject(&:blank?).join(".")
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the interpolated URL. Will raise an error if the url itself
|
45
|
+
# contains ":url" to prevent infinite recursion. This interpolation
|
46
|
+
# is used in the default :path to ease default specifications.
|
47
|
+
RIGHT_HERE = "#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}"
|
48
|
+
def url attachment, style_name
|
49
|
+
raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
|
50
|
+
attachment.url(style_name, :timestamp => false, :escape => false)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns the timestamp as defined by the <attachment>_updated_at field
|
54
|
+
# in the server default time zone unless :use_global_time_zone is set
|
55
|
+
# to false. Note that a Rails.config.time_zone change will still
|
56
|
+
# invalidate any path or URL that uses :timestamp. For a
|
57
|
+
# time_zone-agnostic timestamp, use #updated_at.
|
58
|
+
def timestamp attachment, style_name
|
59
|
+
attachment.instance_read(:updated_at).in_time_zone(attachment.time_zone).to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns an integer timestamp that is time zone-neutral, so that paths
|
63
|
+
# remain valid even if a server's time zone changes.
|
64
|
+
def updated_at attachment, style_name
|
65
|
+
attachment.updated_at
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns the Rails.root constant.
|
69
|
+
def rails_root attachment, style_name
|
70
|
+
Rails.root
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the Rails.env constant.
|
74
|
+
def rails_env attachment, style_name
|
75
|
+
Rails.env
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns the underscored, pluralized version of the class name.
|
79
|
+
# e.g. "users" for the User class.
|
80
|
+
# NOTE: The arguments need to be optional, because some tools fetch
|
81
|
+
# all class names. Calling #class will return the expected class.
|
82
|
+
def class attachment = nil, style_name = nil
|
83
|
+
return super() if attachment.nil? && style_name.nil?
|
84
|
+
attachment.instance.class.to_s.underscore.pluralize
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns the basename of the file. e.g. "file" for "file.jpg"
|
88
|
+
def basename attachment, style_name
|
89
|
+
attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}$/, "")
|
90
|
+
end
|
91
|
+
|
92
|
+
# Returns the extension of the file. e.g. "jpg" for "file.jpg"
|
93
|
+
# If the style has a format defined, it will return the format instead
|
94
|
+
# of the actual extension.
|
95
|
+
def extension attachment, style_name
|
96
|
+
((style = attachment.styles[style_name]) && style[:format]) ||
|
97
|
+
File.extname(attachment.original_filename).gsub(/^\.+/, "")
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns an extension based on the content type. e.g. "jpeg" for "image/jpeg".
|
101
|
+
# Each mime type generally has multiple extensions associated with it, so
|
102
|
+
# if the extension from teh original filename is one of these extensions,
|
103
|
+
# that extension is used, otherwise, the first in the list is used.
|
104
|
+
def content_type_extension attachment, style_name
|
105
|
+
mime_type = MIME::Types[attachment.content_type]
|
106
|
+
extensions_for_mime_type = unless mime_type.empty?
|
107
|
+
mime_type.first.extensions
|
108
|
+
else
|
109
|
+
[]
|
110
|
+
end
|
111
|
+
|
112
|
+
original_extension = extension(attachment, style_name)
|
113
|
+
if extensions_for_mime_type.include? original_extension
|
114
|
+
original_extension
|
115
|
+
elsif !extensions_for_mime_type.empty?
|
116
|
+
extensions_for_mime_type.first
|
117
|
+
else
|
118
|
+
# It's possible, though unlikely, that the mime type is not in the
|
119
|
+
# database, so just use the part after the '/' in the mime type as the
|
120
|
+
# extension.
|
121
|
+
%r{/([^/]*)$}.match(attachment.content_type)[1]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns the id of the instance.
|
126
|
+
def id attachment, style_name
|
127
|
+
attachment.instance.id
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns the #to_param of the instance.
|
131
|
+
def param attachment, style_name
|
132
|
+
attachment.instance.to_param
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the fingerprint of the instance.
|
136
|
+
def fingerprint attachment, style_name
|
137
|
+
attachment.fingerprint
|
138
|
+
end
|
139
|
+
|
140
|
+
# Returns a the attachment hash. See Paperclip::Attachment#hash for
|
141
|
+
# more details.
|
142
|
+
def hash attachment=nil, style_name=nil
|
143
|
+
if attachment && style_name
|
144
|
+
attachment.hash(style_name)
|
145
|
+
else
|
146
|
+
super()
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns the id of the instance in a split path form. e.g. returns
|
151
|
+
# 000/001/234 for an id of 1234.
|
152
|
+
def id_partition attachment, style_name
|
153
|
+
if (id = attachment.instance.id).is_a?(Integer)
|
154
|
+
("%09d" % id).scan(/\d{3}/).join("/")
|
155
|
+
else
|
156
|
+
id.scan(/.{3}/).first(3).join("/")
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns the pluralized form of the attachment name. e.g.
|
161
|
+
# "avatars" for an attachment of :avatar
|
162
|
+
def attachment attachment, style_name
|
163
|
+
attachment.name.to_s.downcase.pluralize
|
164
|
+
end
|
165
|
+
|
166
|
+
# Returns the style, or the default style if nil is supplied.
|
167
|
+
def style attachment, style_name
|
168
|
+
style_name || attachment.default_style
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
|
2
|
+
# and Tempfile conversion.
|
3
|
+
module IOStream
|
4
|
+
# Returns a Tempfile containing the contents of the readable object.
|
5
|
+
def to_tempfile(object)
|
6
|
+
return object.to_tempfile if object.respond_to?(:to_tempfile)
|
7
|
+
name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
|
8
|
+
tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)])
|
9
|
+
tempfile.binmode
|
10
|
+
stream_to(object, tempfile)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Copies one read-able object from one place to another in blocks, obviating the need to load
|
14
|
+
# the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
|
15
|
+
# in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
|
16
|
+
def stream_to object, path_or_file, in_blocks_of = 8192
|
17
|
+
dstio = case path_or_file
|
18
|
+
when String then File.new(path_or_file, "wb+")
|
19
|
+
when IO then path_or_file
|
20
|
+
when Tempfile then path_or_file
|
21
|
+
end
|
22
|
+
buffer = ""
|
23
|
+
object.rewind
|
24
|
+
while object.read(in_blocks_of, buffer) do
|
25
|
+
dstio.write(buffer)
|
26
|
+
end
|
27
|
+
dstio.rewind
|
28
|
+
dstio
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Corrects a bug in Windows when asking for Tempfile size.
|
33
|
+
if defined?(Tempfile) && RUBY_PLATFORM !~ /java/
|
34
|
+
class Tempfile
|
35
|
+
def size
|
36
|
+
if @tmpfile
|
37
|
+
@tmpfile.fsync
|
38
|
+
@tmpfile.flush
|
39
|
+
@tmpfile.stat.size
|
40
|
+
else
|
41
|
+
0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'paperclip/matchers/have_attached_file_matcher'
|
2
|
+
require 'paperclip/matchers/validate_attachment_presence_matcher'
|
3
|
+
require 'paperclip/matchers/validate_attachment_content_type_matcher'
|
4
|
+
require 'paperclip/matchers/validate_attachment_size_matcher'
|
5
|
+
|
6
|
+
module Paperclip
|
7
|
+
module Shoulda
|
8
|
+
# Provides rspec-compatible matchers for testing Paperclip attachments.
|
9
|
+
#
|
10
|
+
# In spec_helper.rb, you'll need to require the matchers:
|
11
|
+
#
|
12
|
+
# require "paperclip/matchers"
|
13
|
+
#
|
14
|
+
# And include the module:
|
15
|
+
#
|
16
|
+
# Spec::Runner.configure do |config|
|
17
|
+
# config.include Paperclip::Shoulda::Matchers
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# describe User do
|
22
|
+
# it { should have_attached_file(:avatar) }
|
23
|
+
# it { should validate_attachment_presence(:avatar) }
|
24
|
+
# it { should validate_attachment_content_type(:avatar).
|
25
|
+
# allowing('image/png', 'image/gif').
|
26
|
+
# rejecting('text/plain', 'text/xml') }
|
27
|
+
# it { should validate_attachment_size(:avatar).
|
28
|
+
# less_than(2.megabytes) }
|
29
|
+
# end
|
30
|
+
module Matchers
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|