From 7b28218f96d14c9650484a183ffab3b2132b05db Mon Sep 17 00:00:00 2001 From: Senorsen Date: Tue, 14 Apr 2015 00:28:49 +0800 Subject: Allow user to choose which email to be public This commit allows user to show one of their emails in profile page, or don't show email in this page. --- app/controllers/profiles/emails_controller.rb | 4 +++- app/controllers/profiles_controller.rb | 7 ++++--- app/models/user.rb | 9 +++++++++ app/views/profiles/emails/index.html.haml | 4 ++++ app/views/profiles/show.html.haml | 5 +++++ app/views/users/_profile.html.haml | 4 ++++ 6 files changed, 29 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/controllers/profiles/emails_controller.rb b/app/controllers/profiles/emails_controller.rb index 4a65c978e5c..954c98c0d9f 100644 --- a/app/controllers/profiles/emails_controller.rb +++ b/app/controllers/profiles/emails_controller.rb @@ -3,6 +3,7 @@ class Profiles::EmailsController < ApplicationController def index @primary = current_user.email + @public_email = current_user.public_email @emails = current_user.emails end @@ -19,7 +20,8 @@ class Profiles::EmailsController < ApplicationController @email.destroy current_user.set_notification_email - current_user.save if current_user.notification_email_changed? + current_user.set_public_email + current_user.save if current_user.notification_email_changed? or current_user.public_email_changed? respond_to do |format| format.html { redirect_to profile_emails_url } diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 9252e85e8cc..7f76906066d 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -67,9 +67,10 @@ class ProfilesController < ApplicationController def user_params params.require(:user).permit( - :email, :password, :password_confirmation, :bio, :name, :username, - :skype, :linkedin, :twitter, :website_url, :color_scheme_id, :theme_id, - :avatar, :hide_no_ssh_key, :hide_no_password, :location + :email, :password, :password_confirmation, :bio, :name, + :username, :skype, :linkedin, :twitter, :website_url, + :color_scheme_id, :theme_id, :avatar, :hide_no_ssh_key, + :hide_no_password, :location, :public_email ) end end diff --git a/app/models/user.rb b/app/models/user.rb index a40111e62dd..d6b93afe739 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -49,6 +49,7 @@ # password_automatically_set :boolean default(FALSE) # bitbucket_access_token :string(255) # bitbucket_access_token_secret :string(255) +# public_email :string(255) default(""), not null # require 'carrierwave/orm/activerecord' @@ -123,6 +124,7 @@ class User < ActiveRecord::Base validates :name, presence: true validates :email, presence: true, email: { strict_mode: true }, uniqueness: true validates :notification_email, presence: true, email: { strict_mode: true } + validates :public_email, presence: true, email: { strict_mode: true }, allow_blank: true, uniqueness: true validates :bio, length: { maximum: 255 }, allow_blank: true validates :projects_limit, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :username, @@ -142,6 +144,7 @@ class User < ActiveRecord::Base before_validation :generate_password, on: :create before_validation :sanitize_attrs before_validation :set_notification_email, if: ->(user) { user.email_changed? } + before_validation :set_public_email, if: ->(user) { user.public_email_changed? } before_save :ensure_authentication_token after_save :ensure_namespace_correct @@ -443,6 +446,12 @@ class User < ActiveRecord::Base end end + def set_public_email + if self.public_email.blank? || !self.all_emails.include?(self.public_email) + self.public_email = '' + end + end + def set_projects_limit connection_default_value_defined = new_record? && !projects_limit_changed? return unless self.projects_limit.nil? || connection_default_value_defined diff --git a/app/views/profiles/emails/index.html.haml b/app/views/profiles/emails/index.html.haml index 9d8f33cbbaa..09f290429ea 100644 --- a/app/views/profiles/emails/index.html.haml +++ b/app/views/profiles/emails/index.html.haml @@ -20,9 +20,13 @@ %li %strong= @primary %span.label.label-success Primary Email + - if @primary === @public_email + %span.label.label-info Public Email - @emails.each do |email| %li %strong= email.email + - if email.email === @public_email + %span.label.label-info Public Email %span.cgray added #{time_ago_with_tooltip(email.created_at)} = link_to 'Remove', profile_email_path(email), data: { confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-sm btn-remove pull-right' diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml index 5a501e43149..6c745e69e40 100644 --- a/app/views/profiles/show.html.haml +++ b/app/views/profiles/show.html.haml @@ -41,6 +41,11 @@ - else %span.help-block We also use email for avatar detection if no avatar is uploaded. + .form-group + = f.label :public_email, class: "control-label" + .col-sm-10 + = f.select :public_email, options_for_select(@user.all_emails, selected: @user.public_email), {include_blank: 'Do not show in profile'}, class: "form-control" + %span.help-block This email will be displayed on your public profile. .form-group = f.label :skype, class: "control-label" .col-sm-10= f.text_field :skype, class: "form-control" diff --git a/app/views/users/_profile.html.haml b/app/views/users/_profile.html.haml index bca71444956..90d9980c85c 100644 --- a/app/views/users/_profile.html.haml +++ b/app/views/users/_profile.html.haml @@ -5,6 +5,10 @@ %li %span.light Member since %strong= user.created_at.stamp("Aug 21, 2011") + - unless user.public_email.blank? + %li + %span.light E-mail: + %strong= link_to user.public_email, "mailto:#{user.public_email}" - unless user.skype.blank? %li %span.light Skype: -- cgit v1.2.3