[refs #114] emails control

This commit is contained in:
Alexander Machehin 2012-01-27 02:11:07 +06:00
parent e04b2eb59b
commit e71a9e84cb
12 changed files with 67 additions and 36 deletions

View File

@ -1,27 +1,21 @@
# coding: UTF-8 # coding: UTF-8
class UserEmailsController < UsersController class UserEmailsController < ApplicationController
before_filter :find_user layout 'sessions'
before_filter :authenticate_user!
def index def edit
@emails = @user.emails (5 - current_user.emails.count).times {current_user.emails.build }
(5 - @user.emails.count).times {|e| @emails << UserEmail.new(:user_id => @user) } render 'users/emails/emails'
end end
def update def update
@user.role = params[:user][:role] new_emails = []
if @user.update_attributes(params[:user]) params[:user][:emails_attributes].each_value {|x| new_emails << x[:email] if x[:email].present?}
flash[:notice] = t('flash.user.saved') emails = current_user.emails
redirect_to users_path emails.each {|e| e.destroy unless new_emails.include?(e.email)}
else new_emails.each {|e| emails.create(:email => e) unless emails.include? e}
flash[:error] = t('flash.user.save_error')
render :action => :edit
end
end
def destroy flash[:notice] = t('flash.user.emails.saved')
@user.destroy redirect_to edit_user_emails_path
flash[:notice] = t("flash.user.destroyed")
redirect_to users_path
end end
end end

View File

@ -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) 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 elsif self.commentable.class == Grit::Commit
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map &:object # admins 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 << self.project.owner if self.project.owner_type == 'User' # project owner
recipients.compact.uniq.each {|user| Subscribe.subscribe_user_to_commit(self, user.id)} recipients.compact.uniq.each {|user| Subscribe.subscribe_user_to_commit(self, user.id)}
end end

View File

@ -42,7 +42,7 @@ class Subscribe < ActiveRecord::Base
def self.subscribed_to_commit?(project, user, commentable) 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_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, 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_id => project.id, :status => Subscribe::OFF).first.present?
(project.owner?(user) && user.notifier.new_comment_commit_repo_owner) or (project.owner?(user) && user.notifier.new_comment_commit_repo_owner) or

View File

@ -25,7 +25,7 @@ class User < ActiveRecord::Base
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy 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 include Modules::Models::PersonalRepository
@ -39,7 +39,10 @@ class User < ActiveRecord::Base
attr_readonly :uname attr_readonly :uname
attr_accessor :login attr_accessor :login
accepts_nested_attributes_for :emails, :allow_destroy => true
after_create :create_settings_notifier after_create :create_settings_notifier
after_create :add_user_email
def admin? def admin?
role == 'admin' role == 'admin'
@ -57,7 +60,7 @@ class User < ActiveRecord::Base
conditions = warden_conditions.dup conditions = warden_conditions.dup
login = conditions.delete(:login) login = conditions.delete(:login)
where(conditions).where("lower(uname) = :value OR " + 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 {:value => login.downcase}).first
end end
@ -94,4 +97,7 @@ class User < ActiveRecord::Base
self.create_notifier self.create_notifier
end end
def add_user_email
UserEmail.create(:user_id => self.id, :email => self.email)
end
end end

View File

@ -78,4 +78,4 @@
= link_to t('layout.settings.notifier'), user_settings_notifier_path(current_user)#, :class => "text_button_padding link_button" = link_to t('layout.settings.notifier'), user_settings_notifier_path(current_user)#, :class => "text_button_padding link_button"
.group.navform.wat-cf .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"

View File

@ -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"

View File

@ -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}

View File

@ -45,7 +45,10 @@ en:
notifier: Notifier setting notifier: Notifier setting
notifiers: notifiers:
edit_header: Notifier setting edit_header: Notifier setting
email: Email addresses email:
list: Email addresses
delete: Destroy
confirm_delete: Are you sure?
processing: working ... processing: working ...
downloads: downloads:
@ -430,7 +433,9 @@ en:
saved: User saved saved: User saved
save_error: User data saves error save_error: User data saves error
destroyed: User account deleted destroyed: User account deleted
emails:
saved: User emails saved
error: That emails addresses is already in use
group: group:
saved: Group saved saved: Group saved
save_error: Group saves error save_error: Group saves error

View File

@ -44,7 +44,10 @@ ru:
notifier: Настройки оповещений notifier: Настройки оповещений
notifiers: notifiers:
edit_header: Настройки оповещений edit_header: Настройки оповещений
emails: Список Email email:
list: Список Email
delete: Удалить
confirm_delete: Вы уверены?
processing: Обрабатывается... processing: Обрабатывается...
downloads: downloads:
@ -425,6 +428,9 @@ ru:
saved: Пользователь успешно сохранен saved: Пользователь успешно сохранен
save_error: Не удалось сохранить данные о пользователе save_error: Не удалось сохранить данные о пользователе
destroyed: Учетная запись успешно удалена destroyed: Учетная запись успешно удалена
emails:
saved: Список еmail успешно сохранен
error: Такой email уже используется
group: group:
saved: Группа успешно сохранена saved: Группа успешно сохранена

View File

@ -8,9 +8,6 @@ Rosa::Application.routes.draw do
resources :users do resources :users do
resources :groups, :only => [:new, :create, :index] 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 get :autocomplete_user_uname, :on => :collection
namespace :settings do namespace :settings do
@ -18,6 +15,9 @@ Rosa::Application.routes.draw do
end end
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 :event_logs, :only => :index
#resources :downloads, :only => :index #resources :downloads, :only => :index

View File

@ -341,7 +341,7 @@ describe Comment do
context 'for committer' do context 'for committer' do
it 'should send an e-mail' do it 'should send an e-mail' do
ActionMailer::Base.deliveries = [] 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, comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 ActionMailer::Base.deliveries.count.should == 1
@ -350,7 +350,7 @@ describe Comment do
it 'should send an e-mail with user locale' do it 'should send an e-mail with user locale' do
ActionMailer::Base.deliveries = [] ActionMailer::Base.deliveries = []
@stranger.update_attribute :email, 'code@tpope.net' @stranger.emails.first.update_attribute :email, 'code@tpope.net'
@stranger.update_attribute :language, 'ru' @stranger.update_attribute :language, 'ru'
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :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 it 'should send a one e-mail when subscribed to commit' do
ActionMailer::Base.deliveries = [] ActionMailer::Base.deliveries = []
Subscribe.set_subscribe_to_commit(@project, @commit, @stranger.id, Subscribe::ON) 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, comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 ActionMailer::Base.deliveries.count.should == 1
@ -372,7 +372,7 @@ describe Comment do
it 'should not send an e-mail for own comment' do it 'should not send an e-mail for own comment' do
ActionMailer::Base.deliveries = [] ActionMailer::Base.deliveries = []
#@project.owner.notifier.update_attribute :can_notify, false #@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, comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 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 it 'should not send an e-mail if global notify off' do
ActionMailer::Base.deliveries = [] ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false @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 @stranger.notifier.update_attribute :can_notify, false
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project, comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :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 it 'should not send an e-mail if notify for my commits off' do
ActionMailer::Base.deliveries = [] ActionMailer::Base.deliveries = []
@stranger.notifier.update_attribute :new_comment_commit_owner, false @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, comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id) :commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0 ActionMailer::Base.deliveries.count.should == 0