Skip to content

Commit

Permalink
Fixed defaults for path, and a bug related to format-finding when no …
Browse files Browse the repository at this point in the history
…format was supplied.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@397 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed Mar 19, 2008
1 parent 0544f0f commit 17a150f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
21 changes: 15 additions & 6 deletions lib/paperclip/attachment.rb
Expand Up @@ -14,7 +14,7 @@ def initialize name, instance, options
@url = options[:url] ||
"/:attachment/:id/:style/:basename.:extension"
@path = options[:path] ||
":attachment/:id/:style/:basename.:extension"
":rails_root/public/:attachment/:id/:style/:basename.:extension"
@styles = options[:styles] || {}
@default_url = options[:default_url] || "/:attachment/:style/missing.png"
@validations = options[:validations] || []
Expand All @@ -25,6 +25,8 @@ def initialize name, instance, options
@validation_errors = nil
@dirty = false

normalize_style_definition

@file = File.new(path) if original_filename && File.exists?(path)
end

Expand Down Expand Up @@ -116,16 +118,16 @@ def original_filename
def self.interpolations
@interpolations ||= {
:rails_root => lambda{|attachment,style| RAILS_ROOT },
:class => lambda{|attachment,style| attachment.instance.class.to_s },
:class => lambda{|attachment,style| attachment.instance.class.to_s.pluralize },
:basename => lambda do |attachment,style|
attachment.original_filename.gsub(/\.(.*?)$/, "")
end,
:extension => lambda do |attachment,style|
((style = attachment.styles[style]) && style.last) ||
File.extname(attachment.original_filename).gsub(/^\./, "")
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end,
:id => lambda{|attachment,style| attachment.instance.id },
:attachment => lambda{|attachment,style| attachment.name },
:attachment => lambda{|attachment,style| attachment.name.pluralize },
:style => lambda{|attachment,style| style || attachment.default_style },
}
end
Expand All @@ -145,12 +147,19 @@ def validate #:nodoc:
end
end

def normalize_style_definition
@styles.each do |name, args|
dimensions, format = [args, nil].flatten[0..1]
format = nil if format == ""
@styles[name] = [dimensions, format]
end
end

def post_process #:nodoc:
return nil if @file.nil?
@styles.each do |name, args|
begin
dimensions, format = [args, nil].flatten[0..1]
@styles[name] = [dimensions, format]
dimensions, format = args
@processed_files[name] = Thumbnail.make(self.file,
dimensions,
format,
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/geometry.rb
Expand Up @@ -17,7 +17,7 @@ def initialize width = nil, height = nil, modifier = nil
# File or path.
def self.from_file file
file = file.path if file.respond_to? "path"
parse(`#{Paperclip.path_for_command('identify')} "#{file}" 2>/dev/null`) ||
parse(`#{Paperclip.path_for_command('identify')} "#{file}"`) ||
raise(Errno::ENOENT, file)
end

Expand Down
16 changes: 16 additions & 0 deletions test/helper.rb
Expand Up @@ -15,6 +15,22 @@

require File.join(ROOT, 'lib', 'paperclip.rb')

class String
unless methods.include? :pluralize
def pluralize
"#{self}s"
end
end
end

class Symbol
unless methods.include? :pluralize
def pluralize
"#{self}s"
end
end
end

FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
Expand Down

0 comments on commit 17a150f

Please sign in to comment.