cirandas.net

ref: master

app/helpers/url_helper.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
module UrlHelper

  mattr_accessor :controller_path_class
  self.controller_path_class = {}

  def url_for options = {}
    return super unless options.is_a? Hash
    # for action mailer
    return super unless respond_to? :params and respond_to? :controller_path

    # Keep profile parameter when not using a custom domain:
    # this is necessary as :profile parameter is optional in config/routes.rb;
    # delete it if using a custom domain
    # This overides the default Rails' behaviour that always recall
    # the request params (see #url_options below)
    host = options[:host]
    if host.blank? or host == environment.default_hostname
      path = options[:controller].to_s.gsub %r{^/}, '' if options[:controller]
      path ||= self.controller_path
      controller = UrlHelper.controller_path_class[path] ||= "#{path}_controller".camelize.constantize
      profile_needed = controller.profile_needed if controller.respond_to? :profile_needed, true
      if profile_needed and options[:profile].blank? and params[:profile].present?
        options[:profile] = params[:profile]
      end
    else
      options.delete :profile
    end

    super options
  end

  def default_url_options options={}
    options[:protocol] ||= '//'

    options[:override_user] = params[:override_user] if params[:override_user].present?

    # Only use profile's custom domains for the profiles and the account controllers.
    # This avoids redirects and multiple URLs for one specific resource
    if controller_path = options[:controller] || self.class.controller_path
      controller = (UrlHelper.controller_path_class[controller_path] ||= "#{controller_path}_controller".camelize.constantize rescue nil)
      profile_needed = controller.profile_needed rescue false
      if controller and not profile_needed and not controller == AccountController
        options.merge! :host => environment.default_hostname, :only_path => false
      end
    end

    options
  end

  # the url_for above put or delete the :profile parameter as needed
  # :profile in _path_segments would always add it
  def url_options
    @_url_options_without_profile ||= begin
      # fix rails exception
      opts = super rescue {}
      opts[:_recall].delete :profile if opts[:_recall]
      opts
    end
  end

  def back_url
    'javascript:history.back()'
  end

  def default_url_options
    options = {}

    options[:override_user] = params[:override_user] if params[:override_user].present?

    options
  end

end