Permalink
Browse files

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

…y methods.
  • Loading branch information...
Jon Yurek
Jon Yurek committed Nov 5, 2008
1 parent f301de5 commit ff6e8485a463dc980c586e101b330399a9051031
Showing with 32 additions and 24 deletions.
  1. +2 −2 lib/paperclip.rb
  2. +20 −12 lib/paperclip/attachment.rb
  3. +10 −10 test/attachment_test.rb
View
@@ -182,7 +182,7 @@ def validates_attachment_size name, options = {}
options[:in] = (0..options[:less_than])
end
- if attachment.file? && !options[:in].include?(instance[:"#{name}_file_size"].to_i)
+ if attachment.file? && !options[:in].include?(attachment.instance_read(:file_size).to_i)
min = options[:in].first
max = options[:in].last
@@ -223,7 +223,7 @@ def validates_attachment_content_type name, options = {}
unless attachment.original_filename.blank?
unless options[:content_type].blank?
- content_type = instance[:"#{name}_content_type"]
+ content_type = attachment.instance_read(:content_type)
unless valid_types.any?{|t| t === content_type }
options[:message] || "is not one of the allowed file types."
end
@@ -56,7 +56,7 @@ def initialize name, instance, options = {}
def assign uploaded_file
%w(file_name).each do |field|
unless @instance.class.column_names.include?("#{name}_#{field}")
- raise PaperclipError.new("#{self} model does not have required column '#{name}_#{field}'")
+ raise PaperclipError.new("#{@instance.class} model does not have required column '#{name}_#{field}'")
end
end
@@ -75,17 +75,17 @@ def assign uploaded_file
logger.info("[paperclip] Writing attributes for #{name}")
@queued_for_write[:original] = uploaded_file.to_tempfile
- @instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip.gsub /[^\w\d\.\-]+/, '_'
- @instance[:"#{@name}_content_type"] = uploaded_file.content_type.strip
- @instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
- @instance[:"#{@name}_updated_at"] = Time.now
+ instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_'))
+ instance_write(:content_type, uploaded_file.content_type.strip)
+ instance_write(:file_size, uploaded_file.size.to_i)
+ instance_write(:updated_at, Time.now)
@dirty = true
post_process
# Reset the file size if the original file was reprocessed.
- @instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
+ instance_write(:file_size, uploaded_file.size.to_i)
ensure
validate
end
@@ -148,11 +148,11 @@ def save
# Returns the name of the file as originally assigned, and as lives in the
# <attachment>_file_name attribute of the model.
def original_filename
- instance[:"#{name}_file_name"]
+ instance_read(:file_name)
end
def updated_at
- time = instance[:"#{name}_updated_at"]
+ time = instance_read(:updated_at)
time && time.to_i
end
@@ -209,6 +209,14 @@ def file?
!original_filename.blank?
end
+ def instance_write(attr, value)
+ instance.send(:"#{name}_#{attr}=", value)
+ end
+
+ def instance_read(attr)
+ instance.send(:"#{name}_#{attr}")
+ end
+
private
def logger
@@ -280,10 +288,10 @@ def queue_existing_for_delete #:nodoc:
@queued_for_delete += [:original, *@styles.keys].uniq.map do |style|
path(style) if exists?(style)
end.compact
- @instance[:"#{@name}_file_name"] = nil
- @instance[:"#{@name}_content_type"] = nil
- @instance[:"#{@name}_file_size"] = nil
- @instance[:"#{@name}_updated_at"] = nil
+ instance_write(:file_name, nil)
+ instance_write(:content_type, nil)
+ instance_write(:file_size, nil)
+ instance_write(:updated_at, nil)
end
def flush_errors #:nodoc:
View
@@ -232,12 +232,12 @@ class AttachmentTest < Test::Unit::TestCase
context "with a file assigned in the database" do
setup do
- @instance.stubs(:[]).with(:avatar_file_name).returns("5k.png")
- @instance.stubs(:[]).with(:avatar_content_type).returns("image/png")
- @instance.stubs(:[]).with(:avatar_file_size).returns(12345)
+ @attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
+ @attachment.stubs(:instance_read).with(:content_type).returns("image/png")
+ @attachment.stubs(:instance_read).with(:file_size).returns(12345)
now = Time.now
Time.stubs(:now).returns(now)
- @instance.stubs(:[]).with(:avatar_updated_at).returns(Time.now)
+ @attachment.stubs(:instance_read).with(:updated_at).returns(Time.now)
end
should "return a correct url even if the file does not exist" do
@@ -251,7 +251,7 @@ class AttachmentTest < Test::Unit::TestCase
context "with the updated_at field removed" do
setup do
- @instance.stubs(:[]).with(:avatar_updated_at).returns(nil)
+ @attachment.stubs(:instance_read).with(:updated_at).returns(nil)
end
should "only return the url without the updated_at when sent #url" do
@@ -264,7 +264,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "return the proper path when filename has multiple .'s" do
- @instance.stubs(:[]).with(:avatar_file_name).returns("5k.old.png")
+ @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path
end
@@ -331,10 +331,10 @@ class AttachmentTest < Test::Unit::TestCase
@existing_names = @attachment.styles.keys.collect do |style|
@attachment.path(style)
end
- @instance.expects(:[]=).with(:avatar_file_name, nil)
- @instance.expects(:[]=).with(:avatar_content_type, nil)
- @instance.expects(:[]=).with(:avatar_file_size, nil)
- @instance.expects(:[]=).with(:avatar_updated_at, nil)
+ @attachment.expects(:instance_write).with(:file_name, nil)
+ @attachment.expects(:instance_write).with(:content_type, nil)
+ @attachment.expects(:instance_write).with(:file_size, nil)
+ @attachment.expects(:instance_write).with(:updated_at, nil)
@attachment.assign nil
@attachment.save
end

0 comments on commit ff6e848

Please sign in to comment.