avatar 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/History.txt +14 -0
- data/License.txt +20 -0
- data/Manifest.txt +73 -0
- data/README.txt +65 -0
- data/Rakefile +10 -0
- data/config/hoe.rb +70 -0
- data/config/requirements.rb +17 -0
- data/lib/avatar.rb +44 -0
- data/lib/avatar/source.rb +4 -0
- data/lib/avatar/source/abstract_source.rb +15 -0
- data/lib/avatar/source/file_column_source.rb +35 -0
- data/lib/avatar/source/gravatar_source.rb +78 -0
- data/lib/avatar/source/nil_source.rb +17 -0
- data/lib/avatar/source/source_chain.rb +51 -0
- data/lib/avatar/source/static_url_source.rb +25 -0
- data/lib/avatar/source/string_substitution_source.rb +48 -0
- data/lib/avatar/version.rb +9 -0
- data/lib/avatar/view.rb +4 -0
- data/lib/avatar/view/abstract_view_support.rb +15 -0
- data/lib/avatar/view/action_view_support.rb +23 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +74 -0
- data/setup.rb +1585 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/testing.rake +37 -0
- data/tasks/website.rake +17 -0
- data/test/.gitignore +3 -0
- data/test/lib/database.rb +2 -0
- data/test/lib/database.yml +10 -0
- data/test/lib/file_column/CHANGELOG +69 -0
- data/test/lib/file_column/README +54 -0
- data/test/lib/file_column/Rakefile +36 -0
- data/test/lib/file_column/TODO +6 -0
- data/test/lib/file_column/init.rb +13 -0
- data/test/lib/file_column/lib/file_column.rb +720 -0
- data/test/lib/file_column/lib/file_column_helper.rb +150 -0
- data/test/lib/file_column/lib/file_compat.rb +28 -0
- data/test/lib/file_column/lib/magick_file_column.rb +260 -0
- data/test/lib/file_column/lib/rails_file_column.rb +19 -0
- data/test/lib/file_column/lib/test_case.rb +124 -0
- data/test/lib/file_column/lib/validations.rb +112 -0
- data/test/lib/file_column/test/abstract_unit.rb +63 -0
- data/test/lib/file_column/test/connection.rb +17 -0
- data/test/lib/file_column/test/file_column_helper_test.rb +97 -0
- data/test/lib/file_column/test/file_column_test.rb +650 -0
- data/test/lib/file_column/test/fixtures/entry.rb +32 -0
- data/test/lib/file_column/test/fixtures/invalid-image.jpg +1 -0
- data/test/lib/file_column/test/fixtures/kerb.jpg +0 -0
- data/test/lib/file_column/test/fixtures/mysql.sql +25 -0
- data/test/lib/file_column/test/fixtures/schema.rb +10 -0
- data/test/lib/file_column/test/fixtures/skanthak.png +0 -0
- data/test/lib/file_column/test/magick_test.rb +380 -0
- data/test/lib/file_column/test/magick_view_only_test.rb +21 -0
- data/test/lib/schema.rb +7 -0
- data/test/lib/user_suit.png +0 -0
- data/test/test_abstract_view_support.rb +22 -0
- data/test/test_action_view_support.rb +30 -0
- data/test/test_avatar.rb +12 -0
- data/test/test_file_column_source.rb +30 -0
- data/test/test_gravatar_source.rb +58 -0
- data/test/test_helper.rb +22 -0
- data/test/test_nil_source.rb +18 -0
- data/test/test_source_chain.rb +44 -0
- data/test/test_static_url_source.rb +18 -0
- data/test/test_string_substitution_source.rb +22 -0
- data/website/index.html +101 -0
- data/website/index.txt +44 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +141 -0
data/.gitignore
ADDED
data/History.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
== 0.0.1 2008-03-24
|
2
|
+
|
3
|
+
* 1 major enhancement:
|
4
|
+
* Initial release
|
5
|
+
* AbstractSource
|
6
|
+
* FileColumnSource
|
7
|
+
* GravatarSource
|
8
|
+
* NilSource
|
9
|
+
* SourceChain
|
10
|
+
* StaticUrlSource
|
11
|
+
* StringSubstitutionSource
|
12
|
+
* AbstractViewSupport
|
13
|
+
* ActionViewSupport
|
14
|
+
* tests
|
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Universal Presence, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
.gitignore
|
2
|
+
History.txt
|
3
|
+
License.txt
|
4
|
+
Manifest.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
config/hoe.rb
|
8
|
+
config/requirements.rb
|
9
|
+
lib/avatar.rb
|
10
|
+
lib/avatar/source.rb
|
11
|
+
lib/avatar/source/abstract_source.rb
|
12
|
+
lib/avatar/source/file_column_source.rb
|
13
|
+
lib/avatar/source/gravatar_source.rb
|
14
|
+
lib/avatar/source/nil_source.rb
|
15
|
+
lib/avatar/source/source_chain.rb
|
16
|
+
lib/avatar/source/static_url_source.rb
|
17
|
+
lib/avatar/source/string_substitution_source.rb
|
18
|
+
lib/avatar/version.rb
|
19
|
+
lib/avatar/view.rb
|
20
|
+
lib/avatar/view/abstract_view_support.rb
|
21
|
+
lib/avatar/view/action_view_support.rb
|
22
|
+
script/destroy
|
23
|
+
script/generate
|
24
|
+
script/txt2html
|
25
|
+
setup.rb
|
26
|
+
tasks/deployment.rake
|
27
|
+
tasks/environment.rake
|
28
|
+
tasks/testing.rake
|
29
|
+
tasks/website.rake
|
30
|
+
test/.gitignore
|
31
|
+
test/lib/database.rb
|
32
|
+
test/lib/database.yml
|
33
|
+
test/lib/file_column/CHANGELOG
|
34
|
+
test/lib/file_column/README
|
35
|
+
test/lib/file_column/Rakefile
|
36
|
+
test/lib/file_column/TODO
|
37
|
+
test/lib/file_column/init.rb
|
38
|
+
test/lib/file_column/lib/file_column.rb
|
39
|
+
test/lib/file_column/lib/file_column_helper.rb
|
40
|
+
test/lib/file_column/lib/file_compat.rb
|
41
|
+
test/lib/file_column/lib/magick_file_column.rb
|
42
|
+
test/lib/file_column/lib/rails_file_column.rb
|
43
|
+
test/lib/file_column/lib/test_case.rb
|
44
|
+
test/lib/file_column/lib/validations.rb
|
45
|
+
test/lib/file_column/test/abstract_unit.rb
|
46
|
+
test/lib/file_column/test/connection.rb
|
47
|
+
test/lib/file_column/test/file_column_helper_test.rb
|
48
|
+
test/lib/file_column/test/file_column_test.rb
|
49
|
+
test/lib/file_column/test/fixtures/entry.rb
|
50
|
+
test/lib/file_column/test/fixtures/invalid-image.jpg
|
51
|
+
test/lib/file_column/test/fixtures/kerb.jpg
|
52
|
+
test/lib/file_column/test/fixtures/mysql.sql
|
53
|
+
test/lib/file_column/test/fixtures/schema.rb
|
54
|
+
test/lib/file_column/test/fixtures/skanthak.png
|
55
|
+
test/lib/file_column/test/magick_test.rb
|
56
|
+
test/lib/file_column/test/magick_view_only_test.rb
|
57
|
+
test/lib/schema.rb
|
58
|
+
test/lib/user_suit.png
|
59
|
+
test/test_abstract_view_support.rb
|
60
|
+
test/test_action_view_support.rb
|
61
|
+
test/test_avatar.rb
|
62
|
+
test/test_file_column_source.rb
|
63
|
+
test/test_gravatar_source.rb
|
64
|
+
test/test_helper.rb
|
65
|
+
test/test_nil_source.rb
|
66
|
+
test/test_source_chain.rb
|
67
|
+
test/test_static_url_source.rb
|
68
|
+
test/test_string_substitution_source.rb
|
69
|
+
website/index.html
|
70
|
+
website/index.txt
|
71
|
+
website/javascripts/rounded_corners_lite.inc.js
|
72
|
+
website/stylesheets/screen.css
|
73
|
+
website/template.rhtml
|
data/README.txt
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= avatar
|
2
|
+
|
3
|
+
* http://avatar.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
Avatar is a collection of Ruby utilities for displaying avatars.
|
7
|
+
|
8
|
+
== FEATURES/PROBLEMS:
|
9
|
+
|
10
|
+
Avatar currently supports the following sources:
|
11
|
+
* Gravatar (see http://www.gravatar.com)
|
12
|
+
* a constant URL (e.g. http://mysite.com/images/default_icon.png)
|
13
|
+
* parameterized URLs (e.g. http://mysite.com/images/famfamfam/user_#{color}.png)
|
14
|
+
* file_column (see http://www.kanthak.net/opensource/file_column/)
|
15
|
+
* chains of sources (e.g. file_column if exists; otherwise default constant URL)
|
16
|
+
|
17
|
+
Avatar currently supports the following views:
|
18
|
+
* ActionView (Rails), through avatar_tag
|
19
|
+
* AbstractView (any framework), through avatar_url_for
|
20
|
+
|
21
|
+
== SYNOPSIS:
|
22
|
+
|
23
|
+
in RAILS_ROOT/app/helpers/people_helper.rb
|
24
|
+
require 'avatar/view/action_view_support'
|
25
|
+
class PeopleHelper
|
26
|
+
include Avatar::View::ActionViewSupport
|
27
|
+
end
|
28
|
+
|
29
|
+
in RAILS_ROOT/app/views/people/show.html.erb:
|
30
|
+
<%= avatar_tag(@current_user, :size => 40) %>
|
31
|
+
|
32
|
+
== REQUIREMENTS:
|
33
|
+
|
34
|
+
* none for basic functionality
|
35
|
+
* will integrate with ActionView
|
36
|
+
* will integrate with the file_column Rails plugin
|
37
|
+
|
38
|
+
== INSTALL:
|
39
|
+
|
40
|
+
* sudo gem install avatar
|
41
|
+
|
42
|
+
== LICENSE:
|
43
|
+
|
44
|
+
(The MIT License)
|
45
|
+
|
46
|
+
Copyright (c) 2008 Universal Presenece, Inc.
|
47
|
+
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
49
|
+
a copy of this software and associated documentation files (the
|
50
|
+
'Software'), to deal in the Software without restriction, including
|
51
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
52
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
53
|
+
permit persons to whom the Software is furnished to do so, subject to
|
54
|
+
the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be
|
57
|
+
included in all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
60
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
61
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
62
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
63
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
64
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
65
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'avatar/version'
|
2
|
+
|
3
|
+
AUTHOR = 'James Rosen' # can also be an array of Authors
|
4
|
+
EMAIL = "james@u-presence.com"
|
5
|
+
DESCRIPTION = "helpers for displaying avatars"
|
6
|
+
GEM_NAME = 'avatar' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'avatar' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
|
11
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
12
|
+
@config = nil
|
13
|
+
RUBYFORGE_USERNAME = "unknown"
|
14
|
+
def rubyforge_username
|
15
|
+
unless @config
|
16
|
+
begin
|
17
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
18
|
+
rescue
|
19
|
+
puts <<-EOS
|
20
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
21
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
22
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
23
|
+
EOS
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
end
|
27
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
REV = nil
|
32
|
+
# UNCOMMENT IF REQUIRED:
|
33
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
34
|
+
VERS = Avatar::VERSION::STRING + (REV ? ".#{REV}" : "")
|
35
|
+
RDOC_OPTS = ['--quiet', '--title', 'avatar documentation',
|
36
|
+
"--opname", "index.html",
|
37
|
+
"--line-numbers",
|
38
|
+
"--main", "README",
|
39
|
+
"--inline-source"]
|
40
|
+
|
41
|
+
class Hoe
|
42
|
+
def extra_deps
|
43
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
44
|
+
@extra_deps
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generate all the Rake tasks
|
49
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
50
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
51
|
+
p.developer(AUTHOR, EMAIL)
|
52
|
+
p.description = DESCRIPTION
|
53
|
+
p.summary = DESCRIPTION
|
54
|
+
p.url = HOMEPATH
|
55
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
56
|
+
p.test_globs = ["test/**/test_*.rb"]
|
57
|
+
p.clean_globs |= ['coverage', 'test/public', '**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
58
|
+
|
59
|
+
# == Optional
|
60
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
61
|
+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
62
|
+
|
63
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
68
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
69
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
70
|
+
hoe.rsync_args = '-av --delete --ignore-errors'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
16
|
+
|
17
|
+
require 'avatar'
|
data/lib/avatar.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'avatar/source/abstract_source'
|
3
|
+
require 'avatar/source/gravatar_source'
|
4
|
+
require 'avatar/source/static_url_source'
|
5
|
+
require 'avatar/source/nil_source'
|
6
|
+
|
7
|
+
# Helpers for displaying avatars.
|
8
|
+
# Usage in Rails:
|
9
|
+
# # in app/helpers/ProfileHelper.rb:
|
10
|
+
# include Avatar::ActionView::Support
|
11
|
+
#
|
12
|
+
# # in app/views/profiles/show.html.erb:
|
13
|
+
# <%= avatar_for @person =>
|
14
|
+
#
|
15
|
+
# By default, Avatar::source is a GravatarSource with
|
16
|
+
# StaticUrlSource as its default.
|
17
|
+
# The latter has the url '/images/default_avatar.png'.
|
18
|
+
module Avatar
|
19
|
+
|
20
|
+
@@source = Avatar::Source::NilSource.new
|
21
|
+
@@default_avatar_options = {}
|
22
|
+
|
23
|
+
def self.source
|
24
|
+
@@source.dup
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.source=(source)
|
28
|
+
raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
|
29
|
+
@@source = source
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.default_avatar_options
|
33
|
+
@@default_avatar_options.dup
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.default_avatar_options=(options)
|
37
|
+
raise ArgumentError.new("#{options} is not a Hash") unless options.kind_of?(Hash)
|
38
|
+
@@options = options
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
url_source = Avatar::Source::StaticUrlSource.new('/images/default_avatar.png')
|
44
|
+
Avatar.source = Avatar::Source::GravatarSource.new(url_source)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Avatar # :nodoc:
|
2
|
+
module Source # :nodoc:
|
3
|
+
# To be included by classes that generate avatar URLs from profiles.
|
4
|
+
module AbstractSource
|
5
|
+
|
6
|
+
# Return an avatar URL for the person, or nil if this source cannot generate one.
|
7
|
+
# Including classes <em>must</em> override this method. In general, implementations
|
8
|
+
# should return nil if +person+ is nil.
|
9
|
+
def avatar_url_for(person, options = {})
|
10
|
+
raise NotImplementedError.new('including class must define avatar_url_for(person, options = {})')
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
require 'file_column_helper'
|
3
|
+
|
4
|
+
module Avatar # :nodoc:
|
5
|
+
module Source # :nodoc:
|
6
|
+
# For use with the FileColumn Rails plugin.
|
7
|
+
class FileColumnSource
|
8
|
+
include AbstractSource
|
9
|
+
include FileColumnHelper
|
10
|
+
|
11
|
+
attr_accessor :default_field
|
12
|
+
|
13
|
+
def initialize(default_field = :avatar)
|
14
|
+
raise ArgumentError.new('default_field cannot be nil') if default_field.nil?
|
15
|
+
@default_field = default_field
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns nil if person is nil; otherwise, returns the (possibly nil) result of
|
19
|
+
# <code>url_for_image_column</code>, passing in all of +options+ except :field.
|
20
|
+
# Options:
|
21
|
+
# * <code>:field</code> - the image file column within +person+; by default, :avatar
|
22
|
+
# * <code>:file_column_version</code> - one of the versions of the file_column; no default
|
23
|
+
# If :file_column_version is not specified, all other options are passed to
|
24
|
+
# <code>url_for_image_column</code> as +options+ (see FileColumnHelper)
|
25
|
+
def avatar_url_for(person, options = {})
|
26
|
+
return nil if person.nil?
|
27
|
+
field = options.delete(:field) || default_field
|
28
|
+
return nil if field.nil? || person.send(field).nil?
|
29
|
+
options = options[:file_column_version] || options
|
30
|
+
url_for_image_column(person, field, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'avatar/source/abstract_source'
|
2
|
+
require 'avatar/source/static_url_source'
|
3
|
+
require 'avatar/source/nil_source'
|
4
|
+
require 'digest/md5'
|
5
|
+
|
6
|
+
module Avatar # :nodoc:
|
7
|
+
module Source # :nodoc:
|
8
|
+
# NOTE: since Gravatar always returns a URL (never a 404), instances of this
|
9
|
+
# class should only be placed at the end of a SourceChain.
|
10
|
+
# (see link:classes/Avatar/Source/SourceChain.html)
|
11
|
+
# Alternatively, use <code>default_source = ...</code> to generate a site-wide
|
12
|
+
# default to be passed to Gravatar. (In fact, since <code>default_source</code>
|
13
|
+
# is an instance of Avatar::Source::AbstractSource, it can generate a different
|
14
|
+
# default for each person.)
|
15
|
+
class GravatarSource
|
16
|
+
include AbstractSource
|
17
|
+
|
18
|
+
attr_accessor :default_field
|
19
|
+
attr_reader :default_source
|
20
|
+
|
21
|
+
# 'http://www.gravatar.com/avatar/'
|
22
|
+
def self.base_url
|
23
|
+
'http://www.gravatar.com/avatar/'
|
24
|
+
end
|
25
|
+
|
26
|
+
# :nodoc:
|
27
|
+
def initialize(default_source = nil, default_field = :email)
|
28
|
+
self.default_source = default_source #not @default_source = ... b/c want to use the setter function below
|
29
|
+
@default_field = default_field
|
30
|
+
raise "There's a bug in the code" if @default_source.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
# Generates a Gravatar URL. Returns nil if person is nil.
|
34
|
+
# Options:
|
35
|
+
# * <code>:field (Symbol)</code> - the field to call from person. By default, <code>:email</code>.
|
36
|
+
# * <code>:default (String)</code> - override the default generated by <code>default_source</code>.
|
37
|
+
# * <code>:size or :s</code> - the size in pixels of the avatar to render.
|
38
|
+
# * <code>:rating or :r</code> - the maximum rating; one of ['G', 'PG', 'R', 'X']
|
39
|
+
def avatar_url_for(person, options = {})
|
40
|
+
return nil if person.nil?
|
41
|
+
options[:default] ||= default_avatar_url_for(person, options)
|
42
|
+
field = options[:field] || default_field
|
43
|
+
raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :field (probably :email)') unless field
|
44
|
+
url = self.class.base_url
|
45
|
+
url << Digest::MD5::hexdigest(person.send(field)).strip
|
46
|
+
[:size, :s, :rating, :r, :default].each do |x|
|
47
|
+
next unless options[x]
|
48
|
+
url << (url.include?('?') ? '&' : '?')
|
49
|
+
url << "#{x}=#{options[x]}"
|
50
|
+
end
|
51
|
+
url
|
52
|
+
end
|
53
|
+
|
54
|
+
# Set the default source for all people.
|
55
|
+
# If +default+ is a String, it will be converted to an instance of Avatar::Source::StaticUrlSource.
|
56
|
+
# If +default+ is nil, sets the default to a NilSource.
|
57
|
+
def default_source=(default)
|
58
|
+
case default
|
59
|
+
when String
|
60
|
+
@default_source = StaticUrlSource.new(default)
|
61
|
+
when AbstractSource
|
62
|
+
@default_source = default
|
63
|
+
when NilClass
|
64
|
+
@default_source = NilSource.new
|
65
|
+
else
|
66
|
+
raise ArgumentError.new("#{default} must be either a String or an instance of #{AbstractSource}")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def default_avatar_url_for(person, options)
|
73
|
+
@default_source.avatar_url_for(person, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|