Skip to content
This repository
Browse code

Moved to send instead of [], so no columns specifically required, onl…

…y methods.
  • Loading branch information...
commit ff6e8485a463dc980c586e101b330399a9051031 1 parent f301de5
Jon Yurek jyurek authored
4 lib/paperclip.rb
@@ -182,7 +182,7 @@ def validates_attachment_size name, options = {}
182 182 options[:in] = (0..options[:less_than])
183 183 end
184 184
185   - if attachment.file? && !options[:in].include?(instance[:"#{name}_file_size"].to_i)
  185 + if attachment.file? && !options[:in].include?(attachment.instance_read(:file_size).to_i)
186 186 min = options[:in].first
187 187 max = options[:in].last
188 188
@@ -223,7 +223,7 @@ def validates_attachment_content_type name, options = {}
223 223
224 224 unless attachment.original_filename.blank?
225 225 unless options[:content_type].blank?
226   - content_type = instance[:"#{name}_content_type"]
  226 + content_type = attachment.instance_read(:content_type)
227 227 unless valid_types.any?{|t| t === content_type }
228 228 options[:message] || "is not one of the allowed file types."
229 229 end
32 lib/paperclip/attachment.rb
@@ -56,7 +56,7 @@ def initialize name, instance, options = {}
56 56 def assign uploaded_file
57 57 %w(file_name).each do |field|
58 58 unless @instance.class.column_names.include?("#{name}_#{field}")
59   - raise PaperclipError.new("#{self} model does not have required column '#{name}_#{field}'")
  59 + raise PaperclipError.new("#{@instance.class} model does not have required column '#{name}_#{field}'")
60 60 end
61 61 end
62 62
@@ -75,17 +75,17 @@ def assign uploaded_file
75 75
76 76 logger.info("[paperclip] Writing attributes for #{name}")
77 77 @queued_for_write[:original] = uploaded_file.to_tempfile
78   - @instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip.gsub /[^\w\d\.\-]+/, '_'
79   - @instance[:"#{@name}_content_type"] = uploaded_file.content_type.strip
80   - @instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
81   - @instance[:"#{@name}_updated_at"] = Time.now
  78 + instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_'))
  79 + instance_write(:content_type, uploaded_file.content_type.strip)
  80 + instance_write(:file_size, uploaded_file.size.to_i)
  81 + instance_write(:updated_at, Time.now)
82 82
83 83 @dirty = true
84 84
85 85 post_process
86 86
87 87 # Reset the file size if the original file was reprocessed.
88   - @instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
  88 + instance_write(:file_size, uploaded_file.size.to_i)
89 89 ensure
90 90 validate
91 91 end
@@ -148,11 +148,11 @@ def save
148 148 # Returns the name of the file as originally assigned, and as lives in the
149 149 # <attachment>_file_name attribute of the model.
150 150 def original_filename
151   - instance[:"#{name}_file_name"]
  151 + instance_read(:file_name)
152 152 end
153 153
154 154 def updated_at
155   - time = instance[:"#{name}_updated_at"]
  155 + time = instance_read(:updated_at)
156 156 time && time.to_i
157 157 end
158 158
@@ -209,6 +209,14 @@ def file?
209 209 !original_filename.blank?
210 210 end
211 211
  212 + def instance_write(attr, value)
  213 + instance.send(:"#{name}_#{attr}=", value)
  214 + end
  215 +
  216 + def instance_read(attr)
  217 + instance.send(:"#{name}_#{attr}")
  218 + end
  219 +
212 220 private
213 221
214 222 def logger
@@ -280,10 +288,10 @@ def queue_existing_for_delete #:nodoc:
280 288 @queued_for_delete += [:original, *@styles.keys].uniq.map do |style|
281 289 path(style) if exists?(style)
282 290 end.compact
283   - @instance[:"#{@name}_file_name"] = nil
284   - @instance[:"#{@name}_content_type"] = nil
285   - @instance[:"#{@name}_file_size"] = nil
286   - @instance[:"#{@name}_updated_at"] = nil
  291 + instance_write(:file_name, nil)
  292 + instance_write(:content_type, nil)
  293 + instance_write(:file_size, nil)
  294 + instance_write(:updated_at, nil)
287 295 end
288 296
289 297 def flush_errors #:nodoc:
20 test/attachment_test.rb
@@ -232,12 +232,12 @@ class AttachmentTest < Test::Unit::TestCase
232 232
233 233 context "with a file assigned in the database" do
234 234 setup do
235   - @instance.stubs(:[]).with(:avatar_file_name).returns("5k.png")
236   - @instance.stubs(:[]).with(:avatar_content_type).returns("image/png")
237   - @instance.stubs(:[]).with(:avatar_file_size).returns(12345)
  235 + @attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
  236 + @attachment.stubs(:instance_read).with(:content_type).returns("image/png")
  237 + @attachment.stubs(:instance_read).with(:file_size).returns(12345)
238 238 now = Time.now
239 239 Time.stubs(:now).returns(now)
240   - @instance.stubs(:[]).with(:avatar_updated_at).returns(Time.now)
  240 + @attachment.stubs(:instance_read).with(:updated_at).returns(Time.now)
241 241 end
242 242
243 243 should "return a correct url even if the file does not exist" do
@@ -251,7 +251,7 @@ class AttachmentTest < Test::Unit::TestCase
251 251
252 252 context "with the updated_at field removed" do
253 253 setup do
254   - @instance.stubs(:[]).with(:avatar_updated_at).returns(nil)
  254 + @attachment.stubs(:instance_read).with(:updated_at).returns(nil)
255 255 end
256 256
257 257 should "only return the url without the updated_at when sent #url" do
@@ -264,7 +264,7 @@ class AttachmentTest < Test::Unit::TestCase
264 264 end
265 265
266 266 should "return the proper path when filename has multiple .'s" do
267   - @instance.stubs(:[]).with(:avatar_file_name).returns("5k.old.png")
  267 + @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
268 268 assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path
269 269 end
270 270
@@ -331,10 +331,10 @@ class AttachmentTest < Test::Unit::TestCase
331 331 @existing_names = @attachment.styles.keys.collect do |style|
332 332 @attachment.path(style)
333 333 end
334   - @instance.expects(:[]=).with(:avatar_file_name, nil)
335   - @instance.expects(:[]=).with(:avatar_content_type, nil)
336   - @instance.expects(:[]=).with(:avatar_file_size, nil)
337   - @instance.expects(:[]=).with(:avatar_updated_at, nil)
  334 + @attachment.expects(:instance_write).with(:file_name, nil)
  335 + @attachment.expects(:instance_write).with(:content_type, nil)
  336 + @attachment.expects(:instance_write).with(:file_size, nil)
  337 + @attachment.expects(:instance_write).with(:updated_at, nil)
338 338 @attachment.assign nil
339 339 @attachment.save
340 340 end

0 comments on commit ff6e848

Please sign in to comment.
Something went wrong with that request. Please try again.