@@ -34,25 +34,11 module ApplicationHelper | |||
|
34 | 34 | # Display a link if user is authorized |
|
35 | 35 | # |
|
36 | 36 | # @param [String] name Anchor text (passed to link_to) |
|
37 | # @param [Hash, String] options Hash params or url for the link target (passed to link_to). | |
|
38 | # This will checked by authorize_for to see if the user is authorized | |
|
37 | # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized | |
|
39 | 38 | # @param [optional, Hash] html_options Options passed to link_to |
|
40 | 39 | # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to |
|
41 | 40 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
42 | if options.is_a?(String) | |
|
43 | begin | |
|
44 | route = ActionController::Routing::Routes.recognize_path(options.gsub(/\?.*/,''), :method => options[:method] || :get) | |
|
45 | link_controller = route[:controller] | |
|
46 | link_action = route[:action] | |
|
47 | rescue ActionController::RoutingError # Parse failed, not a route | |
|
48 | link_controller, link_action = nil, nil | |
|
49 | end | |
|
50 | else | |
|
51 | link_controller = options[:controller] || params[:controller] | |
|
52 | link_action = options[:action] | |
|
53 | end | |
|
54 | ||
|
55 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(link_controller, link_action) | |
|
41 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) | |
|
56 | 42 | end |
|
57 | 43 | |
|
58 | 44 | # Display a link to remote if user is authorized |
@@ -49,15 +49,6 class ApplicationHelperTest < ActionView::TestCase | |||
|
49 | 49 | assert_match /href/, response |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | should "allow using the url for the target link" do | |
|
53 | User.current = User.find_by_login('admin') | |
|
54 | ||
|
55 | @project = Issue.first.project # Used by helper | |
|
56 | response = link_to_if_authorized("By url", | |
|
57 | new_issue_move_path(:id => Issue.first.id)) | |
|
58 | assert_match /href/, response | |
|
59 | end | |
|
60 | ||
|
61 | 52 | end |
|
62 | 53 | |
|
63 | 54 | def test_auto_links |
General Comments 0
You need to be logged in to leave comments.
Login now