paperclip 4.2.1 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +1066 -0
  3. data/.rubocop.yml +1 -0
  4. data/.travis.yml +5 -3
  5. data/Appraisals +2 -7
  6. data/CONTRIBUTING.md +3 -3
  7. data/Gemfile +2 -1
  8. data/LICENSE +1 -1
  9. data/NEWS +57 -30
  10. data/README.md +137 -77
  11. data/RELEASING.md +17 -0
  12. data/Rakefile +1 -1
  13. data/features/basic_integration.feature +7 -4
  14. data/features/step_definitions/attachment_steps.rb +7 -1
  15. data/gemfiles/3.2.gemfile +2 -1
  16. data/gemfiles/4.1.gemfile +2 -1
  17. data/gemfiles/4.2.gemfile +3 -2
  18. data/lib/paperclip/attachment.rb +3 -1
  19. data/lib/paperclip/attachment_registry.rb +1 -1
  20. data/lib/paperclip/content_type_detector.rb +26 -11
  21. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  22. data/lib/paperclip/glue.rb +1 -1
  23. data/lib/paperclip/has_attached_file.rb +2 -1
  24. data/lib/paperclip/interpolations.rb +1 -1
  25. data/lib/paperclip/io_adapters/abstract_adapter.rb +1 -0
  26. data/lib/paperclip/locales/en.yml +1 -1
  27. data/lib/paperclip/media_type_spoof_detector.rb +38 -18
  28. data/lib/paperclip/rails_environment.rb +25 -0
  29. data/lib/paperclip/storage/fog.rb +4 -4
  30. data/lib/paperclip/storage/s3.rb +2 -3
  31. data/lib/paperclip/thumbnail.rb +2 -3
  32. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +1 -1
  33. data/lib/paperclip/version.rb +1 -1
  34. data/lib/paperclip.rb +13 -3
  35. data/lib/tasks/paperclip.rake +16 -0
  36. data/paperclip.gemspec +9 -10
  37. data/spec/paperclip/attachment_definitions_spec.rb +1 -1
  38. data/spec/paperclip/attachment_registry_spec.rb +56 -13
  39. data/spec/paperclip/attachment_spec.rb +57 -19
  40. data/spec/paperclip/content_type_detector_spec.rb +8 -1
  41. data/spec/paperclip/file_command_content_type_detector_spec.rb +0 -1
  42. data/spec/paperclip/interpolations_spec.rb +2 -9
  43. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +2 -1
  44. data/spec/paperclip/io_adapters/file_adapter_spec.rb +4 -1
  45. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +4 -0
  46. data/spec/paperclip/media_type_spoof_detector_spec.rb +31 -7
  47. data/spec/paperclip/paperclip_spec.rb +1 -1
  48. data/spec/paperclip/rails_environment_spec.rb +33 -0
  49. data/spec/paperclip/storage/fog_spec.rb +11 -2
  50. data/spec/paperclip/storage/s3_spec.rb +43 -25
  51. data/spec/paperclip/tempfile_factory_spec.rb +4 -0
  52. data/spec/paperclip/thumbnail_spec.rb +16 -0
  53. data/spec/paperclip/url_generator_spec.rb +1 -1
  54. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +1 -1
  55. data/spec/support/fake_model.rb +4 -0
  56. data/spec/support/fixtures/empty.xlsx +0 -0
  57. data/spec/support/matchers/have_column.rb +11 -2
  58. metadata +49 -24
  59. data/RUNNING_TESTS.md +0 -4
  60. data/gemfiles/4.0.gemfile +0 -19
  61. data/spec/support/mock_model.rb +0 -2
@@ -2,7 +2,7 @@ module Paperclip
2
2
  class ContentTypeDetector
3
3
  # The content-type detection strategy is as follows:
4
4
  #
5
- # 1. Blank/Empty files: If there's no filename or the file is empty,
5
+ # 1. Blank/Empty files: If there's no filepath or the file is empty,
6
6
  # provide a sensible default (application/octet-stream or inode/x-empty)
7
7
  #
8
8
  # 2. Calculated match: Return the first result that is found by both the
@@ -20,8 +20,8 @@ module Paperclip
20
20
  EMPTY_TYPE = "inode/x-empty"
21
21
  SENSIBLE_DEFAULT = "application/octet-stream"
22
22
 
23
- def initialize(filename)
24
- @filename = filename
23
+ def initialize(filepath)
24
+ @filepath = filepath
25
25
  end
26
26
 
27
27
  # Returns a String describing the file's content type
@@ -33,32 +33,47 @@ module Paperclip
33
33
  elsif calculated_type_matches.any?
34
34
  calculated_type_matches.first
35
35
  else
36
- type_from_file_command || SENSIBLE_DEFAULT
36
+ type_from_file_contents || SENSIBLE_DEFAULT
37
37
  end.to_s
38
38
  end
39
39
 
40
40
  private
41
41
 
42
+ def blank_name?
43
+ @filepath.nil? || @filepath.empty?
44
+ end
45
+
42
46
  def empty_file?
43
- File.exist?(@filename) && File.size(@filename) == 0
47
+ File.exist?(@filepath) && File.size(@filepath) == 0
44
48
  end
45
49
 
46
50
  alias :empty? :empty_file?
47
51
 
48
- def blank_name?
49
- @filename.nil? || @filename.empty?
52
+ def calculated_type_matches
53
+ possible_types.select do |content_type|
54
+ content_type == type_from_file_contents
55
+ end
50
56
  end
51
57
 
52
58
  def possible_types
53
- MIME::Types.type_for(@filename).collect(&:content_type)
59
+ MIME::Types.type_for(@filepath).collect(&:content_type)
54
60
  end
55
61
 
56
- def calculated_type_matches
57
- possible_types.select{|content_type| content_type == type_from_file_command }
62
+ def type_from_file_contents
63
+ type_from_mime_magic || type_from_file_command
64
+ rescue Errno::ENOENT => e
65
+ Paperclip.log("Error while determining content type: #{e}")
66
+ SENSIBLE_DEFAULT
67
+ end
68
+
69
+ def type_from_mime_magic
70
+ @type_from_mime_magic ||=
71
+ MimeMagic.by_magic(File.open(@filepath)).try(:type)
58
72
  end
59
73
 
60
74
  def type_from_file_command
61
- @type_from_file_command ||= FileCommandContentTypeDetector.new(@filename).detect
75
+ @type_from_file_command ||=
76
+ FileCommandContentTypeDetector.new(@filepath).detect
62
77
  end
63
78
  end
64
79
  end
@@ -13,20 +13,18 @@ module Paperclip
13
13
  private
14
14
 
15
15
  def type_from_file_command
16
+ # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
16
17
  type = begin
17
- # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
18
- Paperclip.run("file", "-b --mime :file", :file => @filename)
19
- rescue Cocaine::CommandLineError => e
20
- Paperclip.log("Error while determining content type: #{e}")
21
- SENSIBLE_DEFAULT
22
- end
18
+ Paperclip.run("file", "-b --mime :file", file: @filename)
19
+ rescue Cocaine::CommandLineError => e
20
+ Paperclip.log("Error while determining content type: #{e}")
21
+ SENSIBLE_DEFAULT
22
+ end
23
23
 
24
24
  if type.nil? || type.match(/\(.*?\)/)
25
25
  type = SENSIBLE_DEFAULT
26
26
  end
27
27
  type.split(/[:;\s]+/)[0]
28
28
  end
29
-
30
29
  end
31
30
  end
32
-
@@ -8,7 +8,7 @@ module Paperclip
8
8
  base.extend ClassMethods
9
9
  base.send :include, Callbacks
10
10
  base.send :include, Validators
11
- base.send :include, Schema if defined? ActiveRecord
11
+ base.send :include, Schema
12
12
 
13
13
  locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
14
14
  I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
@@ -79,7 +79,8 @@ module Paperclip
79
79
  end
80
80
 
81
81
  def add_required_validations
82
- if @options[:validate_media_type] != false
82
+ options = Paperclip::Attachment.default_options.deep_merge(@options)
83
+ if options[:validate_media_type] != false
83
84
  name = @name
84
85
  @klass.validates_media_type_spoof_detection name,
85
86
  :if => ->(instance){ instance.send(name).dirty? }
@@ -172,7 +172,7 @@ module Paperclip
172
172
  when Integer
173
173
  ("%09d" % id).scan(/\d{3}/).join("/")
174
174
  when String
175
- ('%9.9s' % id).tr(" ", "0").scan(/.{3}/).join("/")
175
+ id.scan(/.{3}/).first(3).join("/")
176
176
  else
177
177
  nil
178
178
  end
@@ -6,6 +6,7 @@ module Paperclip
6
6
 
7
7
  attr_reader :content_type, :original_filename, :size
8
8
  delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile
9
+ alias :length :size
9
10
 
10
11
  def fingerprint
11
12
  @fingerprint ||= Digest::MD5.file(path).to_s
@@ -2,7 +2,7 @@ en:
2
2
  errors:
3
3
  messages:
4
4
  in_between: "must be in between %{min} and %{max}"
5
- spoofed_media_type: "has an extension that does not match its contents"
5
+ spoofed_media_type: "has contents that are not what they are reported to be"
6
6
 
7
7
  number:
8
8
  human:
@@ -1,18 +1,21 @@
1
1
  module Paperclip
2
2
  class MediaTypeSpoofDetector
3
- def self.using(file, name)
4
- new(file, name)
3
+ def self.using(file, name, content_type)
4
+ new(file, name, content_type)
5
5
  end
6
6
 
7
- def initialize(file, name)
7
+ def initialize(file, name, content_type)
8
8
  @file = file
9
9
  @name = name
10
+ @content_type = content_type || ""
10
11
  end
11
12
 
12
13
  def spoofed?
13
14
  if has_name? && has_extension? && media_type_mismatch? && mapping_override_mismatch?
14
- Paperclip.log("Content Type Spoof: Filename #{File.basename(@name)} (#{supplied_file_content_types}), content type discovered from file command: #{calculated_content_type}. See documentation to allow this combination.")
15
+ Paperclip.log("Content Type Spoof: Filename #{File.basename(@name)} (#{supplied_content_type} from Headers, #{content_types_from_name} from Extension), content type discovered from file command: #{calculated_content_type}. See documentation to allow this combination.")
15
16
  true
17
+ else
18
+ false
16
19
  end
17
20
  end
18
21
 
@@ -27,35 +30,44 @@ module Paperclip
27
30
  end
28
31
 
29
32
  def media_type_mismatch?
30
- ! supplied_file_media_types.include?(calculated_media_type)
33
+ supplied_type_mismatch? || calculated_type_mismatch?
34
+ end
35
+
36
+ def supplied_type_mismatch?
37
+ supplied_media_type.present? && !media_types_from_name.include?(supplied_media_type)
38
+ end
39
+
40
+ def calculated_type_mismatch?
41
+ !media_types_from_name.include?(calculated_media_type)
31
42
  end
32
43
 
33
44
  def mapping_override_mismatch?
34
- mapped_content_type != calculated_content_type
45
+ !Array(mapped_content_type).include?(calculated_content_type)
35
46
  end
36
47
 
37
- def supplied_file_media_types
38
- @supplied_file_media_types ||= MIME::Types.type_for(@name).collect(&:media_type)
48
+
49
+ def supplied_content_type
50
+ @content_type
39
51
  end
40
52
 
41
- def calculated_media_type
42
- @calculated_media_type ||= calculated_content_type.split("/").first
53
+ def supplied_media_type
54
+ @content_type.split("/").first
43
55
  end
44
56
 
45
- def supplied_file_content_types
46
- @supplied_file_content_types ||= MIME::Types.type_for(@name).collect(&:content_type)
57
+ def content_types_from_name
58
+ @content_types_from_name ||= MIME::Types.type_for(@name)
47
59
  end
48
60
 
49
- def calculated_content_type
50
- @calculated_content_type ||= type_from_file_command.chomp
61
+ def media_types_from_name
62
+ @media_types_from_name ||= content_types_from_name.collect(&:media_type)
51
63
  end
52
64
 
53
- def mapped_content_type
54
- Paperclip.options[:content_type_mappings][filename_extension]
65
+ def calculated_content_type
66
+ @calculated_content_type ||= type_from_file_command.chomp
55
67
  end
56
68
 
57
- def filename_extension
58
- File.extname(@name.to_s.downcase).sub(/^\./, '').to_sym
69
+ def calculated_media_type
70
+ @calculated_media_type ||= calculated_content_type.split("/").first
59
71
  end
60
72
 
61
73
  def type_from_file_command
@@ -65,5 +77,13 @@ module Paperclip
65
77
  ""
66
78
  end
67
79
  end
80
+
81
+ def mapped_content_type
82
+ Paperclip.options[:content_type_mappings][filename_extension]
83
+ end
84
+
85
+ def filename_extension
86
+ File.extname(@name.to_s.downcase).sub(/^\./, '').to_sym
87
+ end
68
88
  end
69
89
  end
@@ -0,0 +1,25 @@
1
+ module Paperclip
2
+ class RailsEnvironment
3
+ def self.get
4
+ new.get
5
+ end
6
+
7
+ def get
8
+ if rails_exists? && rails_environment_exists?
9
+ Rails.env
10
+ else
11
+ nil
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def rails_exists?
18
+ Object.const_defined?("Rails")
19
+ end
20
+
21
+ def rails_environment_exists?
22
+ Rails.respond_to?(:env)
23
+ end
24
+ end
25
+ end
@@ -141,8 +141,9 @@ module Paperclip
141
141
 
142
142
  def expiring_url(time = (Time.now + 3600), style_name = default_style)
143
143
  time = convert_time(time)
144
- if path(style_name) && directory.files.respond_to?(:get_http_url)
145
- expiring_url = directory.files.get_http_url(path(style_name), time)
144
+ http_url_method = "get_#{scheme}_url"
145
+ if path(style_name) && directory.files.respond_to?(http_url_method)
146
+ expiring_url = directory.files.public_send(http_url_method, path(style_name), time)
146
147
 
147
148
  if @options[:fog_host]
148
149
  expiring_url.gsub!(/#{host_name_for_directory}/, dynamic_fog_host_for_style(style_name))
@@ -156,8 +157,7 @@ module Paperclip
156
157
 
157
158
  def parse_credentials(creds)
158
159
  creds = find_credentials(creds).stringify_keys
159
- env = Object.const_defined?(:Rails) ? Rails.env : nil
160
- (creds[env] || creds).symbolize_keys
160
+ (creds[RailsEnvironment.get] || creds).symbolize_keys
161
161
  end
162
162
 
163
163
  def copy_to_local_file(style, local_dest_path)
@@ -4,7 +4,7 @@ module Paperclip
4
4
  # distribution. You can find out more about it at http://aws.amazon.com/s3
5
5
  #
6
6
  # To use Paperclip with S3, include the +aws-sdk+ gem in your Gemfile:
7
- # gem 'aws-sdk'
7
+ # gem 'aws-sdk', '~> 1.6'
8
8
  # There are a few S3-specific options for has_attached_file:
9
9
  # * +s3_credentials+: Takes a path, a File, a Hash or a Proc. The path (or File) must point
10
10
  # to a YAML file containing the +access_key_id+ and +secret_access_key+ that Amazon
@@ -288,8 +288,7 @@ module Paperclip
288
288
  def parse_credentials creds
289
289
  creds = creds.respond_to?('call') ? creds.call(self) : creds
290
290
  creds = find_credentials(creds).stringify_keys
291
- env = Object.const_defined?(:Rails) ? Rails.env : nil
292
- (creds[env] || creds).symbolize_keys
291
+ (creds[RailsEnvironment.get] || creds).symbolize_keys
293
292
  end
294
293
 
295
294
  def exists?(style = default_style)
@@ -29,7 +29,6 @@ module Paperclip
29
29
  super
30
30
 
31
31
  geometry = options[:geometry].to_s
32
- @file = file
33
32
  @crop = geometry[-1,1] == '#'
34
33
  @target_geometry = options.fetch(:string_geometry_parser, Geometry).parse(geometry)
35
34
  @current_geometry = options.fetch(:file_geometry_parser, Geometry).from_file(@file)
@@ -64,8 +63,8 @@ module Paperclip
64
63
  # that contains the new image.
65
64
  def make
66
65
  src = @file
67
- dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
68
- dst.binmode
66
+ filename = [@basename, @format ? ".#{@format}" : ""].join
67
+ dst = TempfileFactory.new.generate(filename)
69
68
 
70
69
  begin
71
70
  parameters = []
@@ -5,7 +5,7 @@ module Paperclip
5
5
  class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, value)
7
7
  adapter = Paperclip.io_adapters.for(value)
8
- if Paperclip::MediaTypeSpoofDetector.using(adapter, value.original_filename).spoofed?
8
+ if Paperclip::MediaTypeSpoofDetector.using(adapter, value.original_filename, value.content_type).spoofed?
9
9
  record.errors.add(attribute, :spoofed_media_type)
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "4.2.1" unless defined? Paperclip::VERSION
2
+ VERSION = "4.3.0" unless defined? Paperclip::VERSION
3
3
  end
data/lib/paperclip.rb CHANGED
@@ -55,11 +55,21 @@ require 'paperclip/helpers'
55
55
  require 'paperclip/has_attached_file'
56
56
  require 'paperclip/attachment_registry'
57
57
  require 'paperclip/filename_cleaner'
58
- require 'mime/types'
58
+ require 'paperclip/rails_environment'
59
+
60
+ begin
61
+ # Use mime/types/columnar if available, for reduced memory usage
62
+ require "mime/types/columnar"
63
+ rescue LoadError
64
+ require "mime/types"
65
+ end
66
+
67
+ require 'mimemagic'
68
+ require 'mimemagic/overlay'
59
69
  require 'logger'
60
70
  require 'cocaine'
61
71
 
62
- require 'paperclip/railtie' if defined?(Rails)
72
+ require 'paperclip/railtie' if defined?(Rails::Railtie)
63
73
 
64
74
  # The base module that gets included in ActiveRecord::Base. See the
65
75
  # documentation for Paperclip::ClassMethods for more useful information.
@@ -136,7 +146,7 @@ module Paperclip
136
146
  # user.avatar.url # => "/avatars/23/normal_me.png"
137
147
  # * +keep_old_files+: Keep the existing attachment files (original + resized) from
138
148
  # being automatically deleted when an attachment is cleared or updated. Defaults to +false+.
139
- # * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
149
+ # * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
140
150
  # record is destroyed. Defaults to +false+.
141
151
  # * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
142
152
  # to a command line error. This will override the global setting for this attachment.
@@ -108,4 +108,20 @@ namespace :paperclip do
108
108
  end
109
109
  end
110
110
  end
111
+
112
+ desc "find missing attachments. Useful to know which attachments are broken"
113
+ task :find_broken_attachments => :environment do
114
+ klass = Paperclip::Task.obtain_class
115
+ names = Paperclip::Task.obtain_attachments(klass)
116
+ names.each do |name|
117
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
118
+ attachment = instance.send(name)
119
+ if attachment.exists?
120
+ print "."
121
+ else
122
+ Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
123
+ end
124
+ end
125
+ end
126
+ end
111
127
  end
data/paperclip.gemspec CHANGED
@@ -12,8 +12,6 @@ Gem::Specification.new do |s|
12
12
  s.description = "Easy upload management for ActiveRecord"
13
13
  s.license = "MIT"
14
14
 
15
- s.rubyforge_project = "paperclip"
16
-
17
15
  s.files = `git ls-files`.split("\n")
18
16
  s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
19
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -22,30 +20,31 @@ Gem::Specification.new do |s|
22
20
  s.requirements << "ImageMagick"
23
21
  s.required_ruby_version = ">= 1.9.2"
24
22
 
25
- s.add_dependency('activemodel', '>= 3.0.0')
26
- s.add_dependency('activesupport', '>= 3.0.0')
27
- s.add_dependency('cocaine', '~> 0.5.3')
23
+ s.add_dependency('activemodel', '>= 3.2.0')
24
+ s.add_dependency('activesupport', '>= 3.2.0')
25
+ s.add_dependency('cocaine', '~> 0.5.5')
28
26
  s.add_dependency('mime-types')
27
+ s.add_dependency('mimemagic', '0.3.0')
29
28
 
30
- s.add_development_dependency('activerecord', '>= 3.0.0')
29
+ s.add_development_dependency('activerecord', '>= 3.2.0')
31
30
  s.add_development_dependency('shoulda')
32
31
  s.add_development_dependency('rspec')
33
32
  s.add_development_dependency('appraisal')
34
33
  s.add_development_dependency('mocha')
35
- s.add_development_dependency('aws-sdk', '>= 1.5.7')
34
+ s.add_development_dependency('aws-sdk', '>= 1.5.7', "<= 2.0")
36
35
  s.add_development_dependency('bourne')
37
- s.add_development_dependency('cucumber', '~> 1.3.11')
36
+ s.add_development_dependency('cucumber', '~> 1.3.18')
38
37
  s.add_development_dependency('aruba')
39
38
  s.add_development_dependency('nokogiri')
40
39
  # Ruby version < 1.9.3 can't install capybara > 2.0.3.
41
- s.add_development_dependency('capybara', '= 2.0.3')
40
+ s.add_development_dependency('capybara')
42
41
  s.add_development_dependency('bundler')
43
42
  s.add_development_dependency('fog', '~> 1.0')
44
43
  s.add_development_dependency('launchy')
45
44
  s.add_development_dependency('rake')
46
45
  s.add_development_dependency('fakeweb')
47
46
  s.add_development_dependency('railties')
48
- s.add_development_dependency('actionmailer', '>= 3.0.0')
47
+ s.add_development_dependency('actionmailer', '>= 3.2.0')
49
48
  s.add_development_dependency('generator_spec')
50
49
  s.add_development_dependency('timecop')
51
50
  end
@@ -8,6 +8,6 @@ describe "Attachment Definitions" do
8
8
  Dummy.do_not_validate_attachment_file_type :avatar
9
9
  expected = {avatar: {path: "abc"}, other_attachment: {url: "123"}}
10
10
 
11
- assert_equal expected, Dummy.attachment_definitions
11
+ expect(Dummy.attachment_definitions).to eq expected
12
12
  end
13
13
  end
@@ -31,8 +31,8 @@ describe 'Attachment Registry' do
31
31
  it 'calls the block with the class, attachment name, and options' do
32
32
  foo = Class.new
33
33
  expected_accumulations = [
34
- [foo, :avatar, { yo: 'greeting' }],
35
- [foo, :greeter, { ciao: 'greeting' }]
34
+ [foo, :avatar, { yo: "greeting" }],
35
+ [foo, :greeter, { ciao: "greeting" }]
36
36
  ]
37
37
  expected_accumulations.each do |args|
38
38
  Paperclip::AttachmentRegistry.register(*args)
@@ -50,25 +50,64 @@ describe 'Attachment Registry' do
50
50
  context '.definitions_for' do
51
51
  it 'produces the attachment name and options' do
52
52
  expected_definitions = {
53
- avatar: { yo: 'greeting' },
54
- greeter: { ciao: 'greeting' }
53
+ avatar: { yo: "greeting" },
54
+ greeter: { ciao: "greeting" }
55
55
  }
56
56
  foo = Class.new
57
- Paperclip::AttachmentRegistry.register(foo, :avatar, { yo: 'greeting' })
58
- Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })
57
+ Paperclip::AttachmentRegistry.register(
58
+ foo,
59
+ :avatar,
60
+ yo: "greeting"
61
+ )
62
+ Paperclip::AttachmentRegistry.register(
63
+ foo,
64
+ :greeter,
65
+ ciao: "greeting"
66
+ )
59
67
 
60
68
  definitions = Paperclip::AttachmentRegistry.definitions_for(foo)
61
69
 
62
70
  assert_equal expected_definitions, definitions
63
71
  end
64
72
 
65
- it "produces defintions for subclasses" do
66
- expected_definitions = { avatar: { yo: 'greeting' } }
67
- Foo = Class.new
68
- Bar = Class.new(Foo)
69
- Paperclip::AttachmentRegistry.register(Foo, :avatar, expected_definitions[:avatar])
73
+ it 'produces defintions for subclasses' do
74
+ expected_definitions = { avatar: { yo: "greeting" } }
75
+ foo = Class.new
76
+ bar = Class.new(foo)
77
+ Paperclip::AttachmentRegistry.register(
78
+ foo,
79
+ :avatar,
80
+ expected_definitions[:avatar]
81
+ )
82
+
83
+ definitions = Paperclip::AttachmentRegistry.definitions_for(bar)
84
+
85
+ assert_equal expected_definitions, definitions
86
+ end
70
87
 
71
- definitions = Paperclip::AttachmentRegistry.definitions_for(Bar)
88
+ it 'produces defintions for subclasses but deep merging them' do
89
+ foo_definitions = { avatar: { yo: "greeting" } }
90
+ bar_definitions = { avatar: { ciao: "greeting" } }
91
+ expected_definitions = {
92
+ avatar: {
93
+ yo: "greeting",
94
+ ciao: "greeting"
95
+ }
96
+ }
97
+ foo = Class.new
98
+ bar = Class.new(foo)
99
+ Paperclip::AttachmentRegistry.register(
100
+ foo,
101
+ :avatar,
102
+ foo_definitions[:avatar]
103
+ )
104
+ Paperclip::AttachmentRegistry.register(
105
+ bar,
106
+ :avatar,
107
+ bar_definitions[:avatar]
108
+ )
109
+
110
+ definitions = Paperclip::AttachmentRegistry.definitions_for(bar)
72
111
 
73
112
  assert_equal expected_definitions, definitions
74
113
  end
@@ -77,7 +116,11 @@ describe 'Attachment Registry' do
77
116
  context '.clear' do
78
117
  it 'removes all of the existing attachment definitions' do
79
118
  foo = Class.new
80
- Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })
119
+ Paperclip::AttachmentRegistry.register(
120
+ foo,
121
+ :greeter,
122
+ ciao: "greeting"
123
+ )
81
124
 
82
125
  Paperclip::AttachmentRegistry.clear
83
126