From 9cb4feefceaa39a2fb5a5ba096797c593ddc7c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Scha=CC=88fer?= Date: Sat, 14 May 2011 17:53:29 +0200 Subject: [PATCH 1/9] Update the watcher list on click on "watch". #311 Based on a patch contributed by Price M. --- app/controllers/watchers_controller.rb | 7 ++++++- app/views/issues/_action_menu.rhtml | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index ad8d8be2..11699c35 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -90,7 +90,12 @@ private format.js do render(:update) do |page| replace_ids.each do |replace_id| - page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids) + case replace_id + when 'watchers' + page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} + else + page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids) + end end end end diff --git a/app/views/issues/_action_menu.rhtml b/app/views/issues/_action_menu.rhtml index 3f4d89e2..0ec82ecd 100644 --- a/app/views/issues/_action_menu.rhtml +++ b/app/views/issues/_action_menu.rhtml @@ -2,7 +2,7 @@ <%= link_to_if_authorized(l(:button_update), {:controller => 'issues', :action => 'edit', :id => @issue }, :onclick => 'showAndScrollTo("update", "notes"); return false;', :class => 'icon icon-edit', :accesskey => accesskey(:edit)) %> <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'new', :issue_id => @issue}, :class => 'icon icon-time-add' %> <% replace_watcher ||= 'watcher' %> -<%= watcher_tag(@issue, User.current, {:id => replace_watcher, :replace => ['watcher','watcher2']}) %> +<%= watcher_tag(@issue, User.current, {:id => replace_watcher, :replace => ['watchers','watcher','watcher2']}) %> <%= link_to_if_authorized l(:button_duplicate), {:controller => 'issues', :action => 'new', :project_id => @project, :copy_from => @issue }, :class => 'icon icon-duplicate' %> <%= link_to_if_authorized l(:button_copy), {:controller => 'issue_moves', :action => 'new', :id => @issue, :copy_options => {:copy => 't'}}, :class => 'icon icon-copy' %> <%= link_to_if_authorized l(:button_move), {:controller => 'issue_moves', :action => 'new', :id => @issue}, :class => 'icon icon-move' %> From 508b23cf41f345d82ab76eb0e66375fc10b2d1bd Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 17 May 2011 18:15:34 +0200 Subject: [PATCH 2/9] [#416] Watcher links work using of css selectors instead of ids --- app/controllers/watchers_controller.rb | 36 ++++++++++++++++++++------ app/helpers/watchers_helper.rb | 36 ++++++++++++-------------- app/views/boards/show.rhtml | 2 +- app/views/issues/_action_menu.rhtml | 3 +-- app/views/messages/show.rhtml | 2 +- app/views/wiki/date_index.html.erb | 2 +- app/views/wiki/index.html.erb | 2 +- app/views/wiki/show.rhtml | 2 +- 8 files changed, 50 insertions(+), 35 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 11699c35..a7a9abfd 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -78,23 +78,43 @@ private @watched.set_watcher(user, watching) if params[:replace].present? if params[:replace].is_a? Array - replace_ids = params[:replace] + replace_selectors = params[:replace] else - replace_ids = [params[:replace]] + replace_selectors = params[:replace].split(',').map(&:strip) end else - replace_ids = ['watcher'] + replace_selectors = ['#watcher'] end + + watcher_partial = lambda do + render_to_string(:partial => 'watchers/watchers', :locals => {:watched => @watched}) + end + respond_to do |format| format.html { redirect_to :back } format.js do render(:update) do |page| - replace_ids.each do |replace_id| - case replace_id - when 'watchers' - page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} + replace_selectors.each do |selector| + next if selector.blank? + + case selector + when '#watchers' + page.select('#watchers').each do |node| + node.update watcher_partial.call + end else - page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids) + page.select(selector).each do |node| + options = {:replace => replace_selectors} + + last_selector = selector.split(' ').last + if last_selector.starts_with? '.' + options[:class] = last_selector[1..-1] + elsif last_selector.starts_with? '#' + options[:id] = last_selector[1..-1] + end + + node.replace watcher_link(@watched, user, options) + end end end end diff --git a/app/helpers/watchers_helper.rb b/app/helpers/watchers_helper.rb index 2695ce9f..b336233d 100644 --- a/app/helpers/watchers_helper.rb +++ b/app/helpers/watchers_helper.rb @@ -17,32 +17,28 @@ module WatchersHelper - # Valid options - # * :id - the element id - # * :replace - a string or array of element ids that will be - # replaced - def watcher_tag(object, user, options={:replace => 'watcher'}) - id = options[:id] - id ||= options[:replace] if options[:replace].is_a? String - content_tag("span", watcher_link(object, user, options), :id => id) - end - - # Valid options - # * :replace - a string or array of element ids that will be - # replaced - def watcher_link(object, user, options={:replace => 'watcher'}) + # Create a link to watch/unwatch object + # + # * :replace - a string or array of strings with css selectors that will be updated, whenever the watcher status is changed + def watcher_link(object, user, options = {:replace => '.watcher_link', :class => 'watcher_link'}) + options = options.with_indifferent_access + raise ArgumentError, 'Missing :replace option in options hash' if options['replace'].blank? + return '' unless user && user.logged? && object.respond_to?('watched_by?') + watched = object.watched_by?(user) url = {:controller => 'watchers', :action => (watched ? 'unwatch' : 'watch'), :object_type => object.class.to_s.underscore, :object_id => object.id, - :replace => options[:replace]} - link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)), - {:url => url}, - :href => url_for(url), - :class => (watched ? 'icon icon-fav' : 'icon icon-fav-off')) - + :replace => options.delete('replace')} + + url_options = {:url => url} + + html_options = options.merge(:href => url_for(url)) + html_options[:class] += watched ? ' icon icon-fav' : ' icon icon-fav-off' + + link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)), url_options, html_options) end # Returns a comma separated list of users watching the given object diff --git a/app/views/boards/show.rhtml b/app/views/boards/show.rhtml index c4a08b29..595038e8 100644 --- a/app/views/boards/show.rhtml +++ b/app/views/boards/show.rhtml @@ -5,7 +5,7 @@ {:controller => 'messages', :action => 'new', :board_id => @board}, :class => 'icon icon-add', :onclick => 'Element.show("add-message"); Form.Element.focus("message_subject"); return false;' %> -<%= watcher_tag(@board, User.current) %> + <%= watcher_link(@board, User.current) %>