From e71a9e84cb847e1eca26ea05cd82d143ab1da9ad Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 27 Jan 2012 02:11:07 +0600 Subject: [PATCH] [refs #114] emails control --- app/controllers/user_emails_controller.rb | 32 ++++++++----------- app/models/comment.rb | 2 +- app/models/subscribe.rb | 2 +- app/models/user.rb | 10 ++++-- app/views/devise/registrations/edit.html.haml | 2 +- app/views/users/emails.html.haml | 0 app/views/users/emails/_form.html.haml | 15 +++++++++ app/views/users/emails/emails.html.haml | 5 +++ config/locales/en.yml | 9 ++++-- config/locales/ru.yml | 8 ++++- config/routes.rb | 6 ++-- spec/models/comment_for_commit_spec.rb | 12 +++---- 12 files changed, 67 insertions(+), 36 deletions(-) delete mode 100644 app/views/users/emails.html.haml create mode 100644 app/views/users/emails/_form.html.haml create mode 100644 app/views/users/emails/emails.html.haml diff --git a/app/controllers/user_emails_controller.rb b/app/controllers/user_emails_controller.rb index d85e930e5..64ba07341 100644 --- a/app/controllers/user_emails_controller.rb +++ b/app/controllers/user_emails_controller.rb @@ -1,27 +1,21 @@ # coding: UTF-8 -class UserEmailsController < UsersController - before_filter :find_user +class UserEmailsController < ApplicationController + layout 'sessions' + before_filter :authenticate_user! - def index - @emails = @user.emails - (5 - @user.emails.count).times {|e| @emails << UserEmail.new(:user_id => @user) } + def edit + (5 - current_user.emails.count).times {current_user.emails.build } + render 'users/emails/emails' end def update - @user.role = params[:user][:role] - if @user.update_attributes(params[:user]) - flash[:notice] = t('flash.user.saved') - redirect_to users_path - else - flash[:error] = t('flash.user.save_error') - render :action => :edit - end - end + new_emails = [] + params[:user][:emails_attributes].each_value {|x| new_emails << x[:email] if x[:email].present?} + emails = current_user.emails + emails.each {|e| e.destroy unless new_emails.include?(e.email)} + new_emails.each {|e| emails.create(:email => e) unless emails.include? e} - def destroy - @user.destroy - flash[:notice] = t("flash.user.destroyed") - redirect_to users_path + flash[:notice] = t('flash.user.emails.saved') + redirect_to edit_user_emails_path end - end diff --git a/app/models/comment.rb b/app/models/comment.rb index d59d670e8..48db60efc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -28,7 +28,7 @@ class Comment < ActiveRecord::Base self.commentable.subscribes.create(:user_id => self.user_id) if !self.commentable.subscribes.exists?(:user_id => self.user_id) elsif self.commentable.class == Grit::Commit recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map &:object # admins - recipients << self.user << User.where(:email => self.commentable.committer.email).first # commentor and committer + recipients << self.user << UserEmail.where(:email => self.commentable.committer.email).first.try(:user) # commentor and committer recipients << self.project.owner if self.project.owner_type == 'User' # project owner recipients.compact.uniq.each {|user| Subscribe.subscribe_user_to_commit(self, user.id)} end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 7ea9f612e..089c6c97e 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -42,7 +42,7 @@ class Subscribe < ActiveRecord::Base def self.subscribed_to_commit?(project, user, commentable) is_commentor = (Comment.where(:commentable_type => commentable.class.name, :commentable_id => commentable.id).exists?(:user_id => user.id)) - is_committer = (user.email == commentable.committer.email) + is_committer = (user.emails.exists? :email => commentable.committer.email) return false if user.subscribes.where(:subscribeable_id => commentable.id, :subscribeable_type => commentable.class.name, :project_id => project.id, :status => Subscribe::OFF).first.present? (project.owner?(user) && user.notifier.new_comment_commit_repo_owner) or diff --git a/app/models/user.rb b/app/models/user.rb index 80dc16abe..1665dcf09 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,7 +25,7 @@ class User < ActiveRecord::Base has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy - has_many :user_emails, :dependent => :destroy, :as => :emails + has_many :emails, :class_name => 'UserEmail', :dependent => :destroy include Modules::Models::PersonalRepository @@ -39,7 +39,10 @@ class User < ActiveRecord::Base attr_readonly :uname attr_accessor :login + accepts_nested_attributes_for :emails, :allow_destroy => true + after_create :create_settings_notifier + after_create :add_user_email def admin? role == 'admin' @@ -57,7 +60,7 @@ class User < ActiveRecord::Base conditions = warden_conditions.dup login = conditions.delete(:login) where(conditions).where("lower(uname) = :value OR " + - "exists (select null from user_emails m where users.user_id = m.user_id and lower(m.email) = :value)", + "exists (select null from user_emails m where m.user_id = m.user_id and lower(m.email) = :value)", {:value => login.downcase}).first end @@ -94,4 +97,7 @@ class User < ActiveRecord::Base self.create_notifier end + def add_user_email + UserEmail.create(:user_id => self.id, :email => self.email) + end end diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index 2202a2a97..090c2c1e2 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -78,4 +78,4 @@ = link_to t('layout.settings.notifier'), user_settings_notifier_path(current_user)#, :class => "text_button_padding link_button" .group.navform.wat-cf - = link_to t('layout.settings.emails'), user_emails_path(current_user)#, :class => "text_button_padding link_button" + = link_to t('layout.settings.emails'), edit_user_emails_path(current_user.id)#, :class => "text_button_padding link_button" diff --git a/app/views/users/emails.html.haml b/app/views/users/emails.html.haml deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/views/users/emails/_form.html.haml b/app/views/users/emails/_form.html.haml new file mode 100644 index 000000000..3fdb58400 --- /dev/null +++ b/app/views/users/emails/_form.html.haml @@ -0,0 +1,15 @@ += f.fields_for :emails do |m| + .group + .left + = m.text_field :email, :class => :text_field + .right + +.group.navform.wat-cf + %button.button{:type => "submit"} + = image_tag("web-app-theme/icons/tick.png", :alt => t("layout.save")) + = t("layout.save") + %span.text_button_padding= t("layout.or") + = link_to t("layout.cancel"), edit_user_emails_path(current_user.id), :class => "text_button_padding link_button" +.group.navform.wat-cf + %span.text_button_padding + = link_to t('layout.back'), edit_user_registration_path, :class => "text_button_padding link_button" diff --git a/app/views/users/emails/emails.html.haml b/app/views/users/emails/emails.html.haml new file mode 100644 index 000000000..096e1055d --- /dev/null +++ b/app/views/users/emails/emails.html.haml @@ -0,0 +1,5 @@ +#block-signup.block + %h2= title t("layout.settings.email.list") + .content + = form_for current_user, :url => update_user_emails_path(@user), :html => {:method => :put, :id => current_user.id } do |f| + = render :partial => "users/emails/form", :locals => {:f => f} diff --git a/config/locales/en.yml b/config/locales/en.yml index f8ae03bcf..5cebbccc9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -45,7 +45,10 @@ en: notifier: Notifier setting notifiers: edit_header: Notifier setting - email: Email addresses + email: + list: Email addresses + delete: Destroy + confirm_delete: Are you sure? processing: working ... downloads: @@ -430,7 +433,9 @@ en: saved: User saved save_error: User data saves error destroyed: User account deleted - + emails: + saved: User emails saved + error: That emails addresses is already in use group: saved: Group saved save_error: Group saves error diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 80aed8c2b..a558ceb75 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -44,7 +44,10 @@ ru: notifier: Настройки оповещений notifiers: edit_header: Настройки оповещений - emails: Список Email + email: + list: Список Email + delete: Удалить + confirm_delete: Вы уверены? processing: Обрабатывается... downloads: @@ -425,6 +428,9 @@ ru: saved: Пользователь успешно сохранен save_error: Не удалось сохранить данные о пользователе destroyed: Учетная запись успешно удалена + emails: + saved: Список еmail успешно сохранен + error: Такой email уже используется group: saved: Группа успешно сохранена diff --git a/config/routes.rb b/config/routes.rb index 318af8f5f..671ffa1f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,9 +8,6 @@ Rosa::Application.routes.draw do resources :users do resources :groups, :only => [:new, :create, :index] - resources :emails, :only => [:index, :destroy], :controller => :user_emails do - put :update, :as => :member - end get :autocomplete_user_uname, :on => :collection namespace :settings do @@ -18,6 +15,9 @@ Rosa::Application.routes.draw do end end + match 'users/:id/emails' => 'user_emails#edit', :as => :edit_user_emails, :via => :get, :action => :edit + match 'users/:id/emails' => 'user_emails#update', :as => :update_user_emails, :via => :put, :action => :update + resources :event_logs, :only => :index #resources :downloads, :only => :index diff --git a/spec/models/comment_for_commit_spec.rb b/spec/models/comment_for_commit_spec.rb index 3b862751b..cc16b2d60 100644 --- a/spec/models/comment_for_commit_spec.rb +++ b/spec/models/comment_for_commit_spec.rb @@ -341,7 +341,7 @@ describe Comment do context 'for committer' do it 'should send an e-mail' do ActionMailer::Base.deliveries = [] - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) ActionMailer::Base.deliveries.count.should == 1 @@ -350,7 +350,7 @@ describe Comment do it 'should send an e-mail with user locale' do ActionMailer::Base.deliveries = [] - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' @stranger.update_attribute :language, 'ru' comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) @@ -362,7 +362,7 @@ describe Comment do it 'should send a one e-mail when subscribed to commit' do ActionMailer::Base.deliveries = [] Subscribe.set_subscribe_to_commit(@project, @commit, @stranger.id, Subscribe::ON) - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) ActionMailer::Base.deliveries.count.should == 1 @@ -372,7 +372,7 @@ describe Comment do it 'should not send an e-mail for own comment' do ActionMailer::Base.deliveries = [] #@project.owner.notifier.update_attribute :can_notify, false - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) ActionMailer::Base.deliveries.count.should == 1 @@ -382,7 +382,7 @@ describe Comment do it 'should not send an e-mail if global notify off' do ActionMailer::Base.deliveries = [] @project.owner.notifier.update_attribute :can_notify, false - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' @stranger.notifier.update_attribute :can_notify, false comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) @@ -392,7 +392,7 @@ describe Comment do it 'should not send an e-mail if notify for my commits off' do ActionMailer::Base.deliveries = [] @stranger.notifier.update_attribute :new_comment_commit_owner, false - @stranger.update_attribute :email, 'code@tpope.net' + @stranger.emails.first.update_attribute :email, 'code@tpope.net' comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, :commentable_type => @commit.class.name, :commentable_id => @commit.id) ActionMailer::Base.deliveries.count.should == 0