citrusbyte-milton 0.1.2 → 0.1.5
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/lib/milton/attachment.rb +17 -31
- data/lib/milton/is_uploadable.rb +0 -1
- data/spec/milton/attachment_spec.rb +5 -7
- data/spec/milton/is_resizeable_spec.rb +3 -7
- metadata +1 -1
data/lib/milton/attachment.rb
CHANGED
@@ -35,6 +35,7 @@ module Citrusbyte
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
# These get mixed in to your model when you use Milton
|
38
39
|
module InstanceMethods
|
39
40
|
# Sets the filename to the given filename (sanitizes the given filename
|
40
41
|
# as well)
|
@@ -108,6 +109,9 @@ module Citrusbyte
|
|
108
109
|
end
|
109
110
|
end
|
110
111
|
|
112
|
+
attr_accessor :filename
|
113
|
+
attr_accessor :attachment
|
114
|
+
|
111
115
|
# TODO: can probably fanagle a way to only pass a reference to the model
|
112
116
|
# and not need the filename (or better yet just the filename and
|
113
117
|
# decouple)
|
@@ -120,7 +124,7 @@ module Citrusbyte
|
|
120
124
|
# If no options are given then returns the path and filename to the
|
121
125
|
# original file.
|
122
126
|
def path(options={})
|
123
|
-
options.empty? ? File.join(dirname,
|
127
|
+
options.empty? ? File.join(dirname, filename) : Derivative.new(filename, options).path
|
124
128
|
end
|
125
129
|
|
126
130
|
# Returns the full directory path up to the file, w/o the filename.
|
@@ -136,7 +140,6 @@ module Citrusbyte
|
|
136
140
|
# Removes the file from the underlying file system and any derivatives of
|
137
141
|
# the file.
|
138
142
|
def destroy
|
139
|
-
destroy_derivatives
|
140
143
|
destroy_file
|
141
144
|
end
|
142
145
|
|
@@ -149,7 +152,7 @@ module Citrusbyte
|
|
149
152
|
# Returns the partitioned path segment based on the id of the model
|
150
153
|
# this file is attached to.
|
151
154
|
def partitioned_path
|
152
|
-
self.class.partition(
|
155
|
+
self.class.partition(self.attachment.id)
|
153
156
|
end
|
154
157
|
|
155
158
|
# The full path to the root of where files will be stored on disk.
|
@@ -162,30 +165,18 @@ module Citrusbyte
|
|
162
165
|
self.class.recreate_directory(dirname) unless File.exists?(dirname)
|
163
166
|
end
|
164
167
|
|
165
|
-
# Removes the
|
168
|
+
# Removes the containing directory from the filesystem (and hence the
|
169
|
+
# file and any derivatives)
|
166
170
|
def destroy_file
|
167
|
-
FileUtils.
|
171
|
+
FileUtils.rm_rf dirname if File.exists?(dirname)
|
168
172
|
end
|
169
|
-
|
170
|
-
# Derivatives of this Attachment ====================================
|
171
173
|
|
172
|
-
# Returns an array of
|
174
|
+
# Returns an array of Derivatives of this AttachableFile.
|
173
175
|
def derivatives
|
174
|
-
Dir.glob(
|
176
|
+
Dir.glob(dirname + '/*').reject{ |filename| filename == self.filename }.collect do |filename|
|
175
177
|
Derivative.from_filename(filename)
|
176
178
|
end
|
177
179
|
end
|
178
|
-
|
179
|
-
# Recreates the directory derivatives of this file will be stored in.
|
180
|
-
def recreate_derivative_directory
|
181
|
-
dirname = Derivative.dirname_for(path)
|
182
|
-
self.class.recreate_directory(dirname) unless File.exists?(dirname)
|
183
|
-
end
|
184
|
-
|
185
|
-
# Removes the derivatives folder for this file and all files within.
|
186
|
-
def destroy_derivatives
|
187
|
-
FileUtils.rm_rf dirname if File.exists?(dirname)
|
188
|
-
end
|
189
180
|
end
|
190
181
|
|
191
182
|
# Represents a file created on the file system that is a derivative of the
|
@@ -198,7 +189,7 @@ module Citrusbyte
|
|
198
189
|
# Files created as derivatives have their creation options appended into
|
199
190
|
# their filenames so it can be checked later if a file w/ the given
|
200
191
|
# options already exists (so as not to create it again).
|
201
|
-
#
|
192
|
+
#
|
202
193
|
class Derivative
|
203
194
|
attr_reader :options
|
204
195
|
|
@@ -231,29 +222,24 @@ module Citrusbyte
|
|
231
222
|
def from_filename(filename)
|
232
223
|
Derivative.new(filename, options_from(extract_options_from(filename)))
|
233
224
|
end
|
234
|
-
|
235
|
-
# Gives the path to where derivatives of this file are stored.
|
236
|
-
# Derivatives are any files which are based off of this file but are
|
237
|
-
# not Attachments themselves (i.e. thumbnails, transcoded copies,
|
238
|
-
# etc...)
|
239
|
-
def dirname_for(path)
|
240
|
-
File.join(File.dirname(path), File.basename(path, File.extname(path)))
|
241
|
-
end
|
242
225
|
end
|
243
226
|
|
227
|
+
# Instantiate a new Derivative, takes a reference to the AttachableFile
|
228
|
+
# (or specialization class) that this will be a derivative of, and a hash
|
229
|
+
# of the options defining the derivative.
|
244
230
|
def initialize(file, options)
|
245
231
|
@file = file
|
246
232
|
@options = options
|
247
233
|
end
|
248
234
|
|
249
|
-
# The filename of this Derivative with embedded options.
|
235
|
+
# The resulting filename of this Derivative with embedded options.
|
250
236
|
def filename
|
251
237
|
self.class.filename_for(@file.path, options)
|
252
238
|
end
|
253
239
|
|
254
240
|
# The full path and filename to this Derivative.
|
255
241
|
def path
|
256
|
-
File.join(
|
242
|
+
File.join(@file.dirname, filename)
|
257
243
|
end
|
258
244
|
|
259
245
|
# Returns true if the file resulting from this Derivative exists.
|
data/lib/milton/is_uploadable.rb
CHANGED
@@ -4,7 +4,6 @@ describe Attachment do
|
|
4
4
|
describe "being destroyed" do
|
5
5
|
before :each do
|
6
6
|
@attachment = Attachment.create :file => upload('milton.jpg')
|
7
|
-
@derivative_path = File.dirname(@attachment.path) + '/milton'
|
8
7
|
end
|
9
8
|
|
10
9
|
it "should delete the underlying file from the filesystem" do
|
@@ -12,13 +11,12 @@ describe Attachment do
|
|
12
11
|
File.exists?(@attachment.path).should be_false
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
it "should delete the derivative folder from the filesystem" do
|
14
|
+
# the partitioning algorithm ensures that each attachment model has its own
|
15
|
+
# folder, so we can safely delete the folder, if you write a new
|
16
|
+
# partitioner this might change!
|
17
|
+
it "should delete the directory containing the file and all derivatives from the filesystem" do
|
20
18
|
@attachment.destroy
|
21
|
-
File.exists?(@
|
19
|
+
File.exists?(File.dirname(@attachment.path)).should be_false
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
@@ -117,15 +117,11 @@ describe Citrusbyte::Milton::IsResizeable do
|
|
117
117
|
end
|
118
118
|
|
119
119
|
it "should use the partitioned path when grabbing the original file" do
|
120
|
-
@image.path.should =~
|
120
|
+
@image.path.should =~ /\/#{Citrusbyte::Milton::AttachableFile.partition(@image.id)}\/milton.jpg$/
|
121
121
|
end
|
122
122
|
|
123
|
-
it "should use the partitioned path
|
124
|
-
@image.path(:size => '10x10', :crop => true).should =~
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should append source filename to thumbnail path" do
|
128
|
-
@image.path(:size => '10x10').should =~ /\/milton\/milton.size=10x10.jpg$/
|
123
|
+
it "should use the partitioned path when grabbing a thubmnail" do
|
124
|
+
@image.path(:size => '10x10', :crop => true).should =~ /\/#{Citrusbyte::Milton::AttachableFile.partition(@image.id)}\/milton.crop=true_size=10x10.jpg$/
|
129
125
|
end
|
130
126
|
end
|
131
127
|
end
|