Module: CanonicalURL::ControllerExtensions

Included in:
ApplicationController
Defined in:
lib/canonical_url.rb

Constant Summary collapse

ALLOWED_CANONICAL_PARAMS =
%w[page]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



28
29
30
# File 'lib/canonical_url.rb', line 28

def self.included(base)
  base.helper_method :default_canonical
end

Instance Method Details

#canonical_url(url_for_options = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/canonical_url.rb', line 7

def canonical_url(url_for_options = {})
  case url_for_options
  when Hash
    @canonical_url = url_for(url_for_options)
  else
    @canonical_url = url_for_options
  end
end

#default_canonicalObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/canonical_url.rb', line 16

def default_canonical
  @default_canonical ||=
    begin
      canonical = +"#{Discourse.base_url_no_prefix}#{request.path}"
      allowed_params = params.select { |key| ALLOWED_CANONICAL_PARAMS.include?(key) }
      if allowed_params.present?
        canonical << "?#{allowed_params.keys.zip(allowed_params.values).map { |key, value| "#{key}=#{value}" }.join("&")}"
      end
      canonical
    end
end