[refs #54] Add settings notifier

This commit is contained in:
konstantin.grabar 2012-01-11 17:58:13 +04:00
parent dda40d9e9d
commit 2b8aca5f09
19 changed files with 181 additions and 9 deletions

View File

@ -0,0 +1,22 @@
class Settings::NotifiersController < ApplicationController
layout "sessions"
before_filter :authenticate_user!
load_and_authorize_resource :user
load_and_authorize_resource :class => Settings::Notifier, :through => :user, :singleton => true, :shallow => true
def show
end
def update
if @notifier.update_attributes(params[:settings_notifier])
flash[:notice] = I18n.t("flash.settings.saved")
redirect_to [@user, @notifier]
else
flash[:notice] = I18n.t("flash.settings.save_error")
redirect_to [@user, @notifier]
end
end
end

View File

@ -0,0 +1,2 @@
module Settings::NotifiersHelper
end

View File

@ -28,6 +28,8 @@ class Ability
else # Registered user rights
can [:show, :autocomplete_user_uname], User
can [:show, :update], Settings::Notifier, :user_id => user.id
can [:read, :create], Group
can [:update, :manage_members], Group do |group|
group.objects.exists?(:object_type => 'User', :object_id => user.id, :role => 'admin') # or group.owner_id = user.id

View File

@ -41,7 +41,7 @@ class Issue < ActiveRecord::Base
end
def deliver_issue_assign_notification
UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id
UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id && self.user.notifier.issue_assign
end
def subscribe_users
@ -56,7 +56,12 @@ class Issue < ActiveRecord::Base
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map { |rel| rel.read_attribute(:object_id) }
recipients = recipients | [self.user_id] if self.user_id
recipients = recipients | [self.project.owner_id] if self.project.owner_type == 'User'
recipients
# filter by notification settings
recipients = recipients.select do |recipient|
User.find(recipient).notifier.new_issue
end
recipients
end
end

5
app/models/settings.rb Normal file
View File

@ -0,0 +1,5 @@
module Settings
def self.table_name_prefix
'settings_'
end
end

View File

@ -0,0 +1,5 @@
class Settings::Notifier < ActiveRecord::Base
belongs_to :user
validates :user_id, :presence => true
end

View File

@ -4,6 +4,8 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
:recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable
has_one :notifier, :class_name => 'Settings::Notifier' #:notifier
has_many :authentications, :dependent => :destroy
has_many :build_lists, :dependent => :destroy
@ -31,6 +33,8 @@ class User < ActiveRecord::Base
attr_readonly :uname
attr_accessor :login
after_create :create_settings_notifier
def admin?
role == 'admin'
end
@ -76,4 +80,10 @@ class User < ActiveRecord::Base
result
end
private
def create_settings_notifier
self.create_notifier
end
end

View File

@ -68,3 +68,5 @@
%span.text_button_padding
= link_to t('layout.back'), :back, :class => "text_button_padding link_button"
.group.navform.wat-cf
= link_to t('layout.settings.notifier'), user_settings_notifier_path(current_user)#, :class => "text_button_padding link_button"

View File

@ -0,0 +1,23 @@
.group
= f.label :new_comment, t('activerecord.attributes.settings.notifier.new_comment'), :class => :label
= f.check_box :new_comment#, :class => 'text_field'
.group
= f.label :new_comment_reply, t('activerecord.attributes.settings.notifier.new_comment_reply'), :class => :label
= f.check_box :new_comment_reply#, :class => 'text_field'
.group
= f.label :new_issue, t('activerecord.attributes.settings.notifier.new_issue'), :class => :label
= f.check_box :new_issue#, :class => 'text_field'
.group
= f.label :issue_assign, t('activerecord.attributes.settings.notifier.issue_assign'), :class => :label
= f.check_box :issue_assign#, :class => 'text_field'
.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"), user_settings_notifier_path(@user), :class => "text_button_padding link_button"

View File

@ -0,0 +1,5 @@
#block-signup.block
%h2= title t("layout.settings.notifiers.edit_header")
.content
= form_for @notifier, :url => user_settings_notifier_path(@user), :html => { :class => :form } do |f|
= render :partial => "form", :locals => {:f => f}

View File

@ -30,6 +30,10 @@ ru:
not_access: Нет доступа!
owner: Владелец
confirm: Уверенны?
settings:
notifier: Настройки оповещений
notifiers:
edit_header: Настройки оповещений
downloads:
title: Статистика закачек пакетов
@ -353,6 +357,11 @@ ru:
project_version_not_found: версия не найден
flash:
settings:
saved: Настройки успешно сохранены
save_error: При обновлении настроек произошла ошибка
subscribe:
saved: Вы подписаны на оповещения для этой задачи
destroyed: Подписка на оповещения для этой задачи убрана
@ -478,8 +487,17 @@ ru:
build_list_item: Элемент сборочного листа
download: Статистика
auto_build_list: Автоматическая пересборка пакетов
settings:
notifier: Настройки оповещений
attributes:
settings:
notifier:
new_comment: Оповещать о новом комментарии в задаче
new_comment_reply: Оповещать о новом ответе на мой комментарий
new_issue: Оповещать о новых задачах в моих проектах
issue_assign: Оповещать, когда на меня выставляют задачу
auto_build_list:
project_id: Проект
project: Проект

View File

@ -9,6 +9,9 @@ Rosa::Application.routes.draw do
resources :users do
resources :groups, :only => [:new, :create, :index]
get :autocomplete_user_uname, :on => :collection
namespace :settings do
resource :notifier, :only => [:show, :update]
end
end
resources :event_logs, :only => :index

View File

@ -0,0 +1,17 @@
class CreateSettingsNotifiers < ActiveRecord::Migration
def self.up
create_table :settings_notifiers do |t|
t.integer :user_id, :null => false
t.boolean :new_comment, :default => true
t.boolean :new_comment_reply, :default => true
t.boolean :new_issue, :default => true
t.boolean :issue_assign, :default => true
t.timestamps
end
end
def self.down
drop_table :settings_notifiers
end
end

View File

@ -0,0 +1,13 @@
class AddSettingsNotifierToAllUsers < ActiveRecord::Migration
def self.up
User.all.each do |user|
user.create_notifier
end
end
def self.down
User.all.each do |user|
user.notifier.destroy
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111228182425) do
ActiveRecord::Schema.define(:version => 20120111135443) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
@ -246,7 +246,6 @@ ActiveRecord::Schema.define(:version => 20111228182425) do
t.string "object_type"
t.integer "target_id"
t.string "target_type"
t.integer "role_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
@ -273,6 +272,16 @@ ActiveRecord::Schema.define(:version => 20111228182425) do
add_index "rpms", ["project_id", "arch_id"], :name => "index_rpms_on_project_id_and_arch_id"
add_index "rpms", ["project_id"], :name => "index_rpms_on_project_id"
create_table "settings_notifiers", :force => true do |t|
t.integer "user_id", :null => false
t.boolean "new_comment", :default => true
t.boolean "new_comment_reply", :default => true
t.boolean "new_issue", :default => true
t.boolean "issue_assign", :default => true
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "subscribes", :force => true do |t|
t.integer "subscribeable_id"
t.string "subscribeable_type"
@ -285,14 +294,14 @@ ActiveRecord::Schema.define(:version => 20111228182425) do
t.string "name"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.string "remember_token"
t.datetime "remember_created_at"
t.datetime "created_at"
t.datetime "updated_at"
t.string "uname"
t.text "ssh_key"
t.integer "role_id"
t.string "uname"
t.string "role"
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Settings::NotifiersController do
end

View File

@ -0,0 +1,6 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :notifier do
end
end

View File

@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the Settings::NotifiersHelper. For example:
#
# describe Settings::NotifiersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe Settings::NotifiersHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Settings::Notifier do
pending "add some examples to (or delete) #{__FILE__}"
end