Skip to content

Commit

Permalink
Verified :url interpolation. Added FakeModel for testing. Fixed defau…
Browse files Browse the repository at this point in the history
…lt_options merging race test problem.
  • Loading branch information
Jon Yurek committed May 8, 2009
1 parent 3eb7844 commit 81704d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Attachment
def self.default_options
@default_options ||= {
:url => "/system/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public:url",
:styles => {},
:default_url => "/:attachment/:style/missing.png",
:default_style => :original,
Expand All @@ -16,7 +16,7 @@ def self.default_options
}
end

attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write
attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write, :options

# Creates an Attachment object. +name+ is the name of the attachment,
# +instance+ is the ActiveRecord object instance it's attached to, and
Expand Down
14 changes: 13 additions & 1 deletion test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ class Dummy
end

class AttachmentTest < Test::Unit::TestCase
should "return the path based on the url by default" do
@attachment = attachment :url => "/:class/:id/:basename"
@model = @attachment.instance
@model.id = 1234
@model.avatar_file_name = "fake.jpg"
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
end

context "Attachment default_options" do
setup do
rebuild_model
Expand Down Expand Up @@ -456,6 +464,7 @@ def do_after_all; end

context "An attachment" do
setup do
@old_defaults = Paperclip::Attachment.default_options.dup
Paperclip::Attachment.default_options.merge!({
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
})
Expand All @@ -468,7 +477,10 @@ def do_after_all; end
"5k.png"), 'rb')
end

teardown { @file.close }
teardown do
@file.close
Paperclip::Attachment.default_options.merge!(@old_defaults)
end

should "raise if there are not the correct columns when you try to assign" do
@other_attachment = Paperclip::Attachment.new(:not_here, @instance)
Expand Down
19 changes: 19 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,22 @@ def temporary_rails_env(new_env)
Object.const_set("RAILS_ENV", old_env)
end
end

class FakeModel
attr_accessor :avatar_file_name,
:avatar_file_size,
:avatar_last_updated,
:avatar_content_type,
:id

def errors
@errors ||= []
end

def run_callbacks
end
end

def attachment options
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end

0 comments on commit 81704d1

Please sign in to comment.