Commit f22d78f3 by Jon Yurek

Moved interpolations out of attachment

parent ea5f6ac0
...@@ -32,6 +32,7 @@ require 'paperclip/geometry' ...@@ -32,6 +32,7 @@ require 'paperclip/geometry'
require 'paperclip/processor' require 'paperclip/processor'
require 'paperclip/thumbnail' require 'paperclip/thumbnail'
require 'paperclip/storage' require 'paperclip/storage'
require 'paperclip/interpolations'
require 'paperclip/attachment' require 'paperclip/attachment'
if defined? RAILS_ROOT if defined? RAILS_ROOT
Dir.glob(File.join(File.expand_path(RAILS_ROOT), "lib", "paperclip_processors", "*.rb")).each do |processor| Dir.glob(File.join(File.expand_path(RAILS_ROOT), "lib", "paperclip_processors", "*.rb")).each do |processor|
......
...@@ -198,26 +198,7 @@ module Paperclip ...@@ -198,26 +198,7 @@ module Paperclip
# style as arguments. This hash can be added to with your own proc if # style as arguments. This hash can be added to with your own proc if
# necessary. # necessary.
def self.interpolations def self.interpolations
@interpolations ||= { Paperclip::Interpolations
:rails_root => lambda{|attachment,style| RAILS_ROOT },
:rails_env => lambda{|attachment,style| RAILS_ENV },
:class => lambda do |attachment,style|
attachment.instance.class.name.underscore.pluralize
end,
:basename => lambda do |attachment,style|
attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end,
:extension => lambda do |attachment,style|
((style = attachment.styles[style]) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end,
:id => lambda{|attachment,style| attachment.instance.id },
:id_partition => lambda do |attachment, style|
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end,
:attachment => lambda{|attachment,style| attachment.name.to_s.downcase.pluralize },
:style => lambda{|attachment,style| style || attachment.default_style },
}
end end
# This method really shouldn't be called that often. It's expected use is # This method really shouldn't be called that often. It's expected use is
...@@ -373,11 +354,10 @@ module Paperclip ...@@ -373,11 +354,10 @@ module Paperclip
end end
def interpolate pattern, style = default_style #:nodoc: def interpolate pattern, style = default_style #:nodoc:
interpolations = self.class.interpolations.sort{|a,b| a.first.to_s <=> b.first.to_s } interpolations = Paperclip::Interpolations.all
interpolations.reverse.inject( pattern.dup ) do |result, interpolation| interpolations.reverse.inject( pattern.dup ) do |result, tag|
tag, blk = interpolation
result.gsub(/:#{tag}/) do |match| result.gsub(/:#{tag}/) do |match|
blk.call( self, style ) Paperclip::Interpolations.send( tag, self, style )
end end
end end
end end
......
module Paperclip
module Interpolations
def self.[]= name, block
(class << self; self; end).class_eval do
define_method(name, &block)
end
end
def self.[] name
method(name)
end
def self.all
(singleton_methods - ["[]=", "[]", "all"]).sort
end
def self.rails_root attachment, style
RAILS_ROOT
end
def self.rails_env attachment, style
RAILS_ENV
end
def self.class attachment, style
attachment.instance.class.to_s.underscore.pluralize
end
def self.basename attachment, style
attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end
def self.extension attachment, style
((style = attachment.styles[style]) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end
def self.id attachment, style
attachment.instance.id
end
def self.id_partition attachment, style
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end
def self.attachment attachment, style
attachment.name.to_s.downcase.pluralize
end
def self.style attachment, style
style || attachment.default_style
end
end
end
...@@ -17,6 +17,7 @@ end ...@@ -17,6 +17,7 @@ end
ROOT = File.join(File.dirname(__FILE__), '..') ROOT = File.join(File.dirname(__FILE__), '..')
RAILS_ROOT = ROOT RAILS_ROOT = ROOT
RAILS_ENV = "test"
$LOAD_PATH << File.join(ROOT, 'lib') $LOAD_PATH << File.join(ROOT, 'lib')
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip') $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
......
require 'test/helper'
class InterpolationsTest < Test::Unit::TestCase
should "return all methods but the infrastructure when sent #all" do
methods = Paperclip::Interpolations.all
assert ! methods.include?(:[])
assert ! methods.include?(:[]=)
assert ! methods.include?(:all)
methods.each do |m|
assert Paperclip::Interpolations.respond_to? m
end
end
should "return the RAILS_ROOT" do
assert_equal RAILS_ROOT, Paperclip::Interpolations.rails_root(:attachment, :style)
end
should "return the RAILS_ENV" do
assert_equal RAILS_ENV, Paperclip::Interpolations.rails_env(:attachment, :style)
end
should "return the class of the instance" do
attachment = mock
attachment.expects(:instance).returns(attachment)
attachment.expects(:class).returns("Thing")
assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
end
should "return the basename of the file" do
attachment = mock
attachment.expects(:original_filename).returns("one.jpg").times(2)
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
end
should "return the extension of the file" do
attachment = mock
attachment.expects(:original_filename).returns("one.jpg")
attachment.expects(:styles).returns({})
assert_equal "jpg", Paperclip::Interpolations.extension(attachment, :style)
end
should "return the extension of the file as the format is defined in the style" do
attachment = mock
attachment.expects(:original_filename).never
attachment.expects(:styles).returns({:style => {:format => "png"}})
assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
end
should "return the id of the attachment" do
attachment = mock
attachment.expects(:id).returns(23)
attachment.expects(:instance).returns(attachment)
assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
end
should "return the partitioned id of the attachment" do
attachment = mock
attachment.expects(:id).returns(23)
attachment.expects(:instance).returns(attachment)
assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
end
should "return the name of the attachment" do
attachment = mock
attachment.expects(:name).returns("file")
assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
end
should "return the style" do
assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
end
should "return the default style" do
attachment = mock
attachment.expects(:default_style).returns(:default_style)
assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
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