Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 95 lines (79 sloc) 4.35 kb
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
1 require './test/helper'
2
3 class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
4
5 context "Paperclip" do
6 setup do
7 Paperclip.classes_with_attachments = Set.new
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
8 end
9
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
10 teardown do
11 File.unlink(Paperclip.registered_attachments_styles_path) rescue nil
12 end
13
14 should "be able to keep list of models using it" do
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
15 assert_kind_of Set, Paperclip.classes_with_attachments
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
16 assert Paperclip.classes_with_attachments.empty?, 'list should be empty'
17 rebuild_model
7a2e2ed @fxposter Do not save class instances, save their names.
fxposter authored
18 assert_equal ['Dummy'].to_set, Paperclip.classes_with_attachments
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
19 end
20
21 should "enable to get and set path to registered styles file" do
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
22 assert_equal ROOT.join('tmp/public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
23 Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
24 assert_equal '/tmp/config/paperclip_attachments.yml', Paperclip.registered_attachments_styles_path
25 Paperclip.registered_attachments_styles_path = nil
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
26 assert_equal ROOT.join('tmp/public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
27 end
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
28
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
29 should "be able to get current attachment styles" do
30 assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
31 rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
32 expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
33 assert_equal expected_hash, Paperclip.send(:current_attachments_styles)
34 end
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
35
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
36 should "be able to save current attachment styles for further comparison" do
37 rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
38 Paperclip.save_current_attachments_styles!
39 expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
40 assert_equal expected_hash, YAML.load_file(Paperclip.registered_attachments_styles_path)
41 end
42
43 should "be able to read registered attachment styles from file" do
44 rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
45 Paperclip.save_current_attachments_styles!
46 expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
47 assert_equal expected_hash, Paperclip.send(:get_registered_attachments_styles)
48 end
49
50 should "be able to calculate differences between registered styles and current styles" do
51 rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
52 Paperclip.save_current_attachments_styles!
53 rebuild_model :styles => {:thumb => 'x100', :export => 'x400>', :croppable => '600x600>', :big => '1000x1000>'}
54 expected_hash = { :Dummy => {:avatar => [:export, :thumb]} }
55 assert_equal expected_hash, Paperclip.missing_attachments_styles
56
57 ActiveRecord::Base.connection.create_table :books, :force => true
58 class ::Book < ActiveRecord::Base
59 has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
60 has_attached_file :sample, :styles => {:thumb => 'x100'}
61 end
62
63 expected_hash = {
64 :Dummy => {:avatar => [:export, :thumb]},
65 :Book => {:sample => [:thumb], :cover => [:large, :small]}
66 }
67 assert_equal expected_hash, Paperclip.missing_attachments_styles
68 Paperclip.save_current_attachments_styles!
69 assert_equal Hash.new, Paperclip.missing_attachments_styles
70 end
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
71
dc53432 @sjmadsen Don't crash when a new attachment is added.
sjmadsen authored
72 should "be able to calculate differences when a new attachment is added to a model" do
73 rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
74 Paperclip.save_current_attachments_styles!
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
75
dc53432 @sjmadsen Don't crash when a new attachment is added.
sjmadsen authored
76 class ::Dummy
77 has_attached_file :photo, :styles => {:small => 'x100', :large => '1000x1000>'}
78 end
b48ec84 @sikachu Fix test failure from earlier test path cleanup
sikachu authored
79
dc53432 @sjmadsen Don't crash when a new attachment is added.
sjmadsen authored
80 expected_hash = {
81 :Dummy => {:photo => [:large, :small]}
82 }
83 assert_equal expected_hash, Paperclip.missing_attachments_styles
84 Paperclip.save_current_attachments_styles!
85 assert_equal Hash.new, Paperclip.missing_attachments_styles
86 end
3ae7a3e @murbanski Tracking new attachment styles with a Rake task to refresh them
murbanski authored
87
88 # It's impossible to build styles hash without loading from database whole bunch of records
89 should "skip lambda-styles" do
90 rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
91 assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
92 end
93 end
94 end
Something went wrong with that request. Please try again.