diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 9b947578d..498d11926 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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 diff --git a/app/controllers/groups/profile_controller.rb b/app/controllers/groups/profile_controller.rb index 39b6f9619..3e7795680 100644 --- a/app/controllers/groups/profile_controller.rb +++ b/app/controllers/groups/profile_controller.rb @@ -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 diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index cae985b96..3ff416963 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -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 diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb new file mode 100644 index 000000000..b292c065e --- /dev/null +++ b/app/helpers/avatar_helper.rb @@ -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 \ No newline at end of file diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 94d80dcd0..1424c819f 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -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 diff --git a/app/models/avatar.rb b/app/models/avatar.rb new file mode 100644 index 000000000..2fbefa5e8 --- /dev/null +++ b/app/models/avatar.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index c09af034f..3d8661f94 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 027175d5c..fe76e0b3e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/groups/profile/_form.html.haml b/app/views/groups/profile/_form.html.haml index f30cb0ab4..4ac4eaf82 100644 --- a/app/views/groups/profile/_form.html.haml +++ b/app/views/groups/profile/_form.html.haml @@ -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") diff --git a/app/views/shared/_avatar_form.html.haml b/app/views/shared/_avatar_form.html.haml new file mode 100644 index 000000000..9729a6b9b --- /dev/null +++ b/app/views/shared/_avatar_form.html.haml @@ -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 \ No newline at end of file diff --git a/app/views/shared/_profile.html.haml b/app/views/shared/_profile.html.haml index 083cbdd8c..6f8c14778 100644 --- a/app/views/shared/_profile.html.haml +++ b/app/views/shared/_profile.html.haml @@ -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 diff --git a/app/views/users/base/_form.html.haml b/app/views/users/base/_form.html.haml index 4baa5218e..e661a9191 100644 --- a/app/views/users/base/_form.html.haml +++ b/app/views/users/base/_form.html.haml @@ -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 diff --git a/app/views/users/profile/show.html.haml b/app/views/users/profile/show.html.haml index 0dca78c81..5c16bf2fa 100644 --- a/app/views/users/profile/show.html.haml +++ b/app/views/users/profile/show.html.haml @@ -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) \ No newline at end of file += render 'shared/profile', :uname => @user.uname, :name => @user.name, :user => @user, :search_path => user_path, :projects => @projects, :edit_link => edit_link \ No newline at end of file diff --git a/app/views/users/settings/profile.html.haml b/app/views/users/settings/profile.html.haml index bcf053457..d09263464 100644 --- a/app/views/users/settings/profile.html.haml +++ b/app/views/users/settings/profile.html.haml @@ -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'); diff --git a/config/locales/models/avatar.en.yml b/config/locales/models/avatar.en.yml new file mode 100644 index 000000000..eda4a76ab --- /dev/null +++ b/config/locales/models/avatar.en.yml @@ -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}) \ No newline at end of file diff --git a/config/locales/models/avatar.ru.yml b/config/locales/models/avatar.ru.yml new file mode 100644 index 000000000..4fac6e7a5 --- /dev/null +++ b/config/locales/models/avatar.ru.yml @@ -0,0 +1,6 @@ +ru: + layout: + avatars: + avatar_notice: При отсутствии загруженного аватара будет использован Ваш аватар на сервисе gravatar. + delete_avatar: Удалить аватар + avatar_with_size: Аватар (менее %{max}) \ No newline at end of file diff --git a/config/locales/users.en.yml b/config/locales/users.en.yml index 5df7db477..4dc842606 100644 --- a/config/locales/users.en.yml +++ b/config/locales/users.en.yml @@ -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 diff --git a/config/locales/users.ru.yml b/config/locales/users.ru.yml index ec6b25a04..f85f57523 100644 --- a/config/locales/users.ru.yml +++ b/config/locales/users.ru.yml @@ -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: Админы diff --git a/db/migrate/20121003154246_add_avatar_to_groups.rb b/db/migrate/20121003154246_add_avatar_to_groups.rb new file mode 100644 index 000000000..96a835ca8 --- /dev/null +++ b/db/migrate/20121003154246_add_avatar_to_groups.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 3886a06af..97372c6ea 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"