Commit 8cceb0ea by jyurek

Added default options that are resettable

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@432 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent cd7030a7
......@@ -3,8 +3,8 @@ module Paperclip
# deletes when the model is destroyed, and processes the file upon assignment.
class Attachment
def self.defaults
@defaults ||= {
def self.default_options
@default_options ||= {
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:styles => {},
......@@ -23,16 +23,14 @@ module Paperclip
@name = name
@instance = instance
# options = options.merge(self.class.defaults)
options = self.class.default_options.merge(options)
@url = options[:url] ||
"/:attachment/:id/:style/:basename.:extension"
@path = options[:path] ||
":rails_root/public/:attachment/:id/:style/:basename.:extension"
@styles = options[:styles] || {}
@default_url = options[:default_url] || "/:attachment/:style/missing.png"
@validations = options[:validations] || []
@default_style = options[:default_style] || :original
@url = options[:url]
@path = options[:path]
@styles = options[:styles]
@default_url = options[:default_url]
@validations = options[:validations]
@default_style = options[:default_style]
@queued_for_delete = []
@processed_files = {}
@errors = []
......@@ -48,7 +46,7 @@ module Paperclip
# assigns attributes, processes the file, and runs validations. It also queues up
# the previous file for deletion, to be flushed away on #save of its host.
def assign uploaded_file
return nil unless valid_file?(uploaded_file) || uploaded_file.nil?
return nil unless valid_assignment?(uploaded_file)
queue_existing_for_delete
@errors = []
......@@ -114,7 +112,7 @@ module Paperclip
# style. Useful for streaming with +send_file+.
def to_io style = nil
begin
style ||= @default_style
style ||= default_style
@processed_files[style] || File.new(path(style))
rescue Errno::ENOENT
nil
......@@ -154,8 +152,8 @@ module Paperclip
private
def valid_file? file #:nodoc:
file.respond_to?(:original_filename) && file.respond_to?(:content_type)
def valid_assignment? file #:nodoc:
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
end
def validate #:nodoc:
......@@ -183,9 +181,9 @@ module Paperclip
@processed_files[name] = Thumbnail.make(self.file,
dimensions,
format,
@whiny_thumbnails)
@whiny_thumnails)
rescue Errno::ENOENT => e
@errors << "could not be processed because the file does not exist."
@errors << "could not be processed because the filedoes not exist."
rescue PaperclipError => e
@errors << e.message
end
......@@ -194,7 +192,7 @@ module Paperclip
end
def interpolate pattern, style = nil #:nodoc:
style ||= @default_style
style ||= default_style
pattern = pattern.dup
self.class.interpolations.each do |tag, l|
pattern.gsub!(/:#{tag}/) do |match|
......@@ -228,7 +226,7 @@ module Paperclip
FileUtils.mkdir_p( File.dirname(path(style)) )
@processed_files[style] = file.stream_to(path(style)) unless file.path == path(style)
end
@file = @processed_files[@default_style]
@file = @processed_files[default_style]
end
def flush_deletes #:nodoc:
......
......@@ -5,48 +5,51 @@ class Dummy
end
class AttachmentTest < Test::Unit::TestCase
# context "Attachment defaults" do
# setup do
# @old_defaults = Paperclip::Attachment.defaults
# @new_defaults = @old_defaults.merge({
# :path => "argle/bargle",
# :url => "fooferon",
# :default_url => "not here.png"
# })
# end
#
# should "be overrideable" do
# Paperclip::Attachment.defaults.merge!(@new_defaults)
# @new_defaults.keys.each do |key|
# assert_equal @new_defaults[key], Paperclip::Attachment.defaults[key]
# end
# end
#
# context "on an Attachment" do
# setup do
# @attachment = Dummy.new
# end
#
# should "be the defaults" do
# @old_defaults.keys.each do |key|
# assert_equal @old_defaults[key], @attachment.instance_variable_get("@#{key}"), key
# end
# end
#
# context "when redefined" do
# setup do
# Paperclip::Attachment.defaults.merge!(@new_defaults)
# @attachment = Dummy.new
# end
#
# should "be the new defaults" do
# @new_defaults.keys.each do |key|
# assert_equal @new_defaults[key], @attachment.instance_variable_get("@#{key}"), key
# end
# end
# end
# end
# end
context "Attachment default_options" do
setup do
rebuild_model
@old_default_options = Paperclip::Attachment.default_options
@new_default_options = @old_default_options.merge({
:path => "argle/bargle",
:url => "fooferon",
:default_url => "not here.png"
})
end
should "be overrideable" do
Paperclip::Attachment.default_options.merge!(@new_default_options)
@new_default_options.keys.each do |key|
assert_equal @new_default_options[key], Paperclip::Attachment.default_options[key]
end
end
context "on an Attachment" do
setup do
@dummy = Dummy.new
@attachment = @dummy.avatar
end
should "be the default_options" do
@old_default_options.keys.each do |key|
assert_equal @old_default_options[key], @attachment.instance_variable_get("@#{key}"), key
end
end
context "when redefined" do
setup do
Paperclip::Attachment.default_options.merge!(@new_default_options)
@dummy = Dummy.new
@attachment = @dummy.avatar
end
should "be the new default_options" do
@new_default_options.keys.each do |key|
assert_equal @new_default_options[key], @attachment.instance_variable_get("@#{key}"), key
end
end
end
end
end
context "An attachment" do
setup do
......
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