Permalink
Browse files

Enhance user profile: allows users to upload an avatar (issue #230).

The display_user_avatar() helper displays in that order:

* The user's uploaded avatar.
* The user's Twitter avatar if the previous one does not exist
* Nothing if no avatar is available (maybe a placeholder should be interesting, I'll add an issue for this when I have some network connection).

TODO: we need to find out how to test file uploads for both the profile and resources controllers.
  • Loading branch information...
1 parent e1ccf8e commit 353cdbb926e14b76e8ede6ee38e9e3517c3e3c13 @fdv fdv committed Sep 9, 2013
@@ -1,12 +1,37 @@
+require 'fog'
+
class Admin::ProfilesController < Admin::BaseController
def index
@user = current_user
@profiles = Profile.find(:all, :order => 'id')
@user.attributes = params[:user]
- if request.post? and @user.save
- current_user = @user
- flash[:notice] = _('User was successfully updated.')
+ if request.post?
+ avatar = upload_avatar if params[:user][:filename]
+ @user.avatar = avatar.upload.avatar.url
+ @user.thumb_avatar = avatar.upload.thumb.url
+ @user.medium_avatar = avatar.upload.medium.url
+ @user.large_avatar = avatar.upload.url
+
+ if @user.save
+ current_user = @user
+ flash[:notice] = _('User was successfully updated.')
+ end
+ end
+ end
+
+ private
+
+ def upload_avatar
+ file = params[:user][:filename]
+
+ unless file.content_type
+ mime = 'text/plain'
+ else
+ mime = file.content_type.chomp
end
+
+ Resource.create(:upload => file, :mime => mime, :created_at => Time.now)
end
+
end
@@ -90,6 +90,19 @@ def author_link(article)
end
end
+ def display_user_avatar(user_id)
+ user = User.find(user_id)
+
+ if user.avatar.present?
+ avatar = user.avatar
+ elsif user.twitter_profile_image.present?
+ avatar = user.twitter_profile_image.present?
+ end
+
+ return unless avatar
+ image_tag(File.join(this_blog.base_url, avatar))
+ end
+
def author_picture(status)
return if status.user.twitter_profile_image.nil? or status.user.twitter_profile_image.empty?
return if status.twitter_id.nil? or status.twitter_id.empty?
View
@@ -56,6 +56,7 @@ class Blog < ActiveRecord::Base
setting :allow_signup, :integer, 0
setting :date_format, :string, '%d/%m/%Y'
setting :time_format, :string, '%Hh%M'
+ setting :image_avatar_size, :integer, 48
setting :image_thumb_size, :integer, 125
setting :image_medium_size, :integer, 600
View
@@ -19,6 +19,8 @@ class User < ActiveRecord::Base
serialize :settings, Hash
+ attr_accessor :filename
+
# Settings
setting :notify_watch_my_articles, :boolean, true
setting :firstname, :string, ''
@@ -42,6 +44,10 @@ class User < ActiveRecord::Base
setting :twitter_oauth_token, :string, ''
setting :twitter_oauth_token_secret, :string, ''
setting :twitter_profile_image, :string, ''
+ setting :avatar, :string, ''
+ setting :thumb_avatar, :string, ''
+ setting :medium_avatar, :string, ''
+ setting :large_avatar, :string, ''
# echo "publify" | sha1sum -
class_attribute :salt
@@ -27,6 +27,10 @@ def store_dir
process :dynamic_resize_to_fit => :medium
end
+ version :avatar, :if => :image? do
+ process :dynamic_resize_to_fit => :avatar
+ end
+
def dynamic_resize_to_fit(size)
blog = Blog.default
resize_setting = blog.send("image_#{size}_size").to_i
@@ -1,6 +1,5 @@
<% @page_heading = _('Your profile') %>
<% @page_heading_class = 'icon-profile' %>
-<%= form_tag :action=>"index", :id => @user.id do %>
+<%= form_tag({:action => 'index', :id => @user.id}, {:enctype => "multipart/form-data"}) %>
<%= render :partial => "admin/users/form" %>
-<% end %>
@@ -45,6 +45,14 @@
</div>
</div>
</div>
+ <div class='control-group'>
+ <label class='control-label' for="image_avatar_size"><%= _("Avatar size")%></label>
+ <div class='controls'>
+ <div class='input-append'>
+ <%= text_field(:setting, :image_avatar_size, { :class => 'span3'})%><span class='add-on'>px</span>
+ </div>
+ </div>
+ </div>
</fieldset>
<fieldset class='form-horizontal'>
@@ -85,6 +85,20 @@
</div>
</fieldset>
+<% unless controller.controller_name == 'users'%>
+<fieldset class='form-horizontal'>
+ <legend><%= _("Avatar") %></legend>
+ <p><%= display_user_avatar(current_user.id) %> <%= _("Your current avatar") %></p>
+ <div class='control-group'>
+ <label class='control-label' for="avatar"><%= _("Upload your image")%></label>
+ <div class='controls'>
+ <%= file_field('user', 'filename', {:class => 'input-file'}) -%>
+ <p class='help-block'><%= _("Upload a .jpg, .png or .gif file") %></p>
+ </div>
+ </div>
+</fieldset>
+<% end %>
+
<fieldset class='form-horizontal'>
<legend><%= _("Notifications")%></legend>
<div class='control-group'>
@@ -104,7 +104,8 @@
@blog.time_format.should == '%Hh%M'
end
- it 'Thumb and medium image size' do
+ it 'Thumb, medium and avatar image size' do
+ @blog.image_avatar_size.should == 48
@blog.image_thumb_size.should == 125
@blog.image_medium_size.should == 600
end
@@ -357,6 +358,12 @@
@user.twitter_profile_image.should == ''
end
+ it 'avatar is empty' do
+ @user.avatar.should == ''
+ @user.thumb_avatar.should == ''
+ @user.medium_avatar.should == ''
+ @user.large_avatar.should == ''
+ end
end
describe 'Given a new article' do

0 comments on commit 353cdbb

Please sign in to comment.