Commit 8cdc876a by jyurek

Changed the @attachment_definitions hash to be referenced through the inheritable_attributes.


git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@453 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent a1596bb6
...@@ -63,8 +63,6 @@ module Paperclip ...@@ -63,8 +63,6 @@ module Paperclip
end end
module ClassMethods module ClassMethods
attr_reader :attachment_definitions
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
# is typically a file stored somewhere on the filesystem and has been uploaded by a user. # is typically a file stored somewhere on the filesystem and has been uploaded by a user.
# The attribute returns a Paperclip::Attachment object which handles the management of # The attribute returns a Paperclip::Attachment object which handles the management of
...@@ -114,8 +112,8 @@ module Paperclip ...@@ -114,8 +112,8 @@ module Paperclip
def has_attached_file name, options = {} def has_attached_file name, options = {}
include InstanceMethods include InstanceMethods
@attachment_definitions ||= {} write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
@attachment_definitions[name] = {:validations => []}.merge(options) attachment_definitions[name] = {:validations => []}.merge(options)
after_save :save_attached_files after_save :save_attached_files
before_destroy :destroy_attached_files before_destroy :destroy_attached_files
...@@ -144,7 +142,7 @@ module Paperclip ...@@ -144,7 +142,7 @@ module Paperclip
# * +less_than+: equivalent to :in => 0..options[:less_than] # * +less_than+: equivalent to :in => 0..options[:less_than]
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity # * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
def validates_attachment_size name, options = {} def validates_attachment_size name, options = {}
@attachment_definitions[name][:validations] << lambda do |attachment, instance| attachment_definitions[name][:validations] << lambda do |attachment, instance|
unless options[:greater_than].nil? unless options[:greater_than].nil?
options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
end end
...@@ -159,13 +157,18 @@ module Paperclip ...@@ -159,13 +157,18 @@ module Paperclip
# Places ActiveRecord-style validations on the presence of a file. # Places ActiveRecord-style validations on the presence of a file.
def validates_attachment_presence name def validates_attachment_presence name
@attachment_definitions[name][:validations] << lambda do |attachment, instance| attachment_definitions[name][:validations] << lambda do |attachment, instance|
if attachment.file.nil? || !File.exist?(attachment.file.path) if attachment.file.nil? || !File.exist?(attachment.file.path)
"must be set." "must be set."
end end
end end
end end
# Returns the attachment definitions defined by each call to has_attached_file.
def attachment_definitions
read_inheritable_attribute(:attachment_definitions)
end
end end
module InstanceMethods #:nodoc: module InstanceMethods #:nodoc:
......
...@@ -5,7 +5,11 @@ require 'mocha' ...@@ -5,7 +5,11 @@ require 'mocha'
require 'tempfile' require 'tempfile'
require 'active_record' require 'active_record'
require 'ruby-debug' begin
require 'ruby-debug'
rescue LoadError
puts "ruby-debug not loaded"
end
ROOT = File.join(File.dirname(__FILE__), '..') ROOT = File.join(File.dirname(__FILE__), '..')
RAILS_ROOT = ROOT RAILS_ROOT = ROOT
......
...@@ -3,7 +3,7 @@ require 'test/helper.rb' ...@@ -3,7 +3,7 @@ require 'test/helper.rb'
class PaperclipTest < Test::Unit::TestCase class PaperclipTest < Test::Unit::TestCase
context "An ActiveRecord model with an 'avatar' attachment" do context "An ActiveRecord model with an 'avatar' attachment" do
setup do setup do
rebuild_model rebuild_model :path => "tmp/:class/omg/:style.:extension"
@file = File.new(File.join(FIXTURES_DIR, "5k.png")) @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
end end
...@@ -36,6 +36,26 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -36,6 +36,26 @@ class PaperclipTest < Test::Unit::TestCase
end end
end end
context "with a subclass" do
setup do
class ::SubDummy < Dummy; end
end
should "be able to use the attachment from the subclass" do
assert_nothing_raised do
@subdummy = SubDummy.create(:avatar => @file)
end
end
should "be able to see the attachment definition from the subclass's class" do
assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
end
teardown do
Object.send(:remove_const, "SubDummy") rescue nil
end
end
should "have an #avatar method" do should "have an #avatar method" do
assert Dummy.new.respond_to?(:avatar) assert Dummy.new.respond_to?(:avatar)
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