Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

replace Paperclip::Attachment#initialize options with an AttachmentOptions instance #729

Merged
merged 5 commits into from

3 participants

Luke Griffiths Prem Sichanugrist Mike Burns
Luke Griffiths

No description provided.

Prem Sichanugrist
Admin

Go ahead and merge this in, Luke.

Luke Griffiths

Sorry in advance about this:

There is now completely new work in this pull request that's not related to the original ctags change.

The new work is relating to how options are passed in to Paperclip::Attachment.new - using an object which inherits from Hash.

This warrants a completely new code review.

Also, sikachu, I don't believe I have access to push to github.com:thoughtbot/paperclip.git. That's why I've been sending pull requests from my own fork.

test/attachment_options_test.rb
@@ -0,0 +1,45 @@
+require './test/helper'
+
+class AttachmentOptionsTest < Test::Unit::TestCase
+ should "exist" do
+ Paperclip::AttachmentOptions
+ end
Mike Burns Admin

This test is implied by other, more behavioral tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/attachment_options_test.rb
@@ -0,0 +1,45 @@
+require './test/helper'
+
+class AttachmentOptionsTest < Test::Unit::TestCase
+ should "exist" do
+ Paperclip::AttachmentOptions
+ end
+
+ should "be a Hash" do
+ attachment_options = Paperclip::AttachmentOptions.new({})
+ assert attachment_options.is_a?(Hash), "attachment_options is not a Hash"
Mike Burns Admin
assert_kind_of
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/attachment_options_test.rb
((12 lines not shown))
+
+ 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 "not override validations if passed to initializer" do
+ options = {:validations => "something"}
+ attachment_options = Paperclip::AttachmentOptions.new(options)
+ assert_equal "something", attachment_options[:validations]
+ end
+
+ should "respond to []" do
+ Paperclip::AttachmentOptions.new({})[:foo]
Mike Burns Admin
assert Paperclip::AttachmentOptions.new({}).respond_to(:[])

Bonus, you get to use (:[]), which looks like a monkey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/paperclip_test.rb
@@ -98,6 +98,13 @@ class Dummy2 < ActiveRecord::Base
end
end
+ context "An ActiveRecord model responding to has_attached_file" do
Mike Burns Admin

This is testing deep inside the implementation, which will make it harder to refactor. Is there some behavior you need to test, or perhaps it's already testing and this is just a refactoring.

I think I'll just remove this test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/mocks/mock_attachment_options.rb
@@ -0,0 +1,5 @@
+class MockAttachmentOptions
Mike Burns Admin

This is unused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Luke Griffiths Sporky023 merged commit 7f948f8 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  .gitignore
View
@@ -20,3 +20,4 @@ capybara*.html
*SPIKE*
*emfile.lock
+tags
3  lib/paperclip.rb
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)
10 lib/paperclip/attachment_options.rb
View
@@ -0,0 +1,10 @@
+module Paperclip
+ class AttachmentOptions < Hash
+ def initialize(options)
+ options = {:validations => []}.merge(options)
+ options.each do |k, v|
+ self.[]=(k, v)
+ end
+ end
+ end
+end
40 test/attachment_options_test.rb
View
@@ -0,0 +1,40 @@
+require './test/helper'
+
+class AttachmentOptionsTest < Test::Unit::TestCase
+ should "be a Hash" do
+ assert_kind_of Hash, Paperclip::AttachmentOptions.new({})
+ 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 "not override validations if passed to initializer" do
+ options = {:validations => "something"}
+ attachment_options = Paperclip::AttachmentOptions.new(options)
+ assert_equal "something", attachment_options[:validations]
+ end
+
+ should "respond to []" do
+ assert Paperclip::AttachmentOptions.new({}).respond_to?(:[])
+ 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
+ assert Paperclip::AttachmentOptions.new({}).respond_to?(:[]=)
+ end
+
+ should "remember options set with []=" do
+ attachment_options = Paperclip::AttachmentOptions.new({})
+ attachment_options[:foo] = "bar"
+ assert_equal "bar", attachment_options[:foo]
+ end
+end
8 test/helper.rb
View
@@ -54,10 +54,14 @@ 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')
+
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil
Something went wrong with that request. Please try again.