avatar 0.0.4 → 0.0.5

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 (49) hide show
  1. data/History.txt +10 -0
  2. data/Manifest.txt +32 -6
  3. data/lib/avatar.rb +1 -1
  4. data/lib/avatar/source/gravatar_source.rb +7 -5
  5. data/lib/avatar/source/paperclip_source.rb +52 -0
  6. data/lib/avatar/source/wrapper.rb +6 -0
  7. data/lib/avatar/source/wrapper/abstract_source_wrapper.rb +33 -0
  8. data/lib/avatar/source/wrapper/rails_asset_source_wrapper.rb +36 -0
  9. data/lib/avatar/source/wrapper/string_substitution_source_wrapper.rb +55 -0
  10. data/lib/avatar/version.rb +1 -1
  11. data/test/lib/paperclip/README +32 -0
  12. data/test/lib/paperclip/Rakefile +41 -0
  13. data/test/lib/paperclip/generators/paperclip/USAGE +5 -0
  14. data/test/lib/paperclip/generators/paperclip/paperclip_generator.rb +27 -0
  15. data/test/lib/paperclip/generators/paperclip/templates/paperclip_migration.rb +17 -0
  16. data/test/lib/paperclip/init.rb +3 -0
  17. data/test/lib/paperclip/lib/paperclip.rb +197 -0
  18. data/test/lib/paperclip/lib/paperclip/attachment.rb +230 -0
  19. data/test/lib/paperclip/lib/paperclip/geometry.rb +109 -0
  20. data/test/lib/paperclip/lib/paperclip/iostream.rb +43 -0
  21. data/test/lib/paperclip/lib/paperclip/thumbnail.rb +80 -0
  22. data/test/lib/paperclip/lib/paperclip/upfile.rb +32 -0
  23. data/test/lib/paperclip/tasks/paperclip_tasks.rake +38 -0
  24. data/test/lib/paperclip/test/database.yml +5 -0
  25. data/test/lib/paperclip/test/fixtures/12k.png +0 -0
  26. data/test/lib/paperclip/test/fixtures/5k.png +0 -0
  27. data/test/lib/paperclip/test/fixtures/bad.png +1 -0
  28. data/test/lib/paperclip/test/helper.rb +38 -0
  29. data/test/lib/paperclip/test/test_attachment.rb +99 -0
  30. data/test/lib/paperclip/test/test_geometry.rb +142 -0
  31. data/test/lib/paperclip/test/test_integration.rb +76 -0
  32. data/test/lib/paperclip/test/test_iostream.rb +60 -0
  33. data/test/lib/paperclip/test/test_paperclip.rb +83 -0
  34. data/test/lib/paperclip/test/test_thumbnail.rb +76 -0
  35. data/test/lib/schema.rb +14 -1
  36. data/test/test_file_column_source.rb +8 -6
  37. data/test/test_gravatar_source.rb +5 -0
  38. data/test/test_helper.rb +7 -2
  39. data/test/test_paperclip_source.rb +52 -0
  40. data/test/test_rails_asset_source_wrapper.rb +37 -0
  41. data/test/test_string_substitution_source_wrapper.rb +25 -0
  42. data/website/index.html +1 -1
  43. metadata +43 -11
  44. data/lib/avatar/source/rails_asset_source.rb +0 -64
  45. data/lib/avatar/source/string_substitution_source.rb +0 -45
  46. data/lib/avatar/string_substitution.rb +0 -28
  47. data/test/test_rails_asset_source.rb +0 -50
  48. data/test/test_string_substitution.rb +0 -28
  49. data/test/test_string_substitution_source.rb +0 -22
@@ -0,0 +1,76 @@
1
+ require 'test/helper.rb'
2
+
3
+ class PaperclipTest < Test::Unit::TestCase
4
+ context "A model with an attachment" do
5
+ setup do
6
+ rebuild_model :styles => { :large => "300x300>",
7
+ :medium => "100x100",
8
+ :thumb => ["32x32#", :gif] },
9
+ :default_style => :medium,
10
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
11
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
12
+ end
13
+
14
+ should "integrate" do
15
+ @dummy = Dummy.new
16
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
17
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
18
+
19
+ assert @dummy.avatar = @file
20
+ assert @dummy.valid?
21
+ assert @dummy.save
22
+
23
+ [["100x15", nil],
24
+ ["434x66", :original],
25
+ ["300x46", :large],
26
+ ["100x15", :medium],
27
+ ["32x32", :thumb]].each do |geo, style|
28
+ cmd = %Q[identify -format "%wx%h" #{@dummy.avatar.to_io(style).path}]
29
+ assert_equal geo, `#{cmd}`.chomp, cmd
30
+ end
31
+
32
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_io(s).path }
33
+
34
+ @d2 = Dummy.find(@dummy.id)
35
+ assert_equal "100x15", `identify -format "%wx%h" #{@dummy.avatar.to_io.path}`.chomp
36
+ assert_equal "434x66", `identify -format "%wx%h" #{@dummy.avatar.to_io(:original).path}`.chomp
37
+ assert_equal "300x46", `identify -format "%wx%h" #{@d2.avatar.to_io(:large).path}`.chomp
38
+ assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_io(:medium).path}`.chomp
39
+ assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_io(:thumb).path}`.chomp
40
+
41
+ @dummy.avatar = nil
42
+ assert_nil @dummy.avatar_file_name
43
+ assert @dummy.valid?
44
+ assert @dummy.save
45
+
46
+ saved_paths.each do |p|
47
+ assert ! File.exists?(p)
48
+ end
49
+
50
+ @d2 = Dummy.find(@dummy.id)
51
+ assert_nil @d2.avatar_file_name
52
+
53
+ @d2.avatar = @bad_file
54
+ assert ! @d2.valid?
55
+ @d2.avatar = nil
56
+ assert @d2.valid?
57
+
58
+ Dummy.validates_attachment_presence :avatar
59
+ @d3 = Dummy.find(@d2.id)
60
+ @d3.avatar = @file
61
+ assert @d3.valid?
62
+ @d3.avatar = @bad_file
63
+ assert ! @d3.valid?
64
+ @d3.avatar = nil
65
+ assert ! @d3.valid?
66
+
67
+ @dummy.avatar = @file
68
+ assert @dummy.save
69
+ @dummy.avatar = nil
70
+ assert_nil @dummy.avatar_file_name
71
+ @dummy.reload
72
+ assert_equal "5k.png", @dummy.avatar_file_name
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'stringio'
4
+ require 'tempfile'
5
+ require 'shoulda'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'iostream.rb')
8
+
9
+ class IOStreamTest < Test::Unit::TestCase
10
+ context "IOStream" do
11
+ should "be included in IO, File, Tempfile, and StringIO" do
12
+ [IO, File, Tempfile, StringIO].each do |klass|
13
+ assert klass.included_modules.include?(IOStream), "Not in #{klass}"
14
+ end
15
+ end
16
+ end
17
+
18
+ context "A file" do
19
+ setup do
20
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
21
+ end
22
+
23
+ context "that is sent #stream_to" do
24
+
25
+ [["/tmp/iostream.string.test", File],
26
+ [Tempfile.new('iostream.test'), Tempfile]].each do |args|
27
+
28
+ context "and given a #{args[0].class.to_s}" do
29
+ setup do
30
+ assert @result = @file.stream_to(args[0])
31
+ end
32
+
33
+ should "return a #{args[1].to_s}" do
34
+ assert @result.is_a?(args[1])
35
+ end
36
+
37
+ should "contain the same data as the original file" do
38
+ @file.rewind; @result.rewind
39
+ assert_equal @file.read, @result.read
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ context "that is sent #to_tempfile" do
46
+ setup do
47
+ assert @tempfile = @file.to_tempfile
48
+ end
49
+
50
+ should "convert it to a Tempfile" do
51
+ assert @tempfile.is_a?(Tempfile)
52
+ end
53
+
54
+ should "have the Tempfile contain the same data as the file" do
55
+ @file.rewind; @tempfile.rewind
56
+ assert_equal @file.read, @tempfile.read
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,83 @@
1
+ require 'test/helper.rb'
2
+
3
+ class PaperclipTest < Test::Unit::TestCase
4
+ context "An ActiveRecord model with an 'avatar' attachment" do
5
+ setup do
6
+ rebuild_model
7
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
8
+ end
9
+
10
+ context "that is attr_protected" do
11
+ setup do
12
+ Dummy.class_eval do
13
+ attr_protected :avatar
14
+ end
15
+ @dummy = Dummy.new
16
+ end
17
+
18
+ should "not assign the avatar on mass-set" do
19
+ @dummy.logger.expects(:debug)
20
+
21
+ @dummy.attributes = { :other => "I'm set!",
22
+ :avatar => @file }
23
+
24
+ assert_equal "I'm set!", @dummy.other
25
+ assert ! @dummy.avatar?
26
+ end
27
+
28
+ should "still allow assigment on normal set" do
29
+ @dummy.logger.expects(:debug).times(0)
30
+
31
+ @dummy.other = "I'm set!"
32
+ @dummy.avatar = @file
33
+
34
+ assert_equal "I'm set!", @dummy.other
35
+ assert @dummy.avatar?
36
+ end
37
+ end
38
+
39
+ should "have an #avatar method" do
40
+ assert Dummy.new.respond_to?(:avatar)
41
+ end
42
+
43
+ should "have an #avatar= method" do
44
+ assert Dummy.new.respond_to?(:avatar=)
45
+ end
46
+
47
+ [[:presence, nil, "5k.png", nil],
48
+ [:size, {:in => 1..10240}, "5k.png", "12k.png"]].each do |args|
49
+ context "with #{args[0]} validations" do
50
+ setup do
51
+ Dummy.class_eval do
52
+ send(*[:"validates_attachment_#{args[0]}", :avatar, args[1]].compact)
53
+ end
54
+ @dummy = Dummy.new
55
+ end
56
+
57
+ context "and a valid file" do
58
+ setup do
59
+ @file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
60
+ end
61
+
62
+ should "not have any errors" do
63
+ @dummy.avatar = @file
64
+ assert @dummy.avatar.valid?
65
+ assert_equal 0, @dummy.avatar.errors.length
66
+ end
67
+ end
68
+
69
+ context "and an invalid file" do
70
+ setup do
71
+ @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
72
+ end
73
+
74
+ should "have errors" do
75
+ @dummy.avatar = @file
76
+ assert ! @dummy.avatar.valid?
77
+ assert_equal 1, @dummy.avatar.errors.length
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,76 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'tempfile'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'thumbnail.rb')
9
+
10
+ class ThumbnailTest < Test::Unit::TestCase
11
+
12
+ context "A Paperclip Tempfile" do
13
+ setup do
14
+ @tempfile = Paperclip::Tempfile.new("file.jpg")
15
+ end
16
+
17
+ should "have its path contain a real extension" do
18
+ assert_equal ".jpg", File.extname(@tempfile.path)
19
+ end
20
+
21
+ should "be a real Tempfile" do
22
+ assert @tempfile.is_a?(::Tempfile)
23
+ end
24
+ end
25
+
26
+ context "Another Paperclip Tempfile" do
27
+ setup do
28
+ @tempfile = Paperclip::Tempfile.new("file")
29
+ end
30
+
31
+ should "not have an extension if not given one" do
32
+ assert_equal "", File.extname(@tempfile.path)
33
+ end
34
+
35
+ should "still be a real Tempfile" do
36
+ assert @tempfile.is_a?(::Tempfile)
37
+ end
38
+ end
39
+
40
+ context "An image" do
41
+ setup do
42
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
43
+ end
44
+
45
+ context "being thumbnailed at 100x50 with cropping" do
46
+ setup do
47
+ @thumb = Paperclip::Thumbnail.new(@file, "100x50#")
48
+ end
49
+
50
+ should "report its correct current and target geometries" do
51
+ assert_equal "100x50#", @thumb.target_geometry.to_s
52
+ assert_equal "434x66", @thumb.current_geometry.to_s
53
+ end
54
+
55
+ should "report its correct format" do
56
+ assert_nil @thumb.format
57
+ end
58
+
59
+ should "have whiny_thumbnails turned on by default" do
60
+ assert @thumb.whiny_thumbnails
61
+ end
62
+
63
+ should "send the right command to convert when sent #make" do
64
+ @thumb.expects(:system).with do |arg|
65
+ arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}"\s+-scale\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+".*?"}
66
+ end
67
+ @thumb.make
68
+ end
69
+
70
+ should "create the thumbnail when sent #make" do
71
+ dst = @thumb.make
72
+ assert_match /100x50/, `identify #{dst.path}`
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,7 +1,20 @@
1
1
  ActiveRecord::Schema.define :version => 0 do
2
- create_table :users, :force => true do |t|
2
+ create_table :file_column_users, :force => true do |t|
3
3
  t.column :name, :text, :nil => false
4
4
  t.column :email, :text, :nil => false
5
5
  t.column :avatar, :string
6
+ t.column :icon, :string
7
+ end
8
+ create_table :paperclip_users, :force => true do |t|
9
+ t.column :name, :text, :nil => false
10
+ t.column :email, :text, :nil => false
11
+
12
+ t.column :avatar_file_name, :string
13
+ t.column :avatar_content_type, :string
14
+ t.column :avatar_file_size, :integer
15
+
16
+ t.column :icon_file_name, :string
17
+ t.column :icon_content_type, :string
18
+ t.column :icon_file_size, :integer
6
19
  end
7
20
  end
@@ -1,8 +1,10 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), ['lib', 'file_column', 'lib']))
1
2
  require File.join(File.dirname(__FILE__), 'test_helper.rb')
3
+ require File.join(File.dirname(__FILE__), ['lib', 'file_column', 'init'])
2
4
  require 'file_column'
3
5
  require 'avatar/source/file_column_source'
4
6
 
5
- class User < ActiveRecord::Base
7
+ class FileColumnUser < ActiveRecord::Base
6
8
  file_column :avatar
7
9
  file_column :icon
8
10
  end
@@ -12,9 +14,9 @@ class TestFileColumnSource < Test::Unit::TestCase
12
14
  def setup
13
15
  @source = Avatar::Source::FileColumnSource.new
14
16
  png = File.new(File.join(File.dirname(__FILE__), ['lib', 'user_suit.png']))
15
- @user_with_avatar = User.create!(:email => 'joe@example.com', :avatar => png)
16
- @user_with_icon = User.create!(:email => 'terry@example.com', :icon => png)
17
- @user_without_avatar = User.create!(:email => 'sue@example.com')
17
+ @user_with_avatar = FileColumnUser.create!(:email => 'joe@example.com', :avatar => png)
18
+ @user_with_icon = FileColumnUser.create!(:email => 'terry@example.com', :icon => png)
19
+ @user_without_avatar = FileColumnUser.create!(:email => 'sue@example.com')
18
20
  end
19
21
 
20
22
  def test_avatar_url_is_nil_if_person_is_nil
@@ -26,11 +28,11 @@ class TestFileColumnSource < Test::Unit::TestCase
26
28
  end
27
29
 
28
30
  def test_avatar_url_for_person_with_avatar
29
- assert_equal "/user/avatar/#{@user_with_avatar.id}/0/user_suit.png", @source.avatar_url_for(@user_with_avatar)
31
+ assert_equal "/file_column_user/avatar/#{@user_with_avatar.id}/0/user_suit.png", @source.avatar_url_for(@user_with_avatar)
30
32
  end
31
33
 
32
34
  def test_avatar_url_for_person_with_icon_and_custom_file_column_field
33
- assert_equal "/user/icon/#{@user_with_icon.id}/0/user_suit.png", @source.avatar_url_for(@user_with_icon, :file_column_field => :icon)
35
+ assert_equal "/file_column_user/icon/#{@user_with_icon.id}/0/user_suit.png", @source.avatar_url_for(@user_with_icon, :file_column_field => :icon)
34
36
  end
35
37
 
36
38
  end
@@ -71,4 +71,9 @@ class TestGravatarSource < Test::Unit::TestCase
71
71
  assert_equal "http://www.gravatar.com/avatar/#{@email_hash}", @source.avatar_url_for(@gary, :r => 'EVIL')
72
72
  end
73
73
 
74
+ def test_default_always_at_end
75
+ assert_equal "http://www.gravatar.com/avatar/#{@email_hash}?rating=any&default=http://gonzo.com", @source.avatar_url_for(@gary, :gravatar_rating => :any, :gravatar_default_url => 'http://gonzo.com')
76
+ assert_equal "http://www.gravatar.com/avatar/#{@email_hash}?size=110&default=http://gonzo.com", @source.avatar_url_for(@gary, :gravatar_size => 110, :gravatar_default_url => 'http://gonzo.com')
77
+ end
78
+
74
79
  end
@@ -7,8 +7,13 @@ require 'active_record'
7
7
  require 'active_support'
8
8
  require 'action_controller'
9
9
  require 'action_view'
10
- $: << File.expand_path(File.join(File.dirname(__FILE__), ['lib', 'file_column', 'lib']))
11
- require File.join(File.dirname(__FILE__), ['lib', 'file_column', 'init'])
10
+
11
+ #$: << File.expand_path(File.join(File.dirname(__FILE__), ['lib', 'file_column', 'lib']))
12
+ #$: << File.expand_path(File.join(File.dirname(__FILE__), ['lib', 'paperclip', 'lib']))
13
+
14
+ #require File.join(File.dirname(__FILE__), ['lib', 'file_column', 'init'])
15
+ #require File.join(File.dirname(__FILE__), ['lib', 'paperclip', 'init'])
16
+
12
17
  require File.join(File.dirname(__FILE__), ['lib', 'database'])
13
18
  require File.join(File.dirname(__FILE__), ['lib', 'schema'])
14
19
  require File.join(File.dirname(__FILE__), ['..', 'lib', 'avatar'])
@@ -0,0 +1,52 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), ['lib', 'paperclip', 'lib']))
2
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
3
+ require File.join(File.dirname(__FILE__), ['lib', 'paperclip', 'init'])
4
+ require 'avatar/source/paperclip_source'
5
+
6
+ class PaperclipUser < ActiveRecord::Base
7
+ has_attached_file :avatar,
8
+ :styles => { :medium => "300x300>",
9
+ :thumb => "100x100>" },
10
+ :default_style => :medium,
11
+ :path => ":rails_root/public/images/:class/:id/:attachment/:style/:basename.:extension",
12
+ :url => "/images/:class/:id/:attachment/:style/:basename.:extension"
13
+
14
+ has_attached_file :icon,
15
+ :styles => { :small => "80x80" },
16
+ :default_style => :small,
17
+ :path => ":rails_root/public/images/:class/:id/:attachment/:style/:basename.:extension",
18
+ :url => "/images/:class/:id/:attachment/:style/:basename.:extension"
19
+ end
20
+
21
+ class TestPaperclipSource < Test::Unit::TestCase
22
+
23
+ def setup
24
+ @source = Avatar::Source::PaperclipSource.new
25
+ png = File.new(File.join(File.dirname(__FILE__), ['lib', 'user_suit.png']))
26
+ assert File.exists?(png.path)
27
+ @user_with_avatar = PaperclipUser.create!(:email => 'clarence@example.com', :avatar => png)
28
+ @user_with_icon = PaperclipUser.create!(:email => 'bobbi@example.com', :icon => png)
29
+ @user_without_avatar = PaperclipUser.create!(:email => 'brunhilde@example.com')
30
+ end
31
+
32
+ def test_avatar_url_is_nil_if_person_is_nil
33
+ assert_nil @source.avatar_url_for(nil)
34
+ end
35
+
36
+ def test_avatar_url_is_nil_if_person_has_no_avatar
37
+ assert_nil @source.avatar_url_for(@user_without_avatar)
38
+ end
39
+
40
+ def test_avatar_url_for_person_with_avatar
41
+ assert_equal "/images/paperclipusers/#{@user_with_avatar.id}/avatars/medium/user_suit.png", @source.avatar_url_for(@user_with_avatar)
42
+ end
43
+
44
+ def test_avatar_url_is_nil_for_invalid_size
45
+ assert_nil @source.avatar_url_for(@user_with_avatar, :paperclip_style => :not_a_valid_style)
46
+ end
47
+
48
+ def test_avatar_url_for_person_with_icon_and_custom_file_column_field
49
+ assert_equal "/images/paperclipusers/#{@user_with_icon.id}/icons/small/user_suit.png", @source.avatar_url_for(@user_with_icon, :paperclip_field => :icon)
50
+ end
51
+
52
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'avatar/source/wrapper/rails_asset_source_wrapper'
3
+ require 'avatar/source/static_url_source'
4
+ require 'action_controller/base'
5
+
6
+ class TestRailsAssetSourceWrapper < Test::Unit::TestCase
7
+
8
+ def setup
9
+ ActionController::Base.asset_host = 'http://test.com'
10
+ @source = Avatar::Source::StaticUrlSource.new('/')
11
+ @wrapper = Avatar::Source::Wrapper::RailsAssetSourceWrapper.new(@source)
12
+ end
13
+
14
+ def test_returns_nil_if_underlying_source_returns_nil
15
+ @source.instance_eval { def avatar_url_for(*args); nil; end }
16
+ assert_nil @wrapper.avatar_url_for(:a_person)
17
+ end
18
+
19
+ def test_does_not_change_fully_qualified_uri
20
+ @source.url = 'http://example.com/images/avatar.png'
21
+ assert_equal 'http://example.com/images/avatar.png', @wrapper.avatar_url_for(3)
22
+ end
23
+
24
+ def test_uses_asset_host
25
+ @source.url = '/images/avatar.png'
26
+ assert_equal 'http://test.com/images/avatar.png', @wrapper.avatar_url_for(4)
27
+ end
28
+
29
+ def test_error_if_cannot_generate_full_uri
30
+ ActionController::Base.asset_host = ''
31
+ @source.url = '/images/avatar.png'
32
+ assert_raise(RuntimeError) {
33
+ @wrapper.avatar_url_for(4)
34
+ }
35
+ end
36
+
37
+ end