Commit b948f968 by Jon Yurek

Extract options into a new class, so they can be toyed with

parent 9edeb014
......@@ -18,5 +18,4 @@ gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require => false
# gem "ruby-debug", :platform => :ruby_18
# gem "ruby-debug19", :platform => :ruby_19
gem 'pry'
require 'rubygems'
require 'appraisal'
require 'bundler/setup'
require 'appraisal'
require 'rake'
require 'rake/testtask'
......
......@@ -19,6 +19,7 @@ gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 2.3.14"
gem "paperclip", :path=>"../"
......@@ -19,6 +19,7 @@ gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 3.0.10"
gem "paperclip", :path=>"../"
......@@ -19,6 +19,7 @@ gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 3.1.0"
gem "paperclip", :path=>"../"
......@@ -28,6 +28,7 @@
require 'erb'
require 'digest'
require 'tempfile'
require 'paperclip/options'
require 'paperclip/version'
require 'paperclip/upfile'
require 'paperclip/iostream'
......
......@@ -29,7 +29,7 @@ module Paperclip
}
end
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :source_file_options, :interpolator
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :interpolator
attr_accessor :post_processing
# Creates an Attachment object. +name+ is the name of the attachment,
......@@ -62,27 +62,7 @@ module Paperclip
options = self.class.default_options.merge(options)
@url = options[:url]
@url = @url.call(self) if @url.is_a?(Proc)
@path = options[:path]
@path = @path.call(self) if @path.is_a?(Proc)
@styles = options[:styles]
@only_process = options[:only_process]
@normalized_styles = nil
@default_url = options[:default_url]
@default_style = options[:default_style]
@storage = options[:storage]
@use_timestamp = options[:use_timestamp]
@whiny = options[:whiny_thumbnails] || options[:whiny]
@use_default_time_zone = options[:use_default_time_zone]
@hash_digest = options[:hash_digest]
@hash_data = options[:hash_data]
@hash_secret = options[:hash_secret]
@convert_options = options[:convert_options]
@source_file_options = options[:source_file_options]
@processors = options[:processors]
@preserve_files = options[:preserve_files]
@options = options
@options = Paperclip::Options.new(self, options)
@post_processing = true
@queued_for_delete = []
@queued_for_write = {}
......@@ -93,19 +73,13 @@ module Paperclip
initialize_storage
end
def styles
if @styles.respond_to?(:call) || !@normalized_styles
@normalized_styles = ActiveSupport::OrderedHash.new
(@styles.respond_to?(:call) ? @styles.call(self) : @styles).each do |name, args|
@normalized_styles[name] = Paperclip::Style.new(name, args.dup, self)
end
end
@normalized_styles
end
def processors
@processors.respond_to?(:call) ? @processors.call(instance) : @processors
end
# [:url, :path, :only_process, :normalized_styles, :default_url, :default_style,
# :storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
# :convert_options, :preserve_files].each do |field|
# define_method field do
# @options.send(field)
# end
# end
# What gets called when you call instance.attachment = File. It clears
# errors, assigns attributes, and processes the file. It
......@@ -139,7 +113,7 @@ module Paperclip
@dirty = true
post_process(*@only_process) if post_processing
post_process(*@options.only_process) if post_processing
# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
......@@ -154,9 +128,9 @@ module Paperclip
# grained security. This is not recommended if you don't need the
# security, however, for performance reasons. Set use_timestamp to false
# if you want to stop the attachment update time appended to the url
def url(style_name = default_style, use_timestamp = @use_timestamp)
default_url = @default_url.is_a?(Proc) ? @default_url.call(self) : @default_url
url = original_filename.nil? ? interpolate(default_url, style_name) : interpolate(@url, style_name)
def url(style_name = default_style, use_timestamp = @options.use_timestamp)
default_url = @options.default_url.is_a?(Proc) ? @options.default_url.call(self) : @options.default_url
url = original_filename.nil? ? interpolate(default_url, style_name) : interpolate(@options.url, style_name)
URI.escape(use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url)
end
......@@ -165,7 +139,7 @@ module Paperclip
# on disk. If the file is stored in S3, the path is the "key" part of the
# URL, and the :bucket option refers to the S3 bucket.
def path(style_name = default_style)
original_filename.nil? ? nil : interpolate(@path, style_name)
original_filename.nil? ? nil : interpolate(@options.path, style_name)
end
# Alias to +url+
......@@ -173,6 +147,14 @@ module Paperclip
url(style_name)
end
def default_style
@options.default_style
end
def styles
@options.styles
end
# Returns an array containing the errors on this attachment.
def errors
@errors
......@@ -205,7 +187,7 @@ module Paperclip
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
unless @preserve_files
unless @options.preserve_files
clear
save
end
......@@ -245,16 +227,16 @@ module Paperclip
# The time zone to use for timestamp interpolation. Using the default
# time zone ensures that results are consistent across all threads.
def time_zone
@use_default_time_zone ? Time.zone_default : Time.zone
@options.use_default_time_zone ? Time.zone_default : Time.zone
end
# Returns a unique hash suitable for obfuscating the URL of an otherwise
# publicly viewable attachment.
def hash(style_name = default_style)
raise ArgumentError, "Unable to generate hash without :hash_secret" unless @hash_secret
raise ArgumentError, "Unable to generate hash without :hash_secret" unless @options.hash_secret
require 'openssl' unless defined?(OpenSSL)
data = interpolate(@hash_data, style_name)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@hash_digest).new, @hash_secret, data)
data = interpolate(@options.hash_data, style_name)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options.hash_digest).new, @options.hash_secret, data)
end
def generate_fingerprint(source)
......@@ -352,28 +334,28 @@ module Paperclip
end
def initialize_storage #:nodoc:
storage_class_name = @storage.to_s.downcase.camelize
storage_class_name = @options.storage.to_s.downcase.camelize
begin
@storage_module = Paperclip::Storage.const_get(storage_class_name)
storage_module = Paperclip::Storage.const_get(storage_class_name)
rescue NameError
raise StorageMethodNotFound, "Cannot load storage module '#{storage_class_name}'"
end
self.extend(@storage_module)
self.extend(storage_module)
end
def extra_options_for(style) #:nodoc:
all_options = convert_options[:all]
all_options = @options.convert_options[:all]
all_options = all_options.call(instance) if all_options.respond_to?(:call)
style_options = convert_options[style]
style_options = @options.convert_options[style]
style_options = style_options.call(instance) if style_options.respond_to?(:call)
[ style_options, all_options ].compact.join(" ")
end
def extra_source_file_options_for(style) #:nodoc:
all_options = source_file_options[:all]
all_options = @options.source_file_options[:all]
all_options = all_options.call(instance) if all_options.respond_to?(:call)
style_options = source_file_options[style]
style_options = @options.source_file_options[style]
style_options = style_options.call(instance) if style_options.respond_to?(:call)
[ style_options, all_options ].compact.join(" ")
......@@ -389,7 +371,7 @@ module Paperclip
end
def post_process_styles(*style_args) #:nodoc:
styles.each do |name, style|
@options.styles.each do |name, style|
begin
if style_args.empty? || style_args.include?(name)
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
......@@ -399,7 +381,7 @@ module Paperclip
end
rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}")
(@errors[:processing] ||= []) << e.message if @whiny
(@errors[:processing] ||= []) << e.message if @options.whiny
end
end
end
......@@ -409,8 +391,8 @@ module Paperclip
end
def queue_existing_for_delete #:nodoc:
return unless (file? && @preserve_files==false)
@queued_for_delete += [:original, *styles.keys].uniq.map do |style|
return if @options.preserve_files || !file?
@queued_for_delete += [:original, *@options.styles.keys].uniq.map do |style|
path(style) if exists?(style)
end.compact
instance_write(:file_name, nil)
......
module Paperclip
class Options
attr_accessor :url, :path, :only_process, :normalized_styles, :default_url, :default_style,
:storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
:convert_options, :source_file_options, :preserve_files, :http_proxy
attr_accessor :s3_credentials, :s3_host_name, :s3_options, :s3_permissions, :s3_protocol,
:s3_headers, :s3_host_alias, :bucket
attr_accessor :fog_directory, :fog_credentials, :fog_host, :fog_public, :fog_file
def initialize(attachment, hash)
@attachment = attachment
@url = hash[:url]
@url = @url.call(@attachment) if @url.is_a?(Proc)
@path = hash[:path]
@path = @path.call(@attachment) if @path.is_a?(Proc)
@styles = hash[:styles]
@only_process = hash[:only_process]
@normalized_styles = nil
@default_url = hash[:default_url]
@default_style = hash[:default_style]
@storage = hash[:storage]
@use_timestamp = hash[:use_timestamp]
@whiny = hash[:whiny_thumbnails] || hash[:whiny]
@use_default_time_zone = hash[:use_default_time_zone]
@hash_digest = hash[:hash_digest]
@hash_data = hash[:hash_data]
@hash_secret = hash[:hash_secret]
@convert_options = hash[:convert_options]
@source_file_options = hash[:source_file_options]
@processors = hash[:processors]
@preserve_files = hash[:preserve_files]
@http_proxy = hash[:http_proxy]
#s3 options
@s3_credentials = hash[:s3_credentials]
@s3_host_name = hash[:s3_host_name]
@bucket = hash[:bucket]
@s3_options = hash[:s3_options]
@s3_permissions = hash[:s3_permissions]
@s3_protocol = hash[:s3_protocol]
@s3_headers = hash[:s3_headers]
@s3_host_alias = hash[:s3_host_alias]
#fog options
@fog_directory = hash[:fog_directory]
@fog_credentials = hash[:fog_credentials]
@fog_host = hash[:fog_host]
@fog_public = hash[:fog_public]
@fog_file = hash[:fog_file]
end
def method_missing(method, *args, &blk)
if method[-1] == "="
instance_variable_set("@#{method[0..-2]}", args[0])
else
instance_variable_get("@#{method}")
end
end
def processors
@processors.respond_to?(:call) ? @processors.call(instance) : @processors
end
def styles
if @styles.respond_to?(:call) || !@normalized_styles
normalized_styles = ActiveSupport::OrderedHash.new
(@styles.respond_to?(:call) ? @styles.call(@attachment) : @styles).each do |name, args|
normalized_styles[name] = Paperclip::Style.new(name, args.dup, @attachment)
end
end
normalized_styles
end
end
end
......@@ -41,15 +41,9 @@ module Paperclip
end unless defined?(Fog)
base.instance_eval do
@fog_directory = @options[:fog_directory]
@fog_credentials = parse_credentials(@options[:fog_credentials])
@fog_host = @options[:fog_host]
@fog_public = @options.key?(:fog_public) ? @options[:fog_public] : true
@fog_file = @options[:fog_file] || {}
unless @url.to_s.match(/^:fog.*url$/)
@path = @path.gsub(/:url/, @url)
@url = ':fog_public_url'
unless @options.url.to_s.match(/^:fog.*url$/)
@options.path = @options.path.gsub(/:url/, @options.url)
@options.url = ':fog_public_url'
end
Paperclip.interpolates(:fog_public_url) do |attachment, style|
attachment.public_url(style)
......@@ -65,15 +59,27 @@ module Paperclip
end
end
def fog_credentials
@fog_credentials ||= parse_credentials(@options.fog_credentials)
end
def fog_file
@fog_file ||= @options.fog_file || {}
end
def fog_public
@fog_public ||= @options.fog_public || true
end
def flush_writes
for style, file in @queued_for_write do
log("saving #{path(style)}")
retried = false
begin
directory.files.create(@fog_file.merge(
directory.files.create(fog_file.merge(
:body => file,
:key => path(style),
:public => @fog_public
:public => fog_public
))
rescue Excon::Errors::NotFound
raise if retried
......@@ -115,8 +121,8 @@ module Paperclip
end
def public_url(style = default_style)
if @fog_host
host = (@fog_host =~ /%d/) ? @fog_host % (path(style).hash % 4) : @fog_host
if @options.fog_host
host = (@options.fog_host =~ /%d/) ? @options.fog_host % (path(style).hash % 4) : @options.fog_host
"#{host}/#{path(style)}"
else
directory.files.new(:key => path(style)).public_url
......@@ -145,11 +151,11 @@ module Paperclip
end
def connection
@connection ||= ::Fog::Storage.new(@fog_credentials)
@connection ||= ::Fog::Storage.new(fog_credentials)
end
def directory
@directory ||= connection.directories.new(:key => @fog_directory)
@directory ||= connection.directories.new(:key => @options.fog_directory)
end
end
end
......
......@@ -77,33 +77,28 @@ module Paperclip
end unless defined?(AWS::S3)
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@s3_host_name = @options[:s3_host_name] || @s3_credentials[:s3_host_name]
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = set_permissions(@options[:s3_permissions])
@s3_protocol = @options[:s3_protocol] ||
@s3_options = @options.s3_options || {}
@s3_permissions = set_permissions(@options.s3_permissions)
@s3_protocol = @options.s3_protocol ||
Proc.new do |style|
(@s3_permissions[style.to_sym] || @s3_permissions[:default]) == :public_read ? 'http' : 'https'
end
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
@s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
unless @url.to_s.match(/^:s3.*url$/)
@path = @path.gsub(/:url/, @url)
@url = ":s3_path_url"
@s3_headers = @options.s3_headers || {}
unless @options.url.to_s.match(/^:s3.*url$/) || @options.url == ":asset_host"
@options.path = @options.path.gsub(/:url/, @options.url)
@options.url = ":s3_path_url"
end
@url = ":asset_host" if @options[:url].to_s == ":asset_host"
@options.url = @options.url.inspect if @options.url.is_a?(Symbol)
@http_proxy = @options[:http_proxy] || nil
@http_proxy = @options.http_proxy || nil
if @http_proxy
@s3_options.merge!({:proxy => @http_proxy})
end
AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
:access_key_id => s3_credentials[:access_key_id],
:secret_access_key => s3_credentials[:secret_access_key]
))
end
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
......@@ -124,7 +119,23 @@ module Paperclip
AWS::S3::S3Object.url_for(path(style_name), bucket_name, :expires_in => time, :use_ssl => (s3_protocol(style_name) == 'https'))
end
def s3_credentials
@s3_credentials ||= parse_credentials(@options.s3_credentials)
end
def s3_host_name
@options.s3_host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
end
def s3_host_alias
@s3_host_alias = @options.s3_host_alias
@s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
@s3_host_alias
end
def bucket_name
@bucket = @options.bucket || s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@bucket
end
......@@ -148,10 +159,6 @@ module Paperclip
using_http_proxy? ? @http_proxy[:password] : nil
end
def s3_host_name
@s3_host_name || "s3.amazonaws.com"
end
def set_permissions permissions
if permissions.is_a?(Hash)
permissions[:default] = permissions[:default] || :public_read
......@@ -161,10 +168,6 @@ module Paperclip
permissions
end
def s3_host_alias
@s3_host_alias
end
def parse_credentials creds
creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
......
......@@ -32,12 +32,12 @@ module Paperclip
# by default we behave as before, though.
# if a proc has been supplied, we call it here
def processors
@processors.respond_to?(:call) ? @processors.call(attachment.instance) : (@processors || attachment.processors)
@processors.respond_to?(:call) ? @processors.call(attachment.instance) : (@processors || attachment.options.processors)
end
# retrieves from the attachment the whiny setting
def whiny
attachment.whiny
attachment.options.whiny
end
# returns true if we're inclined to grumble
......@@ -74,7 +74,7 @@ module Paperclip
end
# Supports getting and setting style properties with hash notation to ensure backwards-compatibility
# eg. @attachment.styles[:large][:geometry]@ will still work
# eg. @attachment.options.styles[:large][:geometry]@ will still work
def [](key)
if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
send(key)
......
......@@ -85,7 +85,7 @@ class AttachmentTest < Test::Unit::TestCase
Paperclip::Attachment.default_options.keys.each do |key|
should "be the default_options for #{key}" do
assert_equal @old_default_options[key],
@attachment.instance_variable_get("@#{key}"),
@attachment.options.send(key),
key
end
end
......@@ -100,24 +100,11 @@ class AttachmentTest < Test::Unit::TestCase
Paperclip::Attachment.default_options.keys.each do |key|
should "be the new default_options for #{key}" do
assert_equal @new_default_options[key],
@attachment.instance_variable_get("@#{key}"),
@attachment.options.send(key),
key
end
end
end
context "with nested hash default" do
setup do
@nested_hash = {:thumb => {:first => "second" }}
Paperclip::Attachment.default_options[:styles] = @nested_hash
@dummy = Dummy.new
@attachment = @dummy.avatar
end
should "correctly clone the nested hash" do
assert_equal(@nested_hash, @attachment.instance_variable_get(:@styles))
end
end
end
end
......@@ -197,12 +184,12 @@ class AttachmentTest < Test::Unit::TestCase
end
should "interpolate the hash data" do
@attachment.expects(:interpolate).with(@attachment.options[:hash_data],anything).returns("interpolated_stuff")
@attachment.expects(:interpolate).with(@attachment.options.hash_data,anything).returns("interpolated_stuff")
@attachment.hash
end
should "result in the correct interpolation" do
assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options[:hash_data])
assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options.hash_data)
end
should "result in a correct hash" do
......@@ -285,7 +272,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "report the correct options when sent #extra_source_file_options_for(:thumb)" do
assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.options.source_file_options.inspect
end
should "report the correct options when sent #extra_source_file_options_for(:large)" do
......@@ -367,7 +354,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "have the correct geometry" do
assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
assert_equal "50x50#", @attachment.options.styles[:thumb][:geometry]
end
end
......@@ -379,13 +366,13 @@ class AttachmentTest < Test::Unit::TestCase
end
should "have the correct styles for the assigned instance values" do
assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
assert_nil @dummy.avatar.styles[:large]
assert_equal "50x50#", @dummy.avatar.options.styles[:thumb][:geometry]
assert_nil @dummy.avatar.options.styles[:large]
@dummy.other = 'b'
assert_equal "400x400", @dummy.avatar.styles[:large][:geometry]
assert_nil @dummy.avatar.styles[:thumb]
assert_equal "400x400", @dummy.avatar.options.styles[:large][:geometry]
assert_nil @dummy.avatar.options.styles[:thumb]
end
end
......@@ -429,7 +416,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "have the correct geometry" do
assert_equal "50x50#", @attachment.styles[:normal][:geometry]
assert_equal "50x50#", @attachment.options.styles[:normal][:geometry]
end
end
end
......@@ -447,7 +434,7 @@ class AttachmentTest < Test::Unit::TestCase
[:processors, :whiny, :convert_options, :geometry, :format].each do |field|
should "have the same #{field} field" do
assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
assert_equal @attachment.options.styles[:normal][field], @attachment.options.styles[:hash][field]
end
end
end
......@@ -468,7 +455,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "have the correct processors" do
assert_equal [ :test ], @attachment.styles[:normal][:processors]
assert_equal [ :test ], @attachment.options.styles[:normal][:processors]
end
end
end
......@@ -881,7 +868,7 @@ class AttachmentTest < Test::Unit::TestCase
context "and trying to delete" do
setup do
@existing_names = @attachment.styles.keys.collect do |style|
@existing_names = @attachment.options.styles.keys.collect do |style|
@attachment.path(style)
end
end
......@@ -1121,12 +1108,12 @@ class AttachmentTest < Test::Unit::TestCase
@dummy.destroy
end
assert File.exists?(@path)
assert File.exists?(@path), "#{@path} does not exist."
end
should "be deleted when the model is destroyed" do
@dummy.destroy
assert ! File.exists?(@path)
assert ! File.exists?(@path), "#{@path} does not exist."
end
end
......
......@@ -18,7 +18,7 @@ class FogTest < Test::Unit::TestCase
end
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.instance_variable_get("@fog_credentials")[:provider], 'AWS'
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
end
......@@ -34,7 +34,7 @@ class FogTest < Test::Unit::TestCase
end
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.instance_variable_get("@fog_credentials")[:provider], 'AWS'
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
end
......@@ -61,7 +61,7 @@ class FogTest < Test::Unit::TestCase
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
end
......@@ -181,7 +181,7 @@ class FogTest < Test::Unit::TestCase
end
should 'set the @fog_public instance variable to false' do
assert_equal false, @dummy.avatar.instance_variable_get('@fog_public')
assert_equal false, @dummy.avatar.options.fog_public
end
end
......
......@@ -9,6 +9,7 @@ require 'active_record'
require 'active_record/version'
require 'active_support'
require 'mime/types'
require 'pry'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......
......@@ -173,15 +173,23 @@ class S3Test < Test::Unit::TestCase
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => { :bucket => "prod_bucket" },
:s3_host_alias => Proc.new { |image| "cdn#{image.size.to_i % 4}.example.com" },
:s3_host_alias => Proc.new{|atch| "cdn#{atch.instance.counter % 4}.example.com"},
:path => ":attachment/:basename.:extension",
:url => ":s3_alias_url"
Dummy.class_eval do
def counter
@counter ||= 0
@counter += 1
@counter
end
end
@dummy = Dummy.new
@dummy.avatar = StringIO.new(".")
end
should "return a url based on the host_alias" do
assert_match %r{^http://cdn0.example.com/avatars/stringio.txt}, @dummy.avatar.url
assert_match %r{^http://cdn1.example.com/avatars/stringio.txt}, @dummy.avatar.url
assert_match %r{^http://cdn2.example.com/avatars/stringio.txt}, @dummy.avatar.url
end
should "still return the bucket name" do
......@@ -203,7 +211,7 @@ class S3Test < Test::Unit::TestCase
end
should "return a relative URL for Rails to calculate assets host" do
assert_match %r{^avatars/stringio.txt}, @dummy.avatar.url
assert_match %r{^avatars/stringio\.txt}, @dummy.avatar.url
end
end
......@@ -292,7 +300,7 @@ class S3Test < Test::Unit::TestCase
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {
:production => {:s3_host_name => "s3-world-end.amazonaws.com"},
:production => { :s3_host_name => "s3-world-end.amazonaws.com" },
:development => { :s3_host_name => "s3-ap-northeast-1.amazonaws.com" }
}
@dummy = Dummy.new
......
......@@ -6,8 +6,9 @@ class StyleTest < Test::Unit::TestCase
context "A style rule" do
setup do
@attachment = attachment :path => ":basename.:extension",
:styles => { :foo => {:geometry => "100x100#", :format => :png} }
@style = @attachment.styles[:foo]
:styles => { :foo => {:geometry => "100x100#", :format => :png} },
:whiny => true
@style = @attachment.options.styles[:foo]
end
should "be held as a Style object" do
......@@ -23,7 +24,6 @@ class StyleTest < Test::Unit::TestCase
end
should "be whiny if the attachment is" do
@attachment.expects(:whiny).returns(true)
assert @style.whiny?
end
......@@ -46,17 +46,11 @@ class StyleTest < Test::Unit::TestCase
}
end
should "defer processing of procs until they are needed" do
assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@geometry")
assert_kind_of Proc, @attachment.styles[:bar].instance_variable_get("@geometry")
assert_kind_of Proc, @attachment.instance_variable_get("@processors")
end
should "call procs when they are needed" do
assert_equal "300x300#", @attachment.styles[:foo].geometry
assert_equal "300x300#", @attachment.styles[:bar].geometry
assert_equal [:test], @attachment.styles[:foo].processors
assert_equal [:test], @attachment.styles[:bar].processors
assert_equal "300x300#", @attachment.options.styles[:foo].geometry
assert_equal "300x300#", @attachment.options.styles[:bar].geometry
assert_equal [:test], @attachment.options.styles[:foo].processors
assert_equal [:test], @attachment.options.styles[:bar].processors
end
end
......@@ -70,30 +64,30 @@ class StyleTest < Test::Unit::TestCase
:styles => styles
end
should "have the right number of styles" do
assert_kind_of Hash, @attachment.styles
assert_equal 3, @attachment.styles.size
assert_kind_of Hash, @attachment.options.styles
assert_equal 3, @attachment.options.styles.size
end
should "have styles as Style objects" do
[:aslist, :ashash, :aslist].each do |s|
assert_kind_of Paperclip::Style, @attachment.styles[s]
assert_kind_of Paperclip::Style, @attachment.options.styles[s]
end
end
should "have the right geometries" do
[:aslist, :ashash, :aslist].each do |s|
assert_equal @attachment.styles[s].geometry, "100x100"
assert_equal @attachment.options.styles[s].geometry, "100x100"
end
end
should "have the right formats" do
assert_equal @attachment.styles[:aslist].format, :png
assert_equal @attachment.styles[:ashash].format, :png
assert_nil @attachment.styles[:asstring].format
assert_equal @attachment.options.styles[:aslist].format, :png
assert_equal @attachment.options.styles[:ashash].format, :png
assert_nil @attachment.options.styles[:asstring].format
end
should "retain order" do
assert_equal [:aslist, :ashash, :asstring], @attachment.styles.keys
assert_equal [:aslist, :ashash, :asstring], @attachment.options.styles.keys
end
end
......@@ -102,7 +96,7 @@ class StyleTest < Test::Unit::TestCase
@attachment = attachment :path => ":basename.:extension",
:styles => {:thumb => "100x100", :large => "400x400"},
:convert_options => {:all => "-do_stuff", :thumb => "-thumbnailize"}
@style = @attachment.styles[:thumb]
@style = @attachment.options.styles[:thumb]
@file = StringIO.new("...")
@file.stubs(:original_filename).returns("file.jpg")
end
......@@ -113,7 +107,7 @@ class StyleTest < Test::Unit::TestCase
should "call extra_options_for(:thumb/:large) when convert options are requested" do
@attachment.expects(:extra_options_for).with(:thumb)
@attachment.styles[:thumb].convert_options
@attachment.options.styles[:thumb].convert_options
end
end
......@@ -122,7 +116,7 @@ class StyleTest < Test::Unit::TestCase
@attachment = attachment :path => ":basename.:extension",
:styles => {:thumb => "100x100", :large => "400x400"},
:source_file_options => {:all => "-density 400", :thumb => "-depth 8"}
@style = @attachment.styles[:thumb]
@style = @attachment.options.styles[:thumb]
@file = StringIO.new("...")
@file.stubs(:original_filename).returns("file.jpg")
end
......@@ -133,7 +127,7 @@ class StyleTest < Test::Unit::TestCase
should "call extra_options_for(:thumb/:large) when convert options are requested" do
@attachment.expects(:extra_source_file_options_for).with(:thumb)
@attachment.styles[:thumb].source_file_options
@attachment.options.styles[:thumb].source_file_options
end
end
......@@ -148,7 +142,7 @@ class StyleTest < Test::Unit::TestCase
}
},
:processors => [:thumbnail]
@style = @attachment.styles[:foo]
@style = @attachment.options.styles[:foo]
end
should "not get processors from the attachment" do
......@@ -176,11 +170,11 @@ class StyleTest < Test::Unit::TestCase
end
should "defer processing of procs until they are needed" do
assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@processors")
assert_kind_of Proc, @attachment.options.styles[:foo].instance_variable_get("@processors")
end
should "call procs when they are needed" do
assert_equal [:test], @attachment.styles[:foo].processors
assert_equal [:test], @attachment.options.styles[:foo].processors
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment