##// END OF EJS Templates
Upgraded the gravatar plugin from http://github.com/woods/gravatar-plugin...
Eric Davis -
r2727:560e915c50e8
parent child
Show More
@@ -0,0 +1,1
1 coverage
@@ -1,52 +1,55
1 == Gravatar Plugin
1 == Gravatar Plugin
2
2
3 This plugin provides a handful of view helpers for displaying gravatars
3 This plugin provides a handful of view helpers for displaying gravatars
4 (globally-recognized avatars).
4 (globally-recognized avatars).
5
5
6 Gravatars allow users to configure an avatar to go with their email address at
6 Gravatars allow users to configure an avatar to go with their email address at
7 a central location: http://gravatar.com. Gravatar-aware websites (such
7 a central location: http://gravatar.com. Gravatar-aware websites (such
8 as yours) can then look up and display each user's preferred avatar, without
8 as yours) can then look up and display each user's preferred avatar, without
9 having to handle avatar management. The user gets the benefit of not having to
9 having to handle avatar management. The user gets the benefit of not having to
10 set up an avatar for each site that they post on.
10 set up an avatar for each site that they post on.
11
11
12 == Installation
12 == Installation
13
13
14 cd ~/myapp
14 cd ~/myapp
15 ruby script/plugin install svn://rubyforge.org//var/svn/gravatarplugin/plugins/gravatar
15 ruby script/plugin install git://github.com/woods/gravatar-plugin.git
16
16
17 or, if you're using piston[http://piston.rubyforge.org] (worth it!):
17 or, if you're using piston[http://piston.rubyforge.org] (worth it!):
18
18
19 cd ~/myapp/vendor/plugins
19 cd ~/myapp/vendor/plugins
20 piston import svn://rubyforge.org//var/svn/gravatarplugin/plugins/gravatar
20 piston import git://github.com/woods/gravatar-plugin.git
21
21
22 == Example
22 == Example
23
23
24 If you represent your users with a model that has an +email+ method (typical
24 If you represent your users with a model that has an +email+ method (typical
25 for most rails authentication setups), then you can simply use this method
25 for most rails authentication setups), then you can simply use this method
26 in your views:
26 in your views:
27
27
28 <%= gravatar_for @user %>
28 <%= gravatar_for @user %>
29
29
30 This will be replaced with the full HTML +img+ tag necessary for displaying
30 This will be replaced with the full HTML +img+ tag necessary for displaying
31 that user's gravatar.
31 that user's gravatar.
32
32
33 Other helpers are documented under GravatarHelper::PublicMethods.
33 Other helpers are documented under GravatarHelper::PublicMethods.
34
34
35 == Acknowledgments
35 == Acknowledgments
36
36
37 Thanks to Magnus Bergmark (http://github.com/Mange), who contributed the SSL
38 support in this plugin, as well as a few minor fixes.
39
37 The following people have also written gravatar-related Ruby libraries:
40 The following people have also written gravatar-related Ruby libraries:
38 * Seth Rasmussen created the gravatar gem[http://gravatar.rubyforge.org]
41 * Seth Rasmussen created the gravatar gem[http://gravatar.rubyforge.org]
39 * Matt McCray has also created a gravatar
42 * Matt McCray has also created a gravatar
40 plugin[http://mattmccray.com/svn/rails/plugins/gravatar_helper]
43 plugin[http://mattmccray.com/svn/rails/plugins/gravatar_helper]
41
44
42 == Author
45 == Author
43
46
44 Scott A. Woods
47 Scott A. Woods
45 West Arete Computing, Inc.
48 West Arete Computing, Inc.
46 http://westarete.com
49 http://westarete.com
47 scott at westarete dot com
50 scott at westarete dot com
48
51
49 == TODO
52 == TODO
50
53
51 * Get full spec coverage
54 * Add specs for ssl support
52 * Finish rdoc documentation No newline at end of file
55 * Finish rdoc documentation
@@ -1,33 +1,32
1 require 'spec/rake/spectask'
1 require 'spec/rake/spectask'
2 require 'rake/rdoctask'
2 require 'rake/rdoctask'
3
3
4 desc 'Default: run all specs'
4 desc 'Default: run all specs'
5 task :default => :spec
5 task :default => :spec
6
6
7 desc 'Run all application-specific specs'
7 desc 'Run all application-specific specs'
8 Spec::Rake::SpecTask.new(:spec) do |t|
8 Spec::Rake::SpecTask.new(:spec) do |t|
9 t.warning = true
10 t.rcov = true
9 t.rcov = true
11 end
10 end
12
11
13 desc "Report code statistics (KLOCs, etc) from the application"
12 desc "Report code statistics (KLOCs, etc) from the application"
14 task :stats do
13 task :stats do
15 RAILS_ROOT = File.dirname(__FILE__)
14 RAILS_ROOT = File.dirname(__FILE__)
16 STATS_DIRECTORIES = [
15 STATS_DIRECTORIES = [
17 %w(Libraries lib/),
16 %w(Libraries lib/),
18 %w(Specs spec/),
17 %w(Specs spec/),
19 ].collect { |name, dir| [ name, "#{RAILS_ROOT}/#{dir}" ] }.select { |name, dir| File.directory?(dir) }
18 ].collect { |name, dir| [ name, "#{RAILS_ROOT}/#{dir}" ] }.select { |name, dir| File.directory?(dir) }
20 require 'code_statistics'
19 require 'code_statistics'
21 CodeStatistics.new(*STATS_DIRECTORIES).to_s
20 CodeStatistics.new(*STATS_DIRECTORIES).to_s
22 end
21 end
23
22
24 namespace :doc do
23 namespace :doc do
25 desc 'Generate documentation for the assert_request plugin.'
24 desc 'Generate documentation for the assert_request plugin.'
26 Rake::RDocTask.new(:plugin) do |rdoc|
25 Rake::RDocTask.new(:plugin) do |rdoc|
27 rdoc.rdoc_dir = 'rdoc'
26 rdoc.rdoc_dir = 'rdoc'
28 rdoc.title = 'Gravatar Rails Plugin'
27 rdoc.title = 'Gravatar Rails Plugin'
29 rdoc.options << '--line-numbers' << '--inline-source' << '--accessor' << 'cattr_accessor=rw'
28 rdoc.options << '--line-numbers' << '--inline-source' << '--accessor' << 'cattr_accessor=rw'
30 rdoc.rdoc_files.include('README')
29 rdoc.rdoc_files.include('README')
31 rdoc.rdoc_files.include('lib/**/*.rb')
30 rdoc.rdoc_files.include('lib/**/*.rb')
32 end
31 end
33 end
32 end
@@ -1,7 +1,7
1 author: Scott Woods, West Arete Computing
1 author: Scott Woods, West Arete Computing
2 summary: View helpers for displaying gravatars.
2 summary: View helpers for displaying gravatars.
3 homepage: http://gravatarplugin.rubyforge.org/
3 homepage: http://github.com/woods/gravatar-plugin/
4 plugin: svn://rubyforge.org//var/svn/gravatarplugin/plugins/gravatar
4 plugin: git://github.com/woods/gravatar-plugin.git
5 license: MIT
5 license: MIT
6 version: 0.1
6 version: 0.1
7 rails_version: 1.0+
7 rails_version: 1.0+
@@ -1,67 +1,85
1 require 'digest/md5'
1 require 'digest/md5'
2 require 'cgi'
2 require 'cgi'
3
3
4 module GravatarHelper
4 module GravatarHelper
5
5
6 # These are the options that control the default behavior of the public
6 # These are the options that control the default behavior of the public
7 # methods. They can be overridden during the actual call to the helper,
7 # methods. They can be overridden during the actual call to the helper,
8 # or you can set them in your environment.rb as such:
8 # or you can set them in your environment.rb as such:
9 #
9 #
10 # # Allow racier gravatars
10 # # Allow racier gravatars
11 # GravatarHelper::DEFAULT_OPTIONS[:rating] = 'R'
11 # GravatarHelper::DEFAULT_OPTIONS[:rating] = 'R'
12 #
12 #
13 DEFAULT_OPTIONS = {
13 DEFAULT_OPTIONS = {
14 # The URL of a default image to display if the given email address does
14 # The URL of a default image to display if the given email address does
15 # not have a gravatar.
15 # not have a gravatar.
16 :default => nil,
16 :default => nil,
17
17
18 # The default size in pixels for the gravatar image (they're square).
18 # The default size in pixels for the gravatar image (they're square).
19 :size => 50,
19 :size => 50,
20
20
21 # The maximum allowed MPAA rating for gravatars. This allows you to
21 # The maximum allowed MPAA rating for gravatars. This allows you to
22 # exclude gravatars that may be out of character for your site.
22 # exclude gravatars that may be out of character for your site.
23 :rating => 'PG',
23 :rating => 'PG',
24
24
25 # The alt text to use in the img tag for the gravatar.
25 # The alt text to use in the img tag for the gravatar. Since it's a
26 :alt => 'avatar',
26 # decorational picture, the alt text should be empty according to the
27 # XHTML specs.
28 :alt => '',
27
29
28 # The class to assign to the img tag for the gravatar.
30 # The class to assign to the img tag for the gravatar.
29 :class => 'gravatar',
31 :class => 'gravatar',
32
33 # Whether or not to display the gravatars using HTTPS instead of HTTP
34 :ssl => false,
30 }
35 }
31
36
32 # The methods that will be made available to your views.
37 # The methods that will be made available to your views.
33 module PublicMethods
38 module PublicMethods
34
39
35 # Return the HTML img tag for the given user's gravatar. Presumes that
40 # Return the HTML img tag for the given user's gravatar. Presumes that
36 # the given user object will respond_to "email", and return the user's
41 # the given user object will respond_to "email", and return the user's
37 # email address.
42 # email address.
38 def gravatar_for(user, options={})
43 def gravatar_for(user, options={})
39 gravatar(user.email, options)
44 gravatar(user.email, options)
40 end
45 end
41
46
42 # Return the HTML img tag for the given email address's gravatar.
47 # Return the HTML img tag for the given email address's gravatar.
43 def gravatar(email, options={})
48 def gravatar(email, options={})
44 src = h(gravatar_url(email, options))
49 src = h(gravatar_url(email, options))
45 options = DEFAULT_OPTIONS.merge(options)
50 options = DEFAULT_OPTIONS.merge(options)
46 [:class, :alt, :size].each { |opt| options[opt] = h(options[opt]) }
51 [:class, :alt, :size].each { |opt| options[opt] = h(options[opt]) }
47 "<img class=\"#{options[:class]}\" alt=\"#{options[:alt]}\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{src}\" />"
52 "<img class=\"#{options[:class]}\" alt=\"#{options[:alt]}\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{src}\" />"
48 end
53 end
49
54
55 # Returns the base Gravatar URL for the given email hash. If ssl evaluates to true,
56 # a secure URL will be used instead. This is required when the gravatar is to be
57 # displayed on a HTTPS site.
58 def gravatar_api_url(hash, ssl=false)
59 if ssl
60 "https://secure.gravatar.com/avatar/#{hash}"
61 else
62 "http://www.gravatar.com/avatar/#{hash}"
63 end
64 end
65
50 # Return the gravatar URL for the given email address.
66 # Return the gravatar URL for the given email address.
51 def gravatar_url(email, options={})
67 def gravatar_url(email, options={})
52 email_hash = Digest::MD5.hexdigest(email)
68 email_hash = Digest::MD5.hexdigest(email)
53 options = DEFAULT_OPTIONS.merge(options)
69 options = DEFAULT_OPTIONS.merge(options)
54 options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
70 options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
55 returning "http://www.gravatar.com/avatar.php?gravatar_id=#{email_hash}" do |url|
71 returning gravatar_api_url(email_hash, options.delete(:ssl)) do |url|
72 opts = []
56 [:rating, :size, :default].each do |opt|
73 [:rating, :size, :default].each do |opt|
57 unless options[opt].nil?
74 unless options[opt].nil?
58 value = h(options[opt])
75 value = h(options[opt])
59 url << "&#{opt}=#{value}"
76 opts << [opt, value].join('=')
60 end
77 end
61 end
78 end
79 url << "?#{opts.join('&')}" unless opts.empty?
62 end
80 end
63 end
81 end
64
82
65 end
83 end
66
84
67 end No newline at end of file
85 end
General Comments 0
You need to be logged in to leave comments. Login now