From 77420d2a8f42648608ae866066645df0ae920710 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 26 Jan 2012 14:53:40 +0600 Subject: [PATCH] [refs #114] user emails --- app/controllers/user_emails_controller.rb | 27 +++++++++++++++++++ app/models/subscribe.rb | 2 +- app/models/user.rb | 10 ++++--- app/models/user_email.rb | 7 +++++ app/views/devise/registrations/edit.html.haml | 3 +++ app/views/users/emails.html.haml | 0 config/locales/en.yml | 7 ++--- config/locales/ru.yml | 5 ++-- config/routes.rb | 4 +++ .../20120123051330_create_user_emails.rb | 21 +++++++++++++++ db/schema.rb | 16 +++++++++-- spec/factories/user_emails.rb | 8 ++++++ spec/models/user_email_spec.rb | 5 ++++ 13 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 app/controllers/user_emails_controller.rb create mode 100644 app/models/user_email.rb create mode 100644 app/views/users/emails.html.haml create mode 100644 db/migrate/20120123051330_create_user_emails.rb create mode 100644 spec/factories/user_emails.rb create mode 100644 spec/models/user_email_spec.rb diff --git a/app/controllers/user_emails_controller.rb b/app/controllers/user_emails_controller.rb new file mode 100644 index 000000000..d85e930e5 --- /dev/null +++ b/app/controllers/user_emails_controller.rb @@ -0,0 +1,27 @@ +# coding: UTF-8 +class UserEmailsController < UsersController + before_filter :find_user + + def index + @emails = @user.emails + (5 - @user.emails.count).times {|e| @emails << UserEmail.new(:user_id => @user) } + 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 + + def destroy + @user.destroy + flash[:notice] = t("flash.user.destroyed") + redirect_to users_path + end + +end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 381feaf6c..785f0e9d5 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -7,7 +7,7 @@ class Subscribe < ActiveRecord::Base subscribes = comment.commentable.subscribes if commentable_class == Issue if commentable_class == Grit::Commit subscribes = comment.project.commit_comments_subscribes(true) # FIXME (true) for rspec - committer = User.where(:email => comment.commentable.committer.email).first + committer = User.includes(:user_emails).where("user_emails.email = ?", comment.commentable.committer.email).first UserMailer.delay.new_comment_notification(comment, committer) if committer && !comment.own_comment?(committer) && committer.notifier.new_comment_commit_owner && !committer.notifier.can_notify && subscribes.where(:user_id => committer).empty? end subscribes.each do |subscribe| diff --git a/app/models/user.rb b/app/models/user.rb index 16a399caa..f81faa602 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,6 +24,8 @@ class User < ActiveRecord::Base has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true + has_many :user_emails, :dependent => :destroy, :as => :emails + include Modules::Models::PersonalRepository validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ } @@ -41,7 +43,7 @@ class User < ActiveRecord::Base def admin? role == 'admin' end - + def guest? self.id.blank? # persisted? end @@ -53,7 +55,9 @@ class User < ActiveRecord::Base def find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:login) - where(conditions).where(["lower(uname) = :value OR lower(email) = :value", { :value => login.downcase }]).first + 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)", + {:value => login.downcase}).first end def new_with_session(params, session) @@ -82,7 +86,7 @@ class User < ActiveRecord::Base clean_up_passwords result end - + private def create_settings_notifier diff --git a/app/models/user_email.rb b/app/models/user_email.rb new file mode 100644 index 000000000..43c2b7adf --- /dev/null +++ b/app/models/user_email.rb @@ -0,0 +1,7 @@ +class UserEmail < ActiveRecord::Base + belongs_to :user + + validates :email, :uniqueness => true + validates :email, :presence => true + +end diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index b983f1836..2202a2a97 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -76,3 +76,6 @@ .group.navform.wat-cf = 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" diff --git a/app/views/users/emails.html.haml b/app/views/users/emails.html.haml new file mode 100644 index 000000000..e69de29bb diff --git a/config/locales/en.yml b/config/locales/en.yml index 4fa4edbc8..9c9fa1623 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,9 +1,9 @@ en: will_paginate: - previous_label: ‹ Previous!!! + previous_label: ‹ Previous!!! next_label: Next › page_gap: ... - + datatables: previous_label: ‹ Prev. next_label: Next ›b r @@ -44,6 +44,7 @@ en: notifier: Notifier setting notifiers: edit_header: Notifier setting + email: Email addresses processing: working ... downloads: @@ -690,4 +691,4 @@ en: new_comment_notification: New comment to your task new_issue_notification: New task added to project new_user_notification: Registered on project «%{ project_name }» - issue_assign_notification: New task assigned \ No newline at end of file + issue_assign_notification: New task assigned diff --git a/config/locales/ru.yml b/config/locales/ru.yml index a4786dee7..05ba8dff7 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -44,6 +44,7 @@ ru: notifier: Настройки оповещений notifiers: edit_header: Настройки оповещений + emails: Список Email processing: Обрабатывается... downloads: @@ -680,14 +681,14 @@ ru: status: Статус version: Версия build_list: Сборочный лист - + download: name: Название version: Версия distro: Дистрибутив platform: Архитектура counter: Закачки - + notifications: subjects: new_comment_notification: Новый комментарий к Вашей задаче diff --git a/config/routes.rb b/config/routes.rb index d36e2b7e2..4f347b428 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,10 @@ 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 resource :notifier, :only => [:show, :update] diff --git a/db/migrate/20120123051330_create_user_emails.rb b/db/migrate/20120123051330_create_user_emails.rb new file mode 100644 index 000000000..b148e8d56 --- /dev/null +++ b/db/migrate/20120123051330_create_user_emails.rb @@ -0,0 +1,21 @@ +class CreateUserEmails < ActiveRecord::Migration + def self.up + create_table :user_emails do |t| + t.integer :user_id, :null => false + t.string :email, :null => false + + t.timestamps + end + + add_index :user_emails, :user_id + add_index :user_emails, :email + UserEmail.reset_column_information + User.all.each do |u| + UserEmail.create(:user_id => u.id, :email => u.email) + end + end + + def self.down + drop_table :user_emails + end +end diff --git a/db/schema.rb b/db/schema.rb index 86e064a69..19d47b0c9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120118173141) do +ActiveRecord::Schema.define(:version => 20120123161250) do create_table "arches", :force => true do |t| t.string "name", :null => false @@ -304,13 +304,25 @@ ActiveRecord::Schema.define(:version => 20120118173141) do end create_table "subscribes", :force => true do |t| - t.integer "subscribeable_id" + t.string "subscribeable_id" t.string "subscribeable_type" t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" + t.integer "status", :default => 1 + t.integer "project_id" end + create_table "user_emails", :force => true do |t| + t.integer "user_id", :null => false + t.string "email", :null => false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "user_emails", ["email"], :name => "index_user_emails_on_email" + add_index "user_emails", ["user_id"], :name => "index_user_emails_on_user_id" + create_table "users", :force => true do |t| t.string "name" t.string "email", :default => "", :null => false diff --git a/spec/factories/user_emails.rb b/spec/factories/user_emails.rb new file mode 100644 index 000000000..ed339e424 --- /dev/null +++ b/spec/factories/user_emails.rb @@ -0,0 +1,8 @@ +# Read about factories at http://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :user_email do + user_id 1 + email "MyString" + end +end \ No newline at end of file diff --git a/spec/models/user_email_spec.rb b/spec/models/user_email_spec.rb new file mode 100644 index 000000000..f1c88c94c --- /dev/null +++ b/spec/models/user_email_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UserEmail do + pending "add some examples to (or delete) #{__FILE__}" +end