Skip to content
Browse files

Merge branch 'thoughtbot/master'

  • Loading branch information...
2 parents 26078a6 + 808d295 commit d327f0b55ffb238388f5f8a6848c5cb5fef45377 @bamnet committed Dec 7, 2009
View
4 Rakefile
@@ -75,8 +75,8 @@ spec = Gem::Specification.new do |s|
s.extra_rdoc_files = FileList["README*"].to_a
s.rdoc_options << '--line-numbers' << '--inline-source'
s.requirements << "ImageMagick"
- s.add_development_dependency 'thoughtbot-shoulda'
- s.add_development_dependency 'jferris-mocha', '= 0.9.5.0.1241126838'
+ s.add_development_dependency 'shoulda'
+ s.add_development_dependency 'jferris-mocha', '>= 0.9.5.0.1241126838'
s.add_development_dependency 'aws-s3'
s.add_development_dependency 'sqlite3-ruby'
s.add_development_dependency 'activerecord'
View
29 lib/paperclip.rb
@@ -259,13 +259,13 @@ def validates_attachment_size name, options = {}
max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0)
range = (min..max)
message = options[:message] || "file size must be between :min and :max bytes."
+ message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
- attachment_definitions[name][:validations] << [:size, {:min => min,
- :max => max,
- :range => range,
- :message => message,
- :if => options[:if],
- :unless => options[:unless]}]
+ validates_inclusion_of :"#{name}_file_size",
+ :in => range,
+ :message => message,
+ :if => options[:if],
+ :unless => options[:unless]
end
# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
@@ -283,9 +283,10 @@ def validates_attachment_thumbnails name, options = {}
# * +unless+: Same as +if+ but validates if lambda or method returns false.
def validates_attachment_presence name, options = {}
message = options[:message] || "must be set."
- attachment_definitions[name][:validations] << [:presence, {:message => message,
- :if => options[:if],
- :unless => options[:unless]}]
+ validates_presence_of :"#{name}_file_name",
+ :message => message,
+ :if => options[:if],
+ :unless => options[:unless]
end
# Places ActiveRecord-style validations on the content type of the file
@@ -305,10 +306,12 @@ def validates_attachment_presence name, options = {}
# model, content_type validation will work _ONLY upon assignment_ and
# re-validation after the instance has been reloaded will always succeed.
def validates_attachment_content_type name, options = {}
- attachment_definitions[name][:validations] << [:content_type, {:content_type => options[:content_type],
- :message => options[:message],
- :if => options[:if],
- :unless => options[:unless]}]
+ types = [options.delete(:content_type)].flatten
+ validates_each(:"#{name}_content_type", options) do |record, attr, value|
+ unless types.any?{|t| t === value }
+ record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value)
+ end
+ end
end
# Returns the attachment definitions defined by each call to
View
5 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
@@ -46,9 +46,8 @@ def allow_types?(types)
types.all? do |type|
file = StringIO.new(".")
file.content_type = type
- attachment = @subject.new.attachment_for(@attachment_name)
- attachment.assign(file)
- attachment.errors[:content_type].nil?
+ (subject = @subject.new).attachment_for(@attachment_name).assign(file)
+ subject.valid? && subject.errors.on(:"#{@attachment_name}_content_type").blank?
end
end
View
12 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
@@ -30,16 +30,16 @@ def description
protected
def error_when_not_valid?
- @attachment = @subject.new.send(@attachment_name)
- @attachment.assign(nil)
- not @attachment.errors[:presence].nil?
+ (subject = @subject.new).send(@attachment_name).assign(nil)
+ subject.valid?
+ not subject.errors.on(:"#{@attachment_name}_file_name").blank?
end
def no_error_when_valid?
@file = StringIO.new(".")
- @attachment = @subject.new.send(@attachment_name)
- @attachment.assign(@file)
- @attachment.errors[:presence].nil?
+ (subject = @subject.new).send(@attachment_name).assign(@file)
+ subject.valid?
+ subject.errors.on(:"#{@attachment_name}_file_name").blank?
end
end
end
View
8 lib/paperclip/matchers/validate_attachment_size_matcher.rb
@@ -54,9 +54,11 @@ def override_method object, method, &replacement
def passes_validation_with_size(new_size)
file = StringIO.new(".")
override_method(file, :size){ new_size }
- attachment = @subject.new.attachment_for(@attachment_name)
- attachment.assign(file)
- attachment.errors[:size].nil?
+ override_method(file, :to_tempfile){ file }
+
+ (subject = @subject.new).send(@attachment_name).assign(file)
+ subject.valid?
+ subject.errors.on(:"#{@attachment_name}_file_size").blank?
end
def lower_than_low?
View
4 lib/paperclip/storage.rb
@@ -159,6 +159,10 @@ def self.extended base
"#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
end
end
+
+ def expiring_url(time = 3600)
+ AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
+ end
def bucket_name
@bucket
View
1 test/matchers/validate_attachment_content_type_matcher_test.rb
@@ -5,6 +5,7 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
setup do
reset_table("dummies") do |d|
d.string :avatar_file_name
+ d.string :avatar_content_type
end
@dummy_class = reset_class "Dummy"
@dummy_class.has_attached_file :avatar
View
4 test/matchers/validate_attachment_presence_matcher_test.rb
@@ -3,7 +3,9 @@
class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
context "validate_attachment_presence" do
setup do
- reset_table("dummies"){|d| d.string :avatar_file_name }
+ reset_table("dummies") do |d|
+ d.string :avatar_file_name
+ end
@dummy_class = reset_class "Dummy"
@dummy_class.has_attached_file :avatar
@matcher = self.class.validate_attachment_presence(:avatar)
View
1 test/matchers/validate_attachment_size_matcher_test.rb
@@ -5,6 +5,7 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
setup do
reset_table("dummies") do |d|
d.string :avatar_file_name
+ d.integer :avatar_file_size
end
@dummy_class = reset_class "Dummy"
@dummy_class.has_attached_file :avatar
View
32 test/paperclip_test.rb
@@ -212,37 +212,35 @@ class ::SubDummy < Dummy; end
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
@dummy = Dummy.new
+ @dummy.stubs(:avatar_file_name).returns(nil)
end
should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(true)
- @dummy.avatar.expects(:validate_presence).returns(nil)
- @dummy.valid?
+ assert ! @dummy.valid?
end
should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(false)
- @dummy.avatar.expects(:validate_presence).never
- @dummy.valid?
+ assert @dummy.valid?
end
end
context "a validation with an unless guard clause" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
@dummy = Dummy.new
+ @dummy.stubs(:avatar_file_name).returns(nil)
end
should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(false)
- @dummy.avatar.expects(:validate_presence).returns(nil)
- @dummy.valid?
+ assert ! @dummy.valid?
end
should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(true)
- @dummy.avatar.expects(:validate_presence).never
- @dummy.valid?
+ assert @dummy.valid?
end
end
@@ -259,11 +257,11 @@ def self.should_validate validation, options, valid_file, invalid_file
end
if validation == :presence
should "have an error on the attachment" do
- assert @dummy.errors.on(:avatar)
+ assert @dummy.errors.on(:avatar_file_name)
end
else
should "not have an error on the attachment" do
- assert_nil @dummy.errors.on(:avatar)
+ assert_nil @dummy.errors.on(:avatar_file_name), @dummy.errors.full_messages.join(", ")
end
end
end
@@ -273,10 +271,7 @@ def self.should_validate validation, options, valid_file, invalid_file
@dummy.valid?
end
should "not have an error when assigned a valid file" do
- assert ! @dummy.avatar.errors.key?(validation)
- end
- should "not have an error on the attachment" do
- assert_nil @dummy.errors.on(:avatar)
+ assert_equal 0, @dummy.errors.length, @dummy.errors.full_messages.join(", ")
end
end
context "and assigned an invalid file" do
@@ -285,17 +280,14 @@ def self.should_validate validation, options, valid_file, invalid_file
@dummy.valid?
end
should "have an error when assigned a valid file" do
- assert_not_nil @dummy.avatar.errors[validation]
- end
- should "have an error on the attachment" do
- assert @dummy.errors.on(:avatar)
+ assert @dummy.errors.length > 0
end
end
end
end
[[:presence, {}, "5k.png", nil],
- [:size, {:in => 1..10240}, nil, "12k.png"],
+ [:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:size, {:less_than => 10240}, "5k.png", "12k.png"],
[:size, {:greater_than => 8096}, "12k.png", "5k.png"],
[:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
@@ -318,7 +310,7 @@ def self.should_validate validation, options, valid_file, invalid_file
end
should "have a file size min/max error message" do
- assert_match /between 0 and 10240 bytes/, @dummy.errors.on(:avatar)
+ assert_match %r/between 0 and 10240 bytes/, @dummy.errors.on(:avatar_file_size)
end
end
end
View
27 test/storage_test.rb
@@ -96,6 +96,33 @@ def rails_env(env)
assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
end
end
+
+ context "Generating a url with an expiration" do
+ setup do
+ AWS::S3::Base.stubs(:establish_connection!)
+ rebuild_model :storage => :s3,
+ :s3_credentials => {
+ :production => { :bucket => "prod_bucket" },
+ :development => { :bucket => "dev_bucket" }
+ },
+ :s3_host_alias => "something.something.com",
+ :path => ":attachment/:basename.:extension",
+ :url => ":s3_alias_url"
+
+ rails_env("production")
+
+ @dummy = Dummy.new
+ @dummy.avatar = StringIO.new(".")
+
+ AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600 })
+
+ @dummy.avatar.expiring_url
+ end
+
+ should "should succeed" do
+ assert true
+ end
+ end
context "Parsing S3 credentials with a bucket in them" do
setup do

0 comments on commit d327f0b

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