Module: CanonicalRails::TagHelper

Defined in:
app/helpers/canonical_rails/tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#allowed_paramsObject



58
59
60
61
62
63
64
# File 'app/helpers/canonical_rails/tag_helper.rb', line 58

def allowed_params
  selected_params = params.select do |key, value|
    value.present? && CanonicalRails.sym_allowed_parameters.include?(key.to_sym)
  end

  selected_params.respond_to?(:to_unsafe_h) ? selected_params.to_unsafe_h : selected_params.to_h
end

#allowed_query_stringObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/canonical_rails/tag_helper.rb', line 66

def allowed_query_string
  # Rack 1.4.5 fails to handle params that are not strings
  # So if
  #     my_hash = { "a" => 1, "b" => 2}
  # Rack::Utils.build_nested_query(my_hash) would return
  #     "a&b"
  # Rack 1.4.5 did not have a test case for this scenario
  # https://github.com/rack/rack/blob/9939d40a5e23dcb058751d1029b794aa2f551900/test/spec_utils.rb#L222
  # Rack 1.6.0 has it
  # https://github.com/rack/rack/blob/65a7104b6b3e9ecd8f33c63a478ab9a33a103507/test/spec_utils.rb#L251
  parameters = allowed_params
  "?" + Rack::Utils.build_nested_query(convert_numeric_params(parameters)) if parameters.present?
end

#canonical_hostObject



30
31
32
# File 'app/helpers/canonical_rails/tag_helper.rb', line 30

def canonical_host
  CanonicalRails.host || request.host
end

#canonical_href(host = canonical_host, port = canonical_port, force_trailing_slash = nil) ⇒ Object



38
39
40
41
42
# File 'app/helpers/canonical_rails/tag_helper.rb', line 38

def canonical_href(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
  default_ports = { 'https://' => 443, 'http://' => 80 }
  port = port.present? && port.to_i != default_ports[canonical_protocol] ? ":#{port}" : ''
  raw "#{canonical_protocol}#{host}#{port}#{path_without_extension}#{trailing_slash_config(force_trailing_slash)}#{allowed_query_string}"
end

#canonical_path(force_trailing_slash = nil) ⇒ Object



44
45
46
# File 'app/helpers/canonical_rails/tag_helper.rb', line 44

def canonical_path(force_trailing_slash = nil)
  raw "#{path_without_extension}#{trailing_slash_config(force_trailing_slash)}#{allowed_query_string}"
end

#canonical_portObject



34
35
36
# File 'app/helpers/canonical_rails/tag_helper.rb', line 34

def canonical_port
  (CanonicalRails.port || request.port).to_i
end

#canonical_protocolObject



26
27
28
# File 'app/helpers/canonical_rails/tag_helper.rb', line 26

def canonical_protocol
  CanonicalRails.protocol || request.protocol
end

#canonical_tag(host = canonical_host, port = canonical_port, force_trailing_slash = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'app/helpers/canonical_rails/tag_helper.rb', line 48

def canonical_tag(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
  canonical_url = canonical_href(host, port, force_trailing_slash)
  capture do
    if CanonicalRails.opengraph_url
      concat tag(:meta, property: 'og:url', content: canonical_url)
    end
    concat tag(:link, href: canonical_url, rel: :canonical)
  end
end

#path_without_extensionObject



20
21
22
23
24
# File 'app/helpers/canonical_rails/tag_helper.rb', line 20

def path_without_extension
  return '' if request.path == '/'

  request.path.sub(/\.\w{3,4}$/, '')
end

#trailing_slash_config(force_trailing_slash = nil) ⇒ Object

Leave force_trailing_slash as nil to get the original behavior of trailing_slash_if_needed



8
9
10
11
12
13
14
# File 'app/helpers/canonical_rails/tag_helper.rb', line 8

def trailing_slash_config(force_trailing_slash = nil)
  if force_trailing_slash
    "/"
  elsif force_trailing_slash.nil?
    trailing_slash_if_needed
  end
end

#trailing_slash_if_neededObject



16
17
18
# File 'app/helpers/canonical_rails/tag_helper.rb', line 16

def trailing_slash_if_needed
  "/" if trailing_slash_needed?
end

#trailing_slash_needed?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'app/helpers/canonical_rails/tag_helper.rb', line 3

def trailing_slash_needed?
  request.params.key?('action') && CanonicalRails.sym_collection_actions.include?(request.params['action'].to_sym)
end