From 353cdbb926e14b76e8ede6ee38e9e3517c3e3c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20de=20Villamil?= Date: Mon, 9 Sep 2013 20:20:51 +0200 Subject: [PATCH] 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. --- app/controllers/admin/profiles_controller.rb | 31 ++++++++++++++++++-- app/helpers/application_helper.rb | 13 ++++++++ app/models/blog.rb | 1 + app/models/user.rb | 6 ++++ app/uploaders/resource_uploader.rb | 4 +++ app/views/admin/profiles/index.html.erb | 3 +- app/views/admin/settings/write.html.erb | 8 +++++ app/views/admin/users/_form.html.erb | 14 +++++++++ spec/models/configuration_spec.rb | 9 +++++- 9 files changed, 83 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/profiles_controller.rb b/app/controllers/admin/profiles_controller.rb index d78004fe0b..206f4a522b 100644 --- a/app/controllers/admin/profiles_controller.rb +++ b/app/controllers/admin/profiles_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index da39221308..4f11c1c10b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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? diff --git a/app/models/blog.rb b/app/models/blog.rb index f521ed2178..8bf669a882 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 9f2be5f7ca..bbf7195251 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/uploaders/resource_uploader.rb b/app/uploaders/resource_uploader.rb index 3f175582e8..7e7d90eeb5 100644 --- a/app/uploaders/resource_uploader.rb +++ b/app/uploaders/resource_uploader.rb @@ -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 diff --git a/app/views/admin/profiles/index.html.erb b/app/views/admin/profiles/index.html.erb index 2749d41e2c..72cba07c5c 100644 --- a/app/views/admin/profiles/index.html.erb +++ b/app/views/admin/profiles/index.html.erb @@ -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 %> diff --git a/app/views/admin/settings/write.html.erb b/app/views/admin/settings/write.html.erb index 24346f582d..9ef5f99f8d 100644 --- a/app/views/admin/settings/write.html.erb +++ b/app/views/admin/settings/write.html.erb @@ -45,6 +45,14 @@ +
+ +
+
+ <%= text_field(:setting, :image_avatar_size, { :class => 'span3'})%>px +
+
+
diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb index 6098f855b7..496b0f684e 100644 --- a/app/views/admin/users/_form.html.erb +++ b/app/views/admin/users/_form.html.erb @@ -85,6 +85,20 @@
+<% unless controller.controller_name == 'users'%> +
+ <%= _("Avatar") %> +

<%= display_user_avatar(current_user.id) %> <%= _("Your current avatar") %>

+
+ +
+ <%= file_field('user', 'filename', {:class => 'input-file'}) -%> +

<%= _("Upload a .jpg, .png or .gif file") %>

+
+
+
+<% end %> +
<%= _("Notifications")%>
diff --git a/spec/models/configuration_spec.rb b/spec/models/configuration_spec.rb index 57d2c007fb..a25b5fa2a0 100644 --- a/spec/models/configuration_spec.rb +++ b/spec/models/configuration_spec.rb @@ -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