Module: Nitro::URLHelper
- Included in:
- Caching::Proxy
- Defined in:
- lib/nitro/helper/url.rb
Instance Method Summary collapse
-
#encode_absolute_url(*args) ⇒ Object
(also: #RA)
Just like encode_url, but generates an absolute url instead.
-
#encode_url(*args) ⇒ Object
(also: #R)
Encode controller, action, params into a valid url.
Instance Method Details
#encode_absolute_url(*args) ⇒ Object Also known as: RA
Just like encode_url, but generates an absolute url instead.
97 98 99 |
# File 'lib/nitro/helper/url.rb', line 97 def encode_absolute_url(*args) return "#{request.host_url}#{encode_url(*args)}" end |
#encode_url(*args) ⇒ Object Also known as: R
Encode controller, action, params into a valid url. Automatically respects nice urls and routing.
Handles parameters either as a hash or as an array. Use the array method to pass parameters to ‘nice’ actions.
Pass Controller, action, and (param_name, param_value) pairs.
Examples
encode_url ForaController, :post, :title, ‘Hello’, :body, ‘World’ encode_url :post, :title, ‘Hello’, :body, ‘World’ # => implies controller == self encode_url :kick, :oid, 4 encode_url article # => article.to_href
Alternatively you can pass options with a hash:
encode_url :controller => ForaController, :action => :delete, :params => { :title => ‘Hello’ } encode_url :action => :delete – FIXME: better implementation? optimize this? TODO: move elsewhere. ++
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/nitro/helper/url.rb', line 30 def encode_url(*args) f = args.first # A standard url as string, return as is. if f.is_a? String return f end # If the passed param is an object that responds to :to_href # returns the url to this object. if f.respond_to? :to_href return args.first.to_href end if f.is_a? Symbol # no controller passed, imply controller == self! args.unshift(self.class) end # Try to encode using the router. if url = Nitro.server.dispatcher.encode_route(*args) return url end # No routing rule, manual encoding. controller = args.shift action = args.shift.to_sym if action == :index url = "#{controller.mount_path}" else mount_path = controller.mount_path mount_path = nil if mount_path == '/' url = "#{mount_path}/#{action}" end unless args.empty? if controller.respond_to_action_or_template? action param_count = controller.instance_method(action).arity if param_count != 0 param_count.times do args.shift # name url << "/#{CGI.escape(args.shift.to_s)}" end end end unless args.empty? url << '?' params = [] (args.size / 2).times do params << "#{args.shift}=#{args.shift}" end url << params.join(';') end end return url end |