Commit b62cc2d2 by Jon Yurek

Change Paperclip::Tasks::Attachment to Paperclip::AttachmentRegistry

parent 65cc5c7d
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|
......
......@@ -51,7 +51,7 @@ require 'paperclip/validators'
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'
......
require 'singleton'
module Paperclip
module Tasks
class Attachments
include Singleton
class AttachmentRegistry
include Singleton
def self.add(klass, attachment_name, attachment_options)
instance.add(klass, attachment_name, attachment_options)
end
def self.register(klass, attachment_name, attachment_options)
instance.register(klass, attachment_name, attachment_options)
end
def self.clear
instance.clear
end
def self.clear
instance.clear
end
def self.names_for(klass)
instance.names_for(klass)
end
def self.names_for(klass)
instance.names_for(klass)
end
def self.each_definition(&block)
instance.each_definition(&block)
end
def self.each_definition(&block)
instance.each_definition(&block)
end
def self.definitions_for(klass)
instance.definitions_for(klass)
end
def self.definitions_for(klass)
instance.definitions_for(klass)
end
def initialize
clear
end
def initialize
clear
end
def add(klass, attachment_name, attachment_options)
@attachments ||= {}
@attachments[klass] ||= {}
@attachments[klass][attachment_name] = attachment_options
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 clear
@attachments = Hash.new { |h,k| h[k] = {} }
end
def names_for(klass)
@attachments[klass].keys
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
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
def definitions_for(klass)
@attachments[klass]
end
end
end
......@@ -15,7 +15,7 @@ module Paperclip
define_getter
define_setter
define_query
register_with_rake_tasks
register_new_attachment
add_active_record_callbacks
add_paperclip_callbacks
end
......@@ -66,8 +66,8 @@ module Paperclip
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
......
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'
require 'set'
module Paperclip
......@@ -34,7 +34,7 @@ module Paperclip
# }
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|
......
require 'paperclip/tasks/attachments'
require 'paperclip/attachment_registry'
module Paperclip
module Task
......@@ -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"
......
......@@ -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
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
......@@ -36,11 +36,11 @@ class AttachmentsTest < Test::Unit::TestCase
[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
......@@ -55,10 +55,10 @@ class AttachmentsTest < Test::Unit::TestCase
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
......@@ -67,11 +67,11 @@ class AttachmentsTest < Test::Unit::TestCase
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
......@@ -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
......
......@@ -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
......@@ -74,13 +74,13 @@ class HasAttachedFileTest < Test::Unit::TestCase
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
......
......@@ -2,19 +2,19 @@ require 'rubygems'
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}"
......@@ -48,7 +48,7 @@ require './shoulda_macros/paperclip'
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
......
......@@ -2,12 +2,12 @@ require './test/helper'
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
......
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