Commit 35819883 by jyurek

Added new attributes: default_style and missing_url

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@177 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent bcba1518
...@@ -37,7 +37,10 @@ module Thoughtbot #:nodoc: ...@@ -37,7 +37,10 @@ module Thoughtbot #:nodoc:
:path => ":attachment/:id/:style_:name", :path => ":attachment/:id/:style_:name",
:attachment_type => :image, :attachment_type => :image,
:thumbnails => {}, :thumbnails => {},
:delete_on_destroy => true :delete_on_destroy => true,
:default_style => :original,
:missing_url => "",
:missing_path => ""
} }
class ThumbnailCreationError < StandardError; end #:nodoc class ThumbnailCreationError < StandardError; end #:nodoc
...@@ -56,11 +59,11 @@ module Thoughtbot #:nodoc: ...@@ -56,11 +59,11 @@ module Thoughtbot #:nodoc:
# Note this does not save the attachments. # Note this does not save the attachments.
# user.avatar = File.new("~/pictures/me.png") # user.avatar = File.new("~/pictures/me.png")
# user.avatar = params[:user][:avatar] # When :avatar is a file_field # user.avatar = params[:user][:avatar] # When :avatar is a file_field
# * attachment_file_name(style = :original): The name of the file, including path information. Pass in the # * attachment_file_name(style): The name of the file, including path information. Pass in the
# name of a thumbnail to get the path to that thumbnail. # name of a thumbnail to get the path to that thumbnail.
# user.avatar_file_name(:thumb) # => "public/users/44/thumb/me.png" # user.avatar_file_name(:thumb) # => "public/users/44/thumb/me.png"
# user.avatar_file_name # => "public/users/44/original/me.png" # user.avatar_file_name # => "public/users/44/original/me.png"
# * attachment_url(style = :original): The public URL of the attachment, suitable for passing to +image_tag+ # * attachment_url(style): The public URL of the attachment, suitable for passing to +image_tag+
# or +link_to+. Pass in the name of a thumbnail to get the url to that thumbnail. # or +link_to+. Pass in the name of a thumbnail to get the url to that thumbnail.
# user.avatar_url(:thumb) # => "http://assethost.com/users/44/thumb/me.png" # user.avatar_url(:thumb) # => "http://assethost.com/users/44/thumb/me.png"
# user.avatar_url # => "http://assethost.com/users/44/original/me.png" # user.avatar_url # => "http://assethost.com/users/44/original/me.png"
...@@ -92,6 +95,15 @@ module Thoughtbot #:nodoc: ...@@ -92,6 +95,15 @@ module Thoughtbot #:nodoc:
# the rest off (weighted at the center). # the rest off (weighted at the center).
# * +delete_on_destroy+: When records are deleted, the attachment that goes with it is also deleted. Set # * +delete_on_destroy+: When records are deleted, the attachment that goes with it is also deleted. Set
# this to +false+ to prevent the file from being deleted. # this to +false+ to prevent the file from being deleted.
# * +default_style+: The thumbnail style that will be used by default for +attachment_file_name+ and +attachment_url+
# Defaults to +original+.
# has_attached_file :avatar, :thumbnails => { :normal => "100x100#" },
# :default_style => :normal
# user.avatar_url # => "/avatars/23/normal_me.png"
# * +missing_url+: The URL that will be returned if there is no attachment assigned. It should be an absolute
# URL, not relative to the +url_prefix+. This field is interpolated.
# has_attached_file :avatar, :missing_url => "/images/default_:style_avatar.png"
# User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
# #
# == Interpolation # == Interpolation
# The +path_prefix+, +url_prefix+, and +path+ options can have dynamic interpolation done so that the # The +path_prefix+, +url_prefix+, and +path+ options can have dynamic interpolation done so that the
...@@ -167,13 +179,13 @@ module Thoughtbot #:nodoc: ...@@ -167,13 +179,13 @@ module Thoughtbot #:nodoc:
private "#{attr}_attachment" private "#{attr}_attachment"
define_method "#{attr}_file_name" do |*args| define_method "#{attr}_file_name" do |*args|
style = args.shift || :original # This prevents arity warnings style = args.shift || attachments[attr][:default_style] # This prevents arity warnings
read_attribute("#{attr}_file_name") ? path_for(attachments[attr], style) : "" path_for(attachments[attr], style) || interpolate(attachments[attr], attachments[attr][:missing_path], style)
end end
define_method "#{attr}_url" do |*args| define_method "#{attr}_url" do |*args|
style = args.shift || :original # This prevents arity warnings style = args.shift || attachments[attr][:default_style] # This prevents arity warnings
read_attribute("#{attr}_file_name") ? url_for(attachments[attr], style) : "" url_for(attachments[attr], style) || interpolate(attachments[attr], attachments[attr][:missing_url], style)
end end
define_method "#{attr}_valid?" do define_method "#{attr}_valid?" do
...@@ -214,33 +226,33 @@ module Thoughtbot #:nodoc: ...@@ -214,33 +226,33 @@ module Thoughtbot #:nodoc:
module InstanceMethods #:nodoc: module InstanceMethods #:nodoc:
private private
def interpolate attachment, prefix_type, style def interpolate attachment, source, style
file_name = read_attribute("#{attachment[:name]}_file_name") file_name = read_attribute("#{attachment[:name]}_file_name")
return "" unless file_name && self.id returning source.dup do |s|
s.gsub!(/:rails_root/, RAILS_ROOT)
returning "#{attachment[prefix_type]}/#{attachment[:path]}" do |prefix| s.gsub!(/:id/, self.id.to_s) if self.id
prefix.gsub!(/:rails_root/, RAILS_ROOT) s.gsub!(/:class/, self.class.to_s.underscore.pluralize)
prefix.gsub!(/:id/, self.id.to_s) if self.id s.gsub!(/:style/, style.to_s)
prefix.gsub!(/:class/, self.class.to_s.underscore.pluralize) s.gsub!(/:attachment/, attachment[:name].to_s.pluralize)
prefix.gsub!(/:style/, style.to_s) s.gsub!(/:name/, file_name) if file_name
prefix.gsub!(/:attachment/, attachment[:name].to_s.pluralize)
prefix.gsub!(/:name/, file_name)
end end
end end
def path_for attachment, style = :original def path_for attachment, style = nil
style ||= attachment[:default_style]
file = read_attribute("#{attachment[:name]}_file_name") file = read_attribute("#{attachment[:name]}_file_name")
return nil unless file return nil unless file && self.id
prefix = interpolate attachment, :path_prefix, style prefix = interpolate attachment, "#{attachment[:path_prefix]}/#{attachment[:path]}", style
File.join( prefix.split("/") ) File.join( prefix.split("/") )
end end
def url_for attachment, style = :original def url_for attachment, style = nil
style ||= attachment[:default_style]
file = read_attribute("#{attachment[:name]}_file_name") file = read_attribute("#{attachment[:name]}_file_name")
return nil unless file return nil unless file && self.id
interpolate attachment, :url_prefix, style interpolate attachment, "#{attachment[:url_prefix]}/#{attachment[:path]}", style
end end
def ensure_directories_for attachment def ensure_directories_for attachment
......
...@@ -30,12 +30,15 @@ end ...@@ -30,12 +30,15 @@ end
class NonStandard < ActiveRecord::Base class NonStandard < ActiveRecord::Base
has_attached_file :resume, :attachment_type => :document, has_attached_file :resume, :attachment_type => :document,
:path_prefix => "/tmp", :path_prefix => "/tmp",
:path => ":attachment_:id_:name" :path => ":attachment_:id_:name",
:missing_url => "/:class/:style/:attachment/404.txt"
has_attached_file :avatar, :attachment_type => :image, has_attached_file :avatar, :attachment_type => :image,
:thumbnails => { :cropped => "200x10#", :thumbnails => { :cropped => "200x10#",
:bigger => "1000x1000", :bigger => "1000x1000",
:smaller => "200x200>", :smaller => "200x200>",
:square => "150x150#" }, :square => "150x150#" },
:path_prefix => "./repository", :path_prefix => "./repository",
:path => ":class/:attachment/:id/:style_:name" :path => ":class/:attachment/:id/:style_:name",
:default_style => :square,
:missing_url => "/:class/:style/:attachment/404.png"
end end
\ No newline at end of file
...@@ -38,12 +38,12 @@ class PaperclipNonStandardTest < Test::Unit::TestCase ...@@ -38,12 +38,12 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
assert @ns.valid? assert @ns.valid?
end end
def test_should_default_to_original_for_path_and_url def test_should_default_to_the_assigned_default_style_for_path_and_url
assert_equal @ns.resume_file_name(:original), @ns.resume_file_name assert_equal @ns.resume_file_name(:original), @ns.resume_file_name
assert_equal @ns.resume_url(:original), @ns.resume_url assert_equal @ns.resume_url(:original), @ns.resume_url
assert_equal @ns.avatar_file_name(:original), @ns.avatar_file_name assert_equal @ns.avatar_file_name(:square), @ns.avatar_file_name
assert_equal @ns.avatar_url(:original), @ns.avatar_url assert_equal @ns.avatar_url(:square), @ns.avatar_url
end end
def test_should_delete_files_on_destroy def test_should_delete_files_on_destroy
...@@ -61,5 +61,15 @@ class PaperclipNonStandardTest < Test::Unit::TestCase ...@@ -61,5 +61,15 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
assert !File.exists?(name), name assert !File.exists?(name), name
end end
end end
def test_should_return_missing_url_interpolated_when_no_attachment_exists
assert @ns.save
assert @ns.destroy_resume
assert @ns.destroy_avatar
assert_equal "/non_standards/original/resumes/404.txt", @ns.resume_url
assert_equal "/non_standards/bigger/avatars/404.png", @ns.avatar_url(:bigger)
assert_equal "/non_standards/original/avatars/404.png", @ns.avatar_url(:original)
assert_equal "/non_standards/square/avatars/404.png", @ns.avatar_url
end
end end
\ No newline at end of file
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