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