Commit 74784558 by Mike Burns

Decouple the Interpolations class from the Attachment classs, you can extend…

Decouple the Interpolations class from the Attachment classs, you can extend Paperclip with custom interpolations as paperclip-extended does.
parent eebc7d98
...@@ -27,12 +27,33 @@ module Paperclip ...@@ -27,12 +27,33 @@ module Paperclip
} }
end end
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :source_file_options attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :source_file_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,
# +instance+ is the ActiveRecord object instance it's attached to, and # +instance+ is the ActiveRecord object instance it's attached to, and
# +options+ is the same as the hash passed to +has_attached_file+. # +options+ is the same as the hash passed to +has_attached_file+.
#
# Options include:
#
# +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
# +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
# +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
# +only_process+ - style args to be run through the post-processor. This defaults to the empty list
# +default_url+ - a URL for the missing image
# +default_style+ - the style to use when don't specify an argument to e.g. #url, #path
# +storage+ - the storage mechanism. Defaults to :filesystem
# +use_timestamp+ - whether to append an anti-caching timestamp to image URLs. Defaults to true
# +whiny+, +whiny_thumbnails+ - whether to raise when thumbnailing fails
# +use_default_time_zone+ - related to +use_timestamp+. Defaults to true
# +hash_digest+ - a string representing a class that will be used to hash URLs for obfuscation
# +hash_data+ - the relative URL for the hash data. This is interpolated using +interpolator+
# +hash_secret+ - a secret passed to the +hash_digest+
# +convert_options+ - flags passed to the +convert+ command for processing
# +source_file_options+ - flags passed to the +convert+ command that controls how the file is read
# +processors+ - classes that transform the attachment. Defaults to [:thumbnail]
# +preserve_files+ - whether to keep files on the filesystem when deleting to clearing the attachment. Defaults to false
# +interpolator+ - the object used to interpolate filenames and URLs. Defaults to Paperclip::Interpolations
def initialize name, instance, options = {} def initialize name, instance, options = {}
@name = name @name = name
@instance = instance @instance = instance
...@@ -65,6 +86,7 @@ module Paperclip ...@@ -65,6 +86,7 @@ module Paperclip
@queued_for_write = {} @queued_for_write = {}
@errors = {} @errors = {}
@dirty = false @dirty = false
@interpolator = (options[:interpolator] || Paperclip::Interpolations)
initialize_storage initialize_storage
end end
...@@ -138,7 +160,7 @@ module Paperclip ...@@ -138,7 +160,7 @@ module Paperclip
# file is stored in the filesystem the path refers to the path of the file # file is stored in the filesystem the path refers to the path of the file
# 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(@path, style_name)
end end
...@@ -377,8 +399,8 @@ module Paperclip ...@@ -377,8 +399,8 @@ module Paperclip
end end
end end
def interpolate pattern, style_name = default_style #:nodoc: def interpolate(pattern, style_name = default_style) #:nodoc:
Paperclip::Interpolations.interpolate(pattern, self, style_name) interpolator.interpolate(pattern, self, style_name)
end end
def queue_existing_for_delete #:nodoc: def queue_existing_for_delete #:nodoc:
......
...@@ -17,7 +17,7 @@ module Paperclip ...@@ -17,7 +17,7 @@ module Paperclip
# #
# Options include: # Options include:
# #
# +geometry+ - the desired width and height of the thumbnail # +geometry+ - the desired width and height of the thumbnail (required)
# +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry and a +transformation_to+. Defaults to Paperclip::Geometry # +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry and a +transformation_to+. Defaults to Paperclip::Geometry
# +string_geometry_parser+ - an object with a method named +parse+ that takes a string and produces an object with +width+, +height+, and +to_s+ accessors. Defaults to Paperclip::Geometry # +string_geometry_parser+ - an object with a method named +parse+ that takes a string and produces an object with +width+, +height+, and +to_s+ accessors. Defaults to Paperclip::Geometry
# +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read # +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read
......
...@@ -1039,7 +1039,7 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1039,7 +1039,7 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
end end
context "an attachment with delete_file option set to false" do context "an attachment with delete_file option set to false" do
setup do setup do
rebuild_model :preserve_files => true rebuild_model :preserve_files => true
...@@ -1047,19 +1047,33 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1047,19 +1047,33 @@ class AttachmentTest < Test::Unit::TestCase
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy.avatar = @file @dummy.avatar = @file
@dummy.save! @dummy.save!
@attachment = @dummy.avatar @attachment = @dummy.avatar
@path = @attachment.path @path = @attachment.path
end end
should "not delete the files from storage when attachment is destroyed" do should "not delete the files from storage when attachment is destroyed" do
@attachment.destroy @attachment.destroy
assert File.exists?(@path) assert File.exists?(@path)
end end
should "not dleete the file when model is destroy" do should "not delete the file when model is destroy" do
@dummy.destroy @dummy.destroy
assert File.exists?(@path) assert File.exists?(@path)
end end
end end
context "setting an interpolation class" do
should "produce the URL with the given interpolations" do
Interpolator = Class.new do
def self.interpolate(pattern, attachment, style_name)
"hello"
end
end
instance = Dummy.new
attachment = Paperclip::Attachment.new(:avatar, instance, :interpolator => Interpolator)
assert_equal "hello", attachment.url
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