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