#668: added uploading avatars for groups

This commit is contained in:
Vokhmin Alexey V 2012-10-03 20:38:42 +04:00
parent fb80764a74
commit 04b2647ef0
20 changed files with 141 additions and 102 deletions

View File

@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*-
class Admin::UsersController < Admin::BaseController
include AvatarHelper
prepend_before_filter :find_user
def index
@ -28,10 +29,7 @@ class Admin::UsersController < Admin::BaseController
def update
@user.role = params[:role]
if @user.update_without_password(params[:user])
if @user.avatar && params[:delete_avatar] == '1'
@user.avatar = nil
@user.save
end
update_avatar(@user, params)
flash[:notice] = t('flash.user.saved')
redirect_to admin_users_path
else

View File

@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*-
class Groups::ProfileController < Groups::BaseController
include AvatarHelper
load_and_authorize_resource :class => Group, :instance_name => 'group'
skip_before_filter :authenticate_user!, :only => :show if APP_CONFIG['anonymous_access']
@ -35,6 +36,7 @@ class Groups::ProfileController < Groups::BaseController
def update
if @group.update_attributes(params[:group])
update_avatar(@group, params)
flash[:notice] = t('flash.group.saved')
redirect_to group_path(@group)
else

View File

@ -1,15 +1,13 @@
# -*- encoding : utf-8 -*-
class Users::SettingsController < Users::BaseController
include AvatarHelper
before_filter :set_current_user
def profile
if request.put?
send_confirmation = params[:user][:email] != @user.email
if @user.update_without_password(params[:user])
if @user.avatar && params[:delete_avatar] == '1'
@user.avatar = nil
@user.save
end
update_avatar(@user, params)
if send_confirmation
@user.confirmed_at, @user.confirmation_sent_at = nil
@user.send_confirmation_instructions

View File

@ -0,0 +1,8 @@
module AvatarHelper
def update_avatar(subject, params)
if subject.avatar && params[:delete_avatar] == '1'
subject.avatar = nil
subject.save
end
end
end

View File

@ -5,12 +5,13 @@ module UsersHelper
avatar_url(User.where(:email => email).first || User.new(:email => email), size)
end
def avatar_url(user, size = :small)
return image_path('group32.png') if user.kind_of? Group
if user.try('avatar?')
user.avatar.url(size)
def avatar_url(subject, size = :small)
if subject.try('avatar?')
subject.avatar.url(size)
elsif subject.kind_of? Group
image_path('ava-big.png')
else
gravatar_url(user.email, user.avatar.styles[size].geometry.split('x').first)
gravatar_url(subject.email, subject.avatar.styles[size].geometry.split('x').first)
end
end

17
app/models/avatar.rb Normal file
View File

@ -0,0 +1,17 @@
# -*- encoding : utf-8 -*-
class Avatar < ActiveRecord::Base
self.abstract_class = true
MAX_AVATAR_SIZE = 5.megabyte
has_attached_file :avatar, :styles =>
{ :micro => { :geometry => "16x16#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:small => { :geometry => "30x30#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:medium => { :geometry => "40x40#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:big => { :geometry => "81x81#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'}
}
validates_inclusion_of :avatar_file_size, :in => (0..MAX_AVATAR_SIZE), :allow_nil => true
attr_accessible :avatar
end

View File

@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class Group < ActiveRecord::Base
class Group < Avatar
belongs_to :owner, :class_name => 'User'
has_many :relations, :as => :actor, :dependent => :destroy, :dependent => :destroy

View File

@ -1,19 +1,11 @@
# -*- encoding : utf-8 -*-
class User < ActiveRecord::Base
class User < Avatar
ROLES = ['', 'admin', 'banned']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
MAX_AVATAR_SIZE = 5.megabyte
devise :database_authenticatable, :registerable, :omniauthable, :token_authenticatable,# :encryptable, :timeoutable
:recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable
has_attached_file :avatar, :styles =>
{ :micro => { :geometry => "16x16#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:small => { :geometry => "30x30#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:medium => { :geometry => "40x40#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:big => { :geometry => "81x81#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'}
}
validates_inclusion_of :avatar_file_size, :in => (0..MAX_AVATAR_SIZE), :allow_nil => true
has_one :notifier, :class_name => 'SettingsNotifier', :dependent => :destroy #:notifier
@ -43,7 +35,7 @@ class User < ActiveRecord::Base
validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :login, :name, :uname, :language,
:site, :company, :professional_experience, :location, :avatar
:site, :company, :professional_experience, :location
attr_readonly :uname
attr_accessor :login

View File

@ -8,6 +8,8 @@
.rightlist.nomargin= f.text_area :description
.both
%br
= render 'shared/avatar_form', :subject => @group, :f => f
%br
.leftlist
\ 
.rightlist= submit_tag t("layout.save")

View File

@ -0,0 +1,10 @@
.leftlist= f.label :avatar, t("layout.avatars.avatar_with_size", :max => number_to_human_size(Avatar::MAX_AVATAR_SIZE))
.rightlist= image_tag(avatar_url(subject, :medium))
.leftlist
.rightlist
.check
%span#niceCheckbox1.niceCheck-main= check_box_tag "delete_avatar", 1, false, :class => 'niceCheckbox1'
.forcheck= t('layout.avatars.delete_avatar')
.both
= f.file_field :avatar
.both

View File

@ -1,10 +1,9 @@
- avatar_url ||= 'ava-big.png'
- edit_link ||= nil
- user ||= nil
- group ||= nil
- name ||= uname
.avatar
= image_tag avatar_url
= image_tag avatar_url(user || group, :big)
- if edit_link
%br
= edit_link

View File

@ -28,16 +28,7 @@
.leftlist= f.label :location, t("activerecord.attributes.user.location")
.rightlist= f.text_field :location
.both
.leftlist= f.label :avatar, t("layout.users.avatar_with_size", :max => number_to_human_size(User::MAX_AVATAR_SIZE))
.rightlist= image_tag(avatar_url(@user, :medium))
.leftlist
.rightlist
.check
%span#niceCheckbox1.niceCheck-main= check_box_tag "delete_avatar", 1, false, :class => 'niceCheckbox1'
.forcheck= t('layout.users.delete_avatar')
.both
= f.file_field :avatar
.both
= render 'shared/avatar_form', :subject => @user, :f => f
.leftlist= f.label :professional_experience, t("activerecord.attributes.user.professional_experience")
.rightlist= f.text_area :professional_experience
.both

View File

@ -1,4 +1,4 @@
-set_meta_tags :title => title_object(@user)
- edit_link = can?(:edit, @user) ? link_to(t("layout.users.settings"), current_user == @user ? profile_settings_path : edit_admin_user_path(@user), :class => 'button width81') : nil
= render 'shared/profile', :uname => @user.uname, :name => @user.name, :user => @user, :search_path => user_path, :projects => @projects, :edit_link => edit_link, :avatar_url => avatar_url(@user, :big)
= render 'shared/profile', :uname => @user.uname, :name => @user.name, :user => @user, :search_path => user_path, :projects => @projects, :edit_link => edit_link

View File

@ -7,7 +7,7 @@
.notify
%p= t('layout.users.public_data_edit_warning')
.notify
%p= t('layout.users.avatar_notice')
%p= t('layout.avatars.avatar_notice')
:javascript
$('article .right').addClass('middlepadding');

View File

@ -0,0 +1,6 @@
en:
layout:
avatars:
avatar_notice: Without uploaded avatar will be used avatar from gravar web service.
delete_avatar: Delete avatar
avatar_with_size: Avatar (less than %{max})

View File

@ -0,0 +1,6 @@
ru:
layout:
avatars:
avatar_notice: При отсутствии загруженного аватара будет использован Ваш аватар на сервисе gravatar.
delete_avatar: Удалить аватар
avatar_with_size: Аватар (менее %{max})

View File

@ -21,9 +21,6 @@ en:
delete_header: Delete aacount
delete_warning: Warning! Deleted account can not be recovered.
private_settings_header: Password change
avatar_notice: Without uploaded avatar will be used avatar from gravar web service.
delete_avatar: Delete avatar
avatar_with_size: Avatar (less than %{max})
users_filter:
all: All
admin: Admins

View File

@ -21,9 +21,6 @@ ru:
delete_header: Удалить аккаунт
delete_warning: Внимание! Удаленный аккаунт восстановлению не подлежит.
private_settings_header: Изменение пароля
avatar_notice: При отсутствии загруженного аватара будет использован Ваш аватар на сервисе gravatar.
delete_avatar: Удалить аватар
avatar_with_size: Аватар (менее %{max})
users_filter:
all: Все
admin: Админы

View File

@ -0,0 +1,11 @@
class AddAvatarToGroups < ActiveRecord::Migration
def change
change_table :groups do |t|
t.has_attached_file :avatar
end
end
def self.down
drop_attached_file :groups, :avatar
end
end

View File

@ -11,14 +11,14 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121003081546) do
ActiveRecord::Schema.define(:version => 20121003154246) do
create_table "activity_feeds", :force => true do |t|
t.integer "user_id", :null => false
t.string "kind"
t.text "data"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "advisories", :force => true do |t|
@ -53,8 +53,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "arches", ["name"], :name => "index_arches_on_name", :unique => true
@ -63,8 +63,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "authentications", ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true
@ -75,8 +75,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.integer "level"
t.integer "status"
t.integer "build_list_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "version"
end
@ -110,8 +110,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.integer "project_id"
t.integer "arch_id"
t.datetime "notified_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_circle", :default => false
t.text "additional_repos"
t.string "name"
@ -142,8 +142,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "commentable_type"
t.integer "user_id"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.decimal "commentable_id", :precision => 50, :scale => 0
t.integer "project_id"
end
@ -160,8 +160,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "controller"
t.string "action"
t.text "message"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "flash_notifies", :force => true do |t|
@ -175,11 +175,15 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "groups", :force => true do |t|
t.integer "owner_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "uname"
t.integer "own_projects_count", :default => 0, :null => false
t.integer "own_projects_count", :default => 0, :null => false
t.text "description"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
create_table "issues", :force => true do |t|
@ -189,8 +193,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "title"
t.text "body"
t.string "status", :default => "open"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.datetime "closed_at"
t.integer "closed_by"
@ -250,14 +254,14 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "description"
t.string "name", :null => false
t.integer "parent_platform_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "released", :default => false, :null => false
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open", :null => false
t.string "platform_type", :default => "main", :null => false
t.string "distrib_type"
t.string "distrib_type", :null => false
end
add_index "platforms", ["name"], :name => "index_platforms_on_name", :unique => true, :case_sensitive => false
@ -266,16 +270,16 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.integer "platform_id"
t.string "login"
t.string "password"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "product_build_lists", :force => true do |t|
t.integer "product_id"
t.integer "status", :default => 2, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "product_build_lists", ["product_id"], :name => "index_product_build_lists_on_product_id"
@ -283,8 +287,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "products", :force => true do |t|
t.string "name", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.text "build_script"
t.text "counter"
t.text "ks"
@ -303,8 +307,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "name"
t.string "version"
t.datetime "file_mtime"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "platform_id"
end
@ -313,25 +317,25 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "project_to_repositories", :force => true do |t|
t.integer "project_id"
t.integer "repository_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open"
t.text "description"
t.string "ancestry"
t.boolean "has_issues", :default => true
t.boolean "has_wiki", :default => false
t.string "srpm_file_name"
t.string "srpm_content_type"
t.integer "srpm_file_size"
t.datetime "srpm_updated_at"
t.string "srpm_content_type"
t.boolean "has_wiki", :default => false
t.string "default_branch", :default => "master"
t.boolean "is_package", :default => true, :null => false
t.integer "average_build_time", :default => 0, :null => false
@ -359,8 +363,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "token"
t.boolean "approved", :default => false
t.boolean "rejected", :default => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "interest"
t.text "more"
t.string "language"
@ -374,16 +378,16 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "actor_type"
t.integer "target_id"
t.string "target_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
end
create_table "repositories", :force => true do |t|
t.string "description", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", :null => false
t.boolean "publish_without_qa", :default => true
end
@ -395,8 +399,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.boolean "new_comment_reply", :default => true
t.boolean "new_issue", :default => true
t.boolean "issue_assign", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "new_comment_commit_owner", :default => true
t.boolean "new_comment_commit_repo_owner", :default => true
t.boolean "new_comment_commit_commentor", :default => true
@ -407,8 +411,8 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "subscribes", :force => true do |t|
t.string "subscribeable_type"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "status", :default => true
t.integer "project_id"
t.decimal "subscribeable_id", :precision => 50, :scale => 0
@ -416,21 +420,18 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.text "ssh_key"
t.string "uname"
t.string "role"
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.text "professional_experience"
t.string "site"
t.string "company"
@ -439,11 +440,14 @@ ActiveRecord::Schema.define(:version => 20121003081546) do
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "failed_attempts", :default => 0
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "authentication_token"
t.integer "build_priority", :default => 50
t.integer "build_priority", :default => 50
end
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token"