Skip to content

Commit

Permalink
Decouple the Interpolations class from the Attachment classs, you can…
Browse files Browse the repository at this point in the history
… extend Paperclip with custom interpolations as paperclip-extended does.
  • Loading branch information
mike-burns committed Aug 24, 2011
1 parent eebc7d9 commit 7478455
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
30 changes: 26 additions & 4 deletions lib/paperclip/attachment.rb
Expand Up @@ -27,12 +27,33 @@ def self.default_options
}
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

# Creates an Attachment object. +name+ is the name of the attachment,
# +instance+ is the ActiveRecord object instance it's attached to, and
# +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 = {}
@name = name
@instance = instance
Expand Down Expand Up @@ -65,6 +86,7 @@ def initialize name, instance, options = {}
@queued_for_write = {}
@errors = {}
@dirty = false
@interpolator = (options[:interpolator] || Paperclip::Interpolations)

initialize_storage
end
Expand Down Expand Up @@ -138,7 +160,7 @@ def url(style_name = default_style, use_timestamp = @use_timestamp)
# 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
# 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)
end

Expand Down Expand Up @@ -377,8 +399,8 @@ def post_process_styles(*style_args) #:nodoc:
end
end

def interpolate pattern, style_name = default_style #:nodoc:
Paperclip::Interpolations.interpolate(pattern, self, style_name)
def interpolate(pattern, style_name = default_style) #:nodoc:
interpolator.interpolate(pattern, self, style_name)
end

def queue_existing_for_delete #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/thumbnail.rb
Expand Up @@ -17,7 +17,7 @@ class Thumbnail < Processor
#
# 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
# +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
Expand Down
28 changes: 21 additions & 7 deletions test/attachment_test.rb
Expand Up @@ -1039,27 +1039,41 @@ def do_after_all; end
end
end
end

context "an attachment with delete_file option set to false" do
setup do
rebuild_model :preserve_files => true
@dummy = Dummy.new
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy.avatar = @file
@dummy.save!
@attachment = @dummy.avatar
@path = @attachment.path
@attachment = @dummy.avatar
@path = @attachment.path
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
assert File.exists?(@path)
end
should "not dleete the file when model is destroy" do

should "not delete the file when model is destroy" do
@dummy.destroy
assert File.exists?(@path)
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

0 comments on commit 7478455

Please sign in to comment.