Skip to content

Commit

Permalink
Change Paperclip::Tasks::Attachment to Paperclip::AttachmentRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jul 23, 2013
1 parent 65cc5c7 commit b62cc2d
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 99 deletions.
2 changes: 1 addition & 1 deletion features/step_definitions/s3_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
definition = Paperclip::Tasks::Attachments.definitions_for(User)[field.downcase.to_sym]
definition = Paperclip::AttachmentRegistry.definitions_for(User)[field.downcase.to_sym]
path = "https://paperclip.s3.amazonaws.com#{definition[:path]}"
path.gsub!(':filename', File.basename(file_path))
path.gsub!(/:([^\/\.]+)/) do |match|
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
require 'paperclip/logger'
require 'paperclip/helpers'
require 'paperclip/has_attached_file'
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'
require 'paperclip/filename_cleaner'
require 'mime/types'
require 'logger'
Expand Down
57 changes: 57 additions & 0 deletions lib/paperclip/attachment_registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'singleton'

module Paperclip
class AttachmentRegistry
include Singleton

def self.register(klass, attachment_name, attachment_options)
instance.register(klass, attachment_name, attachment_options)
end

def self.clear
instance.clear
end

def self.names_for(klass)
instance.names_for(klass)
end

def self.each_definition(&block)
instance.each_definition(&block)
end

def self.definitions_for(klass)
instance.definitions_for(klass)
end

def initialize
clear
end

def register(klass, attachment_name, attachment_options)
@attachments ||= {}
@attachments[klass] ||= {}
@attachments[klass][attachment_name] = attachment_options
end

def clear
@attachments = Hash.new { |h,k| h[k] = {} }
end

def names_for(klass)
@attachments[klass].keys
end

def each_definition
@attachments.each do |klass, attachments|
attachments.each do |name, options|
yield klass, name, options
end
end
end

def definitions_for(klass)
@attachments[klass]
end
end
end
6 changes: 3 additions & 3 deletions lib/paperclip/has_attached_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def define
define_getter
define_setter
define_query
register_with_rake_tasks
register_new_attachment
add_active_record_callbacks
add_paperclip_callbacks
end
Expand Down Expand Up @@ -66,8 +66,8 @@ def define_query
end
end

def register_with_rake_tasks
Paperclip::Tasks::Attachments.add(@klass, @name, @options)
def register_new_attachment
Paperclip::AttachmentRegistry.register(@klass, @name, @options)
end

def add_active_record_callbacks
Expand Down
4 changes: 2 additions & 2 deletions lib/paperclip/missing_attachment_styles.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'
require 'set'

module Paperclip
Expand Down Expand Up @@ -34,7 +34,7 @@ def self.save_current_attachments_styles!
# }
def self.current_attachments_styles
Hash.new.tap do |current_styles|
Paperclip::Tasks::Attachments.each_definition do |klass, attachment_name, attachment_attributes|
Paperclip::AttachmentRegistry.each_definition do |klass, attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
Expand Down
59 changes: 0 additions & 59 deletions lib/paperclip/tasks/attachments.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/tasks/paperclip.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'

module Paperclip
module Task
Expand All @@ -12,7 +12,7 @@ module Paperclip
klass = Paperclip.class_for(klass.to_s)
name = ENV['ATTACHMENT'] || ENV['attachment']

attachment_names = Paperclip::Tasks::Attachments.names_for(klass)
attachment_names = Paperclip::AttachmentRegistry.names_for(klass)

if attachment_names.empty?
raise "Class #{klass.name} has no attachments specified"
Expand Down
1 change: 1 addition & 0 deletions paperclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('fakeweb')
s.add_development_dependency('railties')
s.add_development_dependency('actionmailer', '>= 3.0.0')
s.add_development_dependency('protected_attributes')
end
34 changes: 17 additions & 17 deletions test/tasks/attachments_test.rb → test/attachment_registry_test.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
require './test/helper'
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'

class AttachmentsTest < Test::Unit::TestCase
class AttachmentRegistryTest < Test::Unit::TestCase
def setup
Paperclip::Tasks::Attachments.clear
Paperclip::AttachmentRegistry.clear
end

context '.names_for' do
should 'include attachment names for the given class' do
foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, {})
Paperclip::AttachmentRegistry.register(foo, :avatar, {})

assert_equal [:avatar], Paperclip::Tasks::Attachments.names_for(foo)
assert_equal [:avatar], Paperclip::AttachmentRegistry.names_for(foo)
end

should 'not include attachment names for other classes' do
foo = Class.new
bar = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, {})
Paperclip::Tasks::Attachments.add(bar, :lover, {})
Paperclip::AttachmentRegistry.register(foo, :avatar, {})
Paperclip::AttachmentRegistry.register(bar, :lover, {})

assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar)
assert_equal [:lover], Paperclip::AttachmentRegistry.names_for(bar)
end

should 'produce the empty array for a missing key' do
assert_empty Paperclip::Tasks::Attachments.names_for(Class.new)
assert_empty Paperclip::AttachmentRegistry.names_for(Class.new)
end
end

Expand All @@ -36,11 +36,11 @@ def setup
[foo, :greeter, { ciao: 'greeting' }]
]
expected_accumulations.each do |args|
Paperclip::Tasks::Attachments.add(*args)
Paperclip::AttachmentRegistry.register(*args)
end
accumulations = []

Paperclip::Tasks::Attachments.each_definition do |*args|
Paperclip::AttachmentRegistry.each_definition do |*args|
accumulations << args
end

Expand All @@ -55,10 +55,10 @@ def setup
greeter: { ciao: 'greeting' }
}
foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, { yo: 'greeting' })
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' })
Paperclip::AttachmentRegistry.register(foo, :avatar, { yo: 'greeting' })
Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })

definitions = Paperclip::Tasks::Attachments.definitions_for(foo)
definitions = Paperclip::AttachmentRegistry.definitions_for(foo)

assert_equal expected_definitions, definitions
end
Expand All @@ -67,11 +67,11 @@ def setup
context '.clear' do
should 'remove all of the existing attachment definitions' do
foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' })
Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })

Paperclip::Tasks::Attachments.clear
Paperclip::AttachmentRegistry.clear

assert_empty Paperclip::Tasks::Attachments.names_for(foo)
assert_empty Paperclip::AttachmentRegistry.names_for(foo)
end
end
end
5 changes: 4 additions & 1 deletion test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ class AttachmentTest < Test::Unit::TestCase
dummy = Dummy.new
dummy.id = 1234
dummy.avatar_file_name = "fake.jpg"
expected_string = '{"dummy":{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}}'
expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
expected_string = %({"dummy":#{expected_string}})
end
# active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
assert_equal expected_string, dummy.to_json(:only => [:dummy_key_for_old_active_model], :methods => [:avatar])
end
Expand Down
10 changes: 5 additions & 5 deletions test/has_attached_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class HasAttachedFileTest < Test::Unit::TestCase
assert_adding_attachment('avatar').defines_validation
end

should 'register the attachment with Paperclip::Tasks' do
assert_adding_attachment('avatar').registers_with_tasks
should 'register the attachment with Paperclip::AttachmentRegistry' do
assert_adding_attachment('avatar').registers_attachment
end

should 'define an after_save callback' do
Expand Down Expand Up @@ -74,13 +74,13 @@ def defines_validation
end
end

def registers_with_tasks
def registers_attachment
a_class = stub_class
Paperclip::Tasks::Attachments.stubs(:add)
Paperclip::AttachmentRegistry.stubs(:register)

Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1})

assert_received(Paperclip::Tasks::Attachments, :add) do |expect|
assert_received(Paperclip::AttachmentRegistry, :register) do |expect|
expect.with(a_class, @attachment_name, {size: 1})
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
require 'tempfile'
require 'pathname'
require 'test/unit'

require 'shoulda'
require 'mocha/setup'
require 'bourne'

require 'active_record'
require 'active_record/version'
require 'active_support'
require 'active_support/core_ext'
require 'shoulda'
require 'mocha/setup'
require 'bourne'
require 'shoulda/context'
require 'mime/types'
require 'pathname'
require 'ostruct'
require 'pry'
require 'protected_attributes'

puts "Testing against version #{ActiveRecord::VERSION::STRING}"

Expand Down Expand Up @@ -48,7 +48,7 @@ def setup

FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.logger = ActiveSupport::Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test'])
Paperclip.options[:logger] = ActiveRecord::Base.logger

Expand Down
4 changes: 2 additions & 2 deletions test/paperclip_missing_attachment_styles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
def setup
Paperclip::Tasks::Attachments.clear
Paperclip::AttachmentRegistry.clear
end

context "Paperclip" do
setup do
Paperclip::Tasks::Attachments.clear
Paperclip::AttachmentRegistry.clear
end

teardown do
Expand Down

0 comments on commit b62cc2d

Please sign in to comment.