public
Description: Kete was developed by Horowhenua Library Trust and Katipo Communications Ltd. to build a digital library of Horowhenua material.
Homepage: http://kete.net.nz/
Clone URL: git://github.com/kete/kete.git
refinement: Adding Gravatar image updater to the registration and account edit 
pages. When the email text field loses focus, it'll update the avatar.
Kieran Pilkington (author)
Sun Jan 11 15:46:42 -0800 2009
commit  03c60ab4bc0528949bdf4b8e363eb707e9dd9b9f
tree    07f4b0f9fdc4772b4bcdf17688e2ebed86bdabd6
parent  d86eb1c529ff5f671746bcf54927d3004eba4341
...
111
112
113
 
 
 
 
 
 
 
 
 
 
 
 
 
114
115
116
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
0
@@ -111,6 +111,19 @@ class AccountController < ApplicationController
0
     render :action => 'signup'
0
   end
0
 
0
+  def fetch_gravatar
0
+    respond_to do |format|
0
+      format.js do
0
+        render :update do |page|
0
+          page.replace_html params[:avatar_id],
0
+                            avatar_tag(User.new({ :email => params[:email] }),
0
+                                                { :size => 30, :rating => 'G', :gravatar_default_url => "#{SITE_URL}images/no-avatar.png" },
0
+                                                { :width => 30, :height => 30, :alt => 'Your Gravatar. ' })
0
+        end
0
+      end
0
+    end
0
+  end
0
+
0
   def simple_captcha_valid?
0
     if params[:user][:security_code] != ''
0
       return true
...
13
14
15
16
 
17
18
 
19
20
21
 
22
23
 
24
25
26
27
 
28
29
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
35
...
13
14
15
 
16
17
18
19
20
21
 
22
23
 
24
25
26
27
 
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
0
@@ -13,23 +13,41 @@ module ApplicationHelper
0
 
0
   # Controls needed for Gravatar support throughout the site
0
   include Avatar::View::ActionViewSupport
0
-  def avatar_for(user)
0
+  def avatar_for(user, options = {})
0
     image_dimension = IMAGE_SIZES[:small_sq].gsub(/(!|>|<)/, '').split('x').first.to_i
0
     default_options = { :width => image_dimension, :height => image_dimension, :alt => "#{user.user_name}'s Avatar. " }
0
+    options = default_options.merge(options)
0
 
0
     if ENABLE_USER_PORTRAITS && !user.portraits.empty? && !user.portraits.first.thumbnail_file.file_private
0
-      return image_tag(user.portraits.first.thumbnail_file.public_filename, default_options)
0
+      return image_tag(user.portraits.first.thumbnail_file.public_filename, options)
0
     elsif ENABLE_USER_PORTRAITS && !ENABLE_GRAVATAR_SUPPORT
0
-      return image_tag('no-avatar.png', default_options)
0
+      return image_tag('no-avatar.png', options)
0
     end
0
 
0
     if ENABLE_GRAVATAR_SUPPORT
0
-      return avatar_tag(user, { :size => 50, :rating => 'G', :gravatar_default_url => "#{SITE_URL}images/no-avatar.png" }, default_options)
0
+      return avatar_tag(user, { :size => 50, :rating => 'G', :gravatar_default_url => "#{SITE_URL}images/no-avatar.png" }, options)
0
     end
0
 
0
     return ''
0
   end
0
 
0
+  # Adds the necessary javascript to update a div with the id of user_avat
0
+  def avatar_updater_js(options = {})
0
+    options = options.merge({ :email_id => 'user_email', :avatar_id => 'user_avatar_img', :spinner_id => 'user_avatar_spinner' })
0
+    javascript_tag("
0
+      $('#{options[:email_id]}').observe('change', function(event){
0
+        // if we dont have something that looks like an email, don't make a request
0
+        if (!$('#{options[:email_id]}').value.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i)) { return true; }
0
+        new Ajax.Request('#{url_for(:controller => 'account', :action => 'fetch_gravatar')}', {
0
+          method: 'get',
0
+          parameters: { email: $('#{options[:email_id]}').value, avatar_id: '#{options[:avatar_id]}' },
0
+          onLoading: function(loading) { $('#{options[:spinner_id]}').show(); },
0
+          onComplete: function(complete) { $('#{options[:spinner_id]}').hide(); },
0
+        });
0
+      });
0
+    ")
0
+  end
0
+
0
   def page_keywords
0
     return DEFAULT_PAGE_KEYWORDS if current_item.nil? || current_item.tags.blank?
0
     current_item.tags.join(",").gsub(" ", "_").gsub("\"", "")
...
9
10
11
12
 
13
14
 
15
16
 
 
 
17
 
18
19
 
 
20
21
22
...
9
10
11
 
12
13
14
15
16
 
17
18
19
20
21
22
 
23
24
25
26
27
0
@@ -9,14 +9,19 @@
0
 <%= error_messages_for :user %>
0
 
0
 <div class="form-element">
0
-<label for="user_login">Login</label> <%= f.text_field :login, :tabindex => '1' %>
0
+<label for="user_login">Login:</label> <%= f.text_field :login, :tabindex => '1' %>
0
 <div class="form_example">Your username must be unique and contain no spaces. You will be asked for this and your password everytime you login. If your names is "John Smith" then you could use "jsmith" as your login.</div>
0
 </div>
0
+
0
 <div class="form-element">
0
-<label for="user_email">Email</label> <%= f.text_field :email, :tabindex => '1' %>
0
+  <label for="user_email">Email:</label>
0
+  <%= f.text_field :email, :tabindex => '1', :style => 'float:left;' %>
0
+  <%= render :partial => 'avatar_updater' -%>
0
 </div>
0
+
0
 <div class="form-element">
0
-<label for="allow_emails">Allow Emails from other users</label> <%= f.check_box :allow_emails %>
0
+  <label for="allow_emails">Allow Emails from other users:</label>
0
+  <%= f.check_box :allow_emails, :tabindex => '1' %>
0
 </div>
0
 
0
 <div class="form-element">
...
11
12
13
 
14
15
 
 
 
16
 
17
18
19
...
11
12
13
14
15
 
16
17
18
19
20
21
22
23
0
@@ -11,9 +11,13 @@
0
 <label for="user_login">Login:</label> <%= f.text_field :login, :tabindex => '1' %>
0
 <div class="form_example">Your login needs to be unique and contain no spaces. You will be asked for this and your password everytime you login. If your names is "John Smith" then you could use "jsmith" as your login.</div>
0
 </div>
0
+
0
 <div class="form-element">
0
-<label for="user_email">Email:</label> <%= f.text_field :email, :tabindex => '1' %>
0
+  <label for="user_email">Email:</label>
0
+  <%= f.text_field :email, :tabindex => '1', :style => 'float:left;' %>
0
+  <%= render :partial => 'avatar_updater' -%>
0
 </div>
0
+
0
 <div class="form-element">
0
 <label for="user_password">Password:</label> <%= f.password_field :password, :tabindex => '1' %>
0
 </div>

Comments