Merge remote-tracking branch 'schmidt/f/416-redo-watcher-links-unstable' into unstable

This commit is contained in:
Eric Davis 2011-05-27 10:42:53 -07:00
commit 46f66add1f
13 changed files with 152 additions and 47 deletions

View File

@ -76,23 +76,22 @@ private
def set_watcher(user, watching)
@watched.set_watcher(user, watching)
if params[:replace].present?
if params[:replace].is_a? Array
replace_ids = params[:replace]
else
replace_ids = [params[:replace]]
end
else
replace_ids = ['watcher']
end
respond_to do |format|
format.html { redirect_to :back }
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)
if params[:replace].present?
if params[:replace].is_a? Array
@replace_selectors = params[:replace]
else
@replace_selectors = params[:replace].split(',').map(&:strip)
end
else
@replace_selectors = ['#watcher']
end
@user = user
render :action => 'replace_selectors'
end
end
rescue ::ActionController::RedirectBackError

View File

@ -17,32 +17,41 @@
module WatchersHelper
# Valid options
# * :id - the element id
# * :replace - a string or array of element ids that will be
# replaced
# Deprecated method. Use watcher_link instead
#
# This method will be removed in ChiliProject 3.0 or later
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)
ActiveSupport::Deprecation.warn "The WatchersHelper#watcher_tag is deprecated and will be removed in ChiliProject 3.0. Please use WatchersHelper#watcher_link instead. Please also note the differences between the APIs.", caller
options[:id] ||= options[:replace] if options[:replace].is_a? String
options[:replace] = Array(options[:replace]).map { |id| "##{id}" }
watcher_link(object, user, options)
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] = html_options[:class].to_s + (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

View File

@ -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) %>
</div>
<div id="add-message" style="display:none;">

View File

@ -1,8 +1,7 @@
<div class="contextual">
<%= 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_link(@issue, User.current, {:class => 'watcher_link', :replace => ['#watchers', '.watcher_link']}) %>
<%= 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' %>

View File

@ -98,7 +98,7 @@
<div style="clear: both;"></div>
<%= render :partial => 'action_menu', :locals => {:replace_watcher => 'watcher2' } %>
<%= render :partial => 'action_menu' %>
<div style="clear: both;"></div>
<% if authorize_for('issues', 'edit') %>

View File

@ -2,7 +2,7 @@
link_to(h(@board.name), {:controller => 'boards', :action => 'show', :project_id => @project, :id => @board}) %>
<div class="contextual">
<%= watcher_tag(@topic, User.current) %>
<%= watcher_link(@topic, User.current) %>
<%= link_to_remote_if_authorized(l(:button_quote), { :url => {:action => 'quote', :id => @topic} }, :class => 'icon icon-comment') unless @topic.locked? %>
<%= link_to(l(:button_edit), {:action => 'edit', :id => @topic}, :class => 'icon icon-edit') if @message.editable_by?(User.current) %>
<%= link_to(l(:button_delete), {:action => 'destroy', :id => @topic}, :method => :post, :confirm => l(:text_are_you_sure), :class => 'icon icon-del') if @message.destroyable_by?(User.current) %>

View File

@ -1,5 +1,5 @@
<div class="contextual">
<%= watcher_tag(@news, User.current) %>
<%= watcher_link(@news, User.current) %>
<%= link_to(l(:button_edit),
edit_news_path(@news),
:class => 'icon icon-edit',

View File

@ -0,0 +1,22 @@
@replace_selectors.each do |selector|
next if selector.blank?
case selector
when '#watchers'
page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
else
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

View File

@ -1,5 +1,5 @@
<div class="contextual">
<%= watcher_tag(@wiki, User.current) %>
<%= watcher_link(@wiki, User.current) %>
</div>
<h2><%= l(:label_index_by_date) %></h2>

View File

@ -1,5 +1,5 @@
<div class="contextual">
<%= watcher_tag(@wiki, User.current) %>
<%= watcher_link(@wiki, User.current) %>
</div>
<h2><%= l(:label_index_by_title) %></h2>

View File

@ -1,7 +1,7 @@
<div class="contextual">
<% if @editable %>
<%= link_to_if_authorized(l(:button_edit), {:action => 'edit', :id => @page.title}, :class => 'icon icon-edit', :accesskey => accesskey(:edit)) if @content.version == @page.content.version %>
<%= watcher_tag(@page, User.current) %>
<%= watcher_link(@page, User.current) %>
<%= link_to_if_authorized(l(:button_lock), {:action => 'protect', :id => @page.title, :protected => 1}, :method => :post, :class => 'icon icon-lock') if !@page.protected? %>
<%= link_to_if_authorized(l(:button_unlock), {:action => 'protect', :id => @page.title, :protected => 0}, :method => :post, :class => 'icon icon-unlock') if @page.protected? %>
<%= link_to_if_authorized(l(:button_rename), {:action => 'rename', :id => @page.title}, :class => 'icon icon-move') if @content.version == @page.content.version %>

View File

@ -43,7 +43,8 @@ class WatchersControllerTest < ActionController::TestCase
assert_difference('Watcher.count') do
xhr :post, :watch, :object_type => 'issue', :object_id => '1'
assert_response :success
assert_select_rjs :replace_html, 'watcher'
assert @response.body.include? "$$(\"#watcher\").each"
assert @response.body.include? "value.replace"
end
assert Issue.find(1).watched_by?(User.find(3))
end
@ -60,10 +61,22 @@ class WatchersControllerTest < ActionController::TestCase
def test_watch_with_multiple_replacements
@request.session[:user_id] = 3
assert_difference('Watcher.count') do
xhr :post, :watch, :object_type => 'issue', :object_id => '1', :replace => ['watch_item_1','watch_item_2']
xhr :post, :watch, :object_type => 'issue', :object_id => '1', :replace => ['#watch_item_1','.watch_item_2']
assert_response :success
assert_select_rjs :replace_html, 'watch_item_1'
assert_select_rjs :replace_html, 'watch_item_2'
assert @response.body.include? "$$(\"#watch_item_1\").each"
assert @response.body.include? "$$(\".watch_item_2\").each"
assert @response.body.include? "value.replace"
end
end
def test_watch_with_watchers_special_logic
@request.session[:user_id] = 3
assert_difference('Watcher.count') do
xhr :post, :watch, :object_type => 'issue', :object_id => '1', :replace => ['#watchers', '.watcher']
assert_response :success
assert_select_rjs :replace_html, 'watchers'
assert @response.body.include? "$$(\".watcher\").each"
assert @response.body.include? "value.replace"
end
end
@ -72,7 +85,8 @@ class WatchersControllerTest < ActionController::TestCase
assert_difference('Watcher.count', -1) do
xhr :post, :unwatch, :object_type => 'issue', :object_id => '2'
assert_response :success
assert_select_rjs :replace_html, 'watcher'
assert @response.body.include? "$$(\"#watcher\").each"
assert @response.body.include? "value.replace"
end
assert !Issue.find(1).watched_by?(User.find(3))
end
@ -80,10 +94,23 @@ class WatchersControllerTest < ActionController::TestCase
def test_unwatch_with_multiple_replacements
@request.session[:user_id] = 3
assert_difference('Watcher.count', -1) do
xhr :post, :unwatch, :object_type => 'issue', :object_id => '2', :replace => ['watch_item_1', 'watch_item_2']
xhr :post, :unwatch, :object_type => 'issue', :object_id => '2', :replace => ['#watch_item_1', '.watch_item_2']
assert_response :success
assert_select_rjs :replace_html, 'watch_item_1'
assert_select_rjs :replace_html, 'watch_item_2'
assert @response.body.include? "$$(\"#watch_item_1\").each"
assert @response.body.include? "$$(\".watch_item_2\").each"
assert @response.body.include? "value.replace"
end
assert !Issue.find(1).watched_by?(User.find(3))
end
def test_unwatch_with_watchers_special_logic
@request.session[:user_id] = 3
assert_difference('Watcher.count', -1) do
xhr :post, :unwatch, :object_type => 'issue', :object_id => '2', :replace => ['#watchers', '.watcher']
assert_response :success
assert_select_rjs :replace_html, 'watchers'
assert @response.body.include? "$$(\".watcher\").each"
assert @response.body.include? "value.replace"
end
assert !Issue.find(1).watched_by?(User.find(3))
end

View File

@ -0,0 +1,49 @@
require File.expand_path('../../../test_helper', __FILE__)
class WatchersHelperTest < HelperTestCase
include WatchersHelper
# tested for backwards compatibility
context '#watcher_tag' do
setup do
# mocking watcher_link to make sure, that new API is properly called from
# the old one.
def self.watcher_link(*args)
@watcher_link_args = args
nil
end
# silencing deprecation warnings while testing the deprecated behavior
def self.watcher_tag(*args)
ActiveSupport::Deprecation.silence { super }
end
end
context 'without options' do
should "call watcher_link with object, user and {:id => 'watcher', :replace => '#watcher'}" do
watcher_tag(:object, :user)
assert_equal :object, @watcher_link_args.first
assert_equal :user, @watcher_link_args.second
assert_equal({:id => 'watcher', :replace => ['#watcher']}, @watcher_link_args.third)
end
end
context 'with replace, without id option' do
should "set id to replace value and prefix replace with a # to make it a valid css selectors" do
watcher_tag(:object, :user, :replace => 'abc')
assert_equal :object, @watcher_link_args.first
assert_equal :user, @watcher_link_args.second
assert_equal({:id => 'abc', :replace => ['#abc']}, @watcher_link_args.third)
end
end
context 'with all options' do
should "prefix all elements in replace with a # to make them valid css selectors" do
watcher_tag(:object, :user, :id => 'abc', :replace => ['abc', 'def'])
assert_equal :object, @watcher_link_args.first
assert_equal :user, @watcher_link_args.second
assert_equal({:id => 'abc', :replace => ['#abc', '#def']}, @watcher_link_args.third)
end
end
end
end