PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/actionview/lib/action_view/routing_url_for.rb

https://gitlab.com/Rockyspade/rails
Ruby | 140 lines | 59 code | 9 blank | 72 comment | 2 complexity | 21f974424fde4a360b5db23e732cead1 MD5 | raw file
  1. require 'action_dispatch/routing/polymorphic_routes'
  2. module ActionView
  3. module RoutingUrlFor
  4. # Returns the URL for the set of +options+ provided. This takes the
  5. # same options as +url_for+ in Action Controller (see the
  6. # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
  7. # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
  8. # instead of the fully qualified URL like "http://example.com/controller/action".
  9. #
  10. # ==== Options
  11. # * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
  12. # * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
  13. # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
  14. # is currently not recommended since it breaks caching.
  15. # * <tt>:host</tt> - Overrides the default (current) host if provided.
  16. # * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
  17. # * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
  18. # * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
  19. #
  20. # ==== Relying on named routes
  21. #
  22. # Passing a record (like an Active Record) instead of a hash as the options parameter will
  23. # trigger the named route for that record. The lookup will happen on the name of the class. So passing a
  24. # Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
  25. # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
  26. #
  27. # ==== Implicit Controller Namespacing
  28. #
  29. # Controllers passed in using the +:controller+ option will retain their namespace unless it is an absolute one.
  30. #
  31. # ==== Examples
  32. # <%= url_for(action: 'index') %>
  33. # # => /blog/
  34. #
  35. # <%= url_for(action: 'find', controller: 'books') %>
  36. # # => /books/find
  37. #
  38. # <%= url_for(action: 'login', controller: 'members', only_path: false, protocol: 'https') %>
  39. # # => https://www.example.com/members/login/
  40. #
  41. # <%= url_for(action: 'play', anchor: 'player') %>
  42. # # => /messages/play/#player
  43. #
  44. # <%= url_for(action: 'jump', anchor: 'tax&ship') %>
  45. # # => /testing/jump/#tax&ship
  46. #
  47. # <%= url_for(Workshop.new) %>
  48. # # relies on Workshop answering a persisted? call (and in this case returning false)
  49. # # => /workshops
  50. #
  51. # <%= url_for(@workshop) %>
  52. # # calls @workshop.to_param which by default returns the id
  53. # # => /workshops/5
  54. #
  55. # # to_param can be re-defined in a model to provide different URL names:
  56. # # => /workshops/1-workshop-name
  57. #
  58. # <%= url_for("http://www.example.com") %>
  59. # # => http://www.example.com
  60. #
  61. # <%= url_for(:back) %>
  62. # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
  63. # # => http://www.example.com
  64. #
  65. # <%= url_for(:back) %>
  66. # # if request.env["HTTP_REFERER"] is not set or is blank
  67. # # => javascript:history.back()
  68. #
  69. # <%= url_for(action: 'index', controller: 'users') %>
  70. # # Assuming an "admin" namespace
  71. # # => /admin/users
  72. #
  73. # <%= url_for(action: 'index', controller: '/users') %>
  74. # # Specify absolute path with beginning slash
  75. # # => /users
  76. def url_for(options = nil)
  77. case options
  78. when String
  79. options
  80. when nil
  81. super(only_path: _generate_paths_by_default)
  82. when Hash
  83. options = options.symbolize_keys
  84. unless options.key?(:only_path)
  85. if options[:host].nil?
  86. options[:only_path] = _generate_paths_by_default
  87. else
  88. options[:only_path] = false
  89. end
  90. end
  91. super(options)
  92. when :back
  93. _back_url
  94. when Array
  95. if _generate_paths_by_default
  96. polymorphic_path(options, options.extract_options!)
  97. else
  98. polymorphic_url(options, options.extract_options!)
  99. end
  100. else
  101. method = _generate_paths_by_default ? :path : :url
  102. builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.send(method)
  103. case options
  104. when Symbol
  105. builder.handle_string_call(self, options)
  106. when Class
  107. builder.handle_class_call(self, options)
  108. else
  109. builder.handle_model_call(self, options)
  110. end
  111. end
  112. end
  113. def url_options #:nodoc:
  114. return super unless controller.respond_to?(:url_options)
  115. controller.url_options
  116. end
  117. def _routes_context #:nodoc:
  118. controller
  119. end
  120. protected :_routes_context
  121. def optimize_routes_generation? #:nodoc:
  122. controller.respond_to?(:optimize_routes_generation?, true) ?
  123. controller.optimize_routes_generation? : super
  124. end
  125. protected :optimize_routes_generation?
  126. private
  127. def _generate_paths_by_default
  128. true
  129. end
  130. end
  131. end