diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 4aa3172..370bdaa 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -4,6 +4,7 @@ class NotificationsController < ApplicationController def show @notification = current_user.notification @users_projects = current_user.users_projects + @users_groups = current_user.users_groups end def update @@ -12,6 +13,10 @@ class NotificationsController < ApplicationController @saved = if type == 'global' current_user.notification_level = params[:notification_level] current_user.save + elsif type == 'group' + users_group = current_user.users_groups.find(params[:notification_id]) + users_group.notification_level = params[:notification_level] + users_group.save else users_project = current_user.users_projects.find(params[:notification_id]) users_project.notification_level = params[:notification_level] diff --git a/app/models/users_group.rb b/app/models/users_group.rb index d70f56f..b334066 100644 --- a/app/models/users_group.rb +++ b/app/models/users_group.rb @@ -11,6 +11,8 @@ # class UsersGroup < ActiveRecord::Base + include Notifiable + GUEST = 10 REPORTER = 20 DEVELOPER = 30 diff --git a/app/models/users_project.rb b/app/models/users_project.rb index 58d05fb..a982b16 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -13,6 +13,7 @@ class UsersProject < ActiveRecord::Base include Gitlab::ShellAdapter + include Notifiable GUEST = 10 REPORTER = 20 @@ -30,7 +31,6 @@ class UsersProject < ActiveRecord::Base validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" } validates :project_access, inclusion: { in: [GUEST, REPORTER, DEVELOPER, MASTER] }, presence: true validates :project, presence: true - validates :notification_level, inclusion: { in: Notification.project_notification_levels }, presence: true delegate :name, :username, :email, to: :user, prefix: true @@ -134,8 +134,4 @@ class UsersProject < ActiveRecord::Base def skip_git? !!@skip_git end - - def notification - @notification ||= Notification.new(self) - end end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 379d2c5..1a999a6 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -148,13 +148,18 @@ class NotificationService # Get project users with WATCH notification level def project_watchers(project) - # Get project notification settings since it has higher priority - user_ids = project.users_projects.where(notification_level: Notification::N_WATCH).pluck(:user_id) - project_watchers = User.where(id: user_ids) + member_methods = [:users_projects] + member_methods << :users_groups if project.group - # next collect users who use global settings with watch state - user_ids = project.users_projects.where(notification_level: Notification::N_GLOBAL).pluck(:user_id) - project_watchers += User.where(id: user_ids, notification_level: Notification::N_WATCH) + member_methods.each do |member_method| + # Get project notification settings since it has higher priority + user_ids = project.send(member_method).where(notification_level: Notification::N_WATCH).pluck(:user_id) + project_watchers = User.where(id: user_ids) + + # next collect users who use global settings with watch state + user_ids = project.send(member_method).where(notification_level: Notification::N_GLOBAL).pluck(:user_id) + project_watchers += User.where(id: user_ids, notification_level: Notification::N_WATCH) + end project_watchers.uniq end diff --git a/app/views/notifications/_settings.html.haml b/app/views/notifications/_settings.html.haml new file mode 100644 index 0000000..1a4a4a9 --- /dev/null +++ b/app/views/notifications/_settings.html.haml @@ -0,0 +1,29 @@ +%li + .row + .span4 + %span + - if membership.kind_of? UsersGroup + = link_to membership.group.name, membership.group + - else + = link_to_project(membership.project) + .span7 + = form_tag profile_notifications_path, method: :put, remote: true, class: 'update-notifications' do + = hidden_field_tag :notification_type, type, id: dom_id(membership, 'notification_type') + = hidden_field_tag :notification_id, membership.id, id: dom_id(membership, 'notification_id') + + = label_tag do + = radio_button_tag :notification_level, Notification::N_GLOBAL, notification.global?, id: dom_id(membership, 'notification_level'), class: 'trigger-submit' + %span Use global setting + + = label_tag do + = radio_button_tag :notification_level, Notification::N_DISABLED, notification.disabled?, id: dom_id(membership, 'notification_level'), class: 'trigger-submit' + %span Disabled + + = label_tag do + = radio_button_tag :notification_level, Notification::N_PARTICIPATING, notification.participating?, id: dom_id(membership, 'notification_level'), class: 'trigger-submit' + %span Participating + + = label_tag do + = radio_button_tag :notification_level, Notification::N_WATCH, notification.watch?, id: dom_id(membership, 'notification_level'), class: 'trigger-submit' + %span Watch + diff --git a/app/views/notifications/show.html.haml b/app/views/notifications/show.html.haml index b7f3930..f4ac482 100644 --- a/app/views/notifications/show.html.haml +++ b/app/views/notifications/show.html.haml @@ -36,36 +36,19 @@ = link_to '#', class: 'js-toggle-visibility-link' do %h6.btn.btn-tiny %i.icon-chevron-down - %span Per project notifications setting - -%ul.well-list.js-toggle-visibility-container.hide - - @users_projects.each do |users_project| - - notification = Notification.new(users_project) - %li - .row - .span4 - %span - = link_to_project(users_project.project) - .span7 - = form_tag profile_notifications_path, method: :put, remote: true, class: 'update-notifications' do - = hidden_field_tag :notification_type, 'project', id: dom_id(users_project, 'notification_type') - = hidden_field_tag :notification_id, users_project.id, id: dom_id(users_project, 'notification_id') - - = label_tag do - = radio_button_tag :notification_level, Notification::N_GLOBAL, notification.global?, id: dom_id(users_project, 'notification_level'), class: 'trigger-submit' - %span Use global setting - - = label_tag do - = radio_button_tag :notification_level, Notification::N_DISABLED, notification.disabled?, id: dom_id(users_project, 'notification_level'), class: 'trigger-submit' - %span Disabled - - = label_tag do - = radio_button_tag :notification_level, Notification::N_PARTICIPATING, notification.participating?, id: dom_id(users_project, 'notification_level'), class: 'trigger-submit' - %span Participating - - = label_tag do - = radio_button_tag :notification_level, Notification::N_WATCH, notification.watch?, id: dom_id(users_project, 'notification_level'), class: 'trigger-submit' - %span Watch + %span Advanced notifications settings +.js-toggle-visibility-container.hide + %h5 Groups: + %ul.well-list + - @users_groups.each do |users_group| + - notification = Notification.new(users_group) + = render 'settings', type: 'project', membership: users_group, notification: notification + + %h5 Projects: + %ul.well-list + - @users_projects.each do |users_project| + - notification = Notification.new(users_project) + = render 'settings', type: 'project', membership: users_project, notification: notification .save-status-fixed diff --git a/db/schema.rb b/db/schema.rb index 415e972..500828a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130617095603) do +ActiveRecord::Schema.define(:version => 20130621195223) do create_table "deploy_keys_projects", :force => true do |t| t.integer "deploy_key_id", :null => false @@ -301,11 +301,12 @@ ActiveRecord::Schema.define(:version => 20130617095603) do add_index "users", ["username"], :name => "index_users_on_username" create_table "users_groups", :force => true do |t| - t.integer "group_access", :null => false - t.integer "group_id", :null => false - t.integer "user_id", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.integer "group_access", :null => false + t.integer "group_id", :null => false + t.integer "user_id", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.integer "notification_level", :default => 3, :null => false end create_table "users_projects", :force => true do |t| -- libgit2 0.21.2