Browse Source

Adds Watcher.any_watched? to check if at least one object of a collection is watched.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11959 e93f8b46-1217-0410-a6f0-8f06a7374b81
tags/2.4.0
Jean-Philippe Lang 11 years ago
parent
commit
e0c7eb25a4
3 changed files with 31 additions and 1 deletions
  1. 1
    1
      app/helpers/watchers_helper.rb
  2. 13
    0
      app/models/watcher.rb
  3. 17
    0
      test/unit/watcher_test.rb

+ 1
- 1
app/helpers/watchers_helper.rb View File

return '' unless user && user.logged? return '' unless user && user.logged?
objects = Array.wrap(objects) objects = Array.wrap(objects)


watched = objects.any? {|object| object.watched_by?(user)}
watched = Watcher.any_watched?(objects, user)
css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ') css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ')
text = watched ? l(:button_unwatch) : l(:button_watch) text = watched ? l(:button_unwatch) : l(:button_watch)
url = watch_path( url = watch_path(

+ 13
- 0
app/models/watcher.rb View File

validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
validate :validate_user validate :validate_user


# Returns true if at least one object among objects is watched by user
def self.any_watched?(objects, user)
objects = objects.reject(&:new_record?)
if objects.any?
objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
return true
end
end
end
false
end

# Unwatch things that users are no longer allowed to view # Unwatch things that users are no longer allowed to view
def self.prune(options={}) def self.prune(options={})
if options.has_key?(:user) if options.has_key?(:user)

+ 17
- 0
test/unit/watcher_test.rb View File

assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)} assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
end end


def test_any_watched_should_return_false_if_no_object_is_watched
objects = (0..2).map {Issue.generate!}

assert_equal false, Watcher.any_watched?(objects, @user)
end

def test_any_watched_should_return_true_if_one_object_is_watched
objects = (0..2).map {Issue.generate!}
objects.last.add_watcher(@user)

assert_equal true, Watcher.any_watched?(objects, @user)
end

def test_any_watched_should_return_false_with_no_object
assert_equal false, Watcher.any_watched?([], @user)
end

def test_recipients def test_recipients
@issue.watchers.delete_all @issue.watchers.delete_all
@issue.reload @issue.reload

Loading…
Cancel
Save