Permalink
Browse files

passing AttachmentOptions instance instead of hash to Attachment.new

  • Loading branch information...
1 parent ac4408a commit 5f8140d1d2ca5f8cf133ee96dd7ee958dfd36ce7 @Sporky023 Sporky023 committed Feb 3, 2012
View
@@ -37,6 +37,7 @@
require 'paperclip/interpolations'
require 'paperclip/style'
require 'paperclip/attachment'
+require 'paperclip/attachment_options'
require 'paperclip/storage'
require 'paperclip/callback_compatibility'
require 'paperclip/missing_attachment_styles'
@@ -325,7 +326,7 @@ def has_attached_file name, options = {}
self.attachment_definitions = self.attachment_definitions.dup
end
- attachment_definitions[name] = {:validations => []}.merge(options)
+ attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name
Paperclip.check_for_url_clash(name,attachment_definitions[name][:url],self.name)
@@ -0,0 +1,19 @@
+module Paperclip
+ class AttachmentOptions
+ def initialize(options)
+ @options = {:validations => []}.merge(options)
+ end
+
+ def [](key)
+ @options[key]
+ end
+
+ def []=(key, value)
+ @options[key] = value
+ end
+
+ def to_hash
+ @options
+ end
+ end
+end
@@ -0,0 +1,34 @@
+require './test/helper'
+
+class AttachmentOptionsTest < Test::Unit::TestCase
+ should "exist" do
+ Paperclip::AttachmentOptions
+ end
+
+ should "add a default empty validations" do
+ options = {:arbi => :trary}
+ expected = {:validations => []}.merge(options)
+ actual = Paperclip::AttachmentOptions.new(options).to_hash
+ assert_equal expected, actual
+ end
+
+ should "respond to []" do
+ Paperclip::AttachmentOptions.new({})[:foo]
+ end
+
+ should "deliver the specified options through []" do
+ intended_options = {:specific_key => "specific value"}
+ attachment_options = Paperclip::AttachmentOptions.new(intended_options)
+ assert_equal "specific value", attachment_options[:specific_key]
+ end
+
+ should "respond to []=" do
+ Paperclip::AttachmentOptions.new({})[:foo] = "bar"
+ end
+
+ should "remember options set with []=" do
+ attachment_options = Paperclip::AttachmentOptions.new({})
+ attachment_options[:foo] = "bar"
+ assert_equal "bar", attachment_options[:foo]
+ end
+end
View
@@ -54,10 +54,15 @@ def setup
ActiveRecord::Base.establish_connection(config['test'])
Paperclip.options[:logger] = ActiveRecord::Base.logger
-Dir[File.join(File.dirname(__FILE__), 'support','*')].each do |f|
- require f
+def require_everything_in_directory(directory_name)
+ Dir[File.join(File.dirname(__FILE__), directory_name, '*')].each do |f|
+ require f
+ end
end
+require_everything_in_directory('support')
+require_everything_in_directory('mocks')
+
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil
@@ -0,0 +1,5 @@
+class MockAttachmentOptions
+ def [](key)
+ nil
+ end
+end
View
@@ -98,6 +98,13 @@ class Dummy2 < ActiveRecord::Base
end
end
+ context "An ActiveRecord model responding to has_attached_file" do
+ should "pass the options to Paperclip::AttachmentOptions.new" do
+ Paperclip::AttachmentOptions.expects(:new).with({"test" => "hash"}).returns(MockAttachmentOptions.new)
+ rebuild_model "test" => "hash"
+ end
+ end
+
context "An ActiveRecord model with an 'avatar' attachment" do
setup do
rebuild_model :path => "tmp/:class/omg/:style.:extension"

0 comments on commit 5f8140d

Please sign in to comment.