Module: ZhuleiCanonicalRails::TagHelper

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

Instance Method Summary collapse

Instance Method Details

#canonical_hostObject



19
20
21
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 19

def canonical_host
  ZhuleiCanonicalRails.host || request.host
end

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



27
28
29
30
31
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 27

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

#canonical_pathObject



33
34
35
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 33

def canonical_path
  raw "#{path_without_html_extension}#{trailing_slash_if_needed}#{whitelisted_query_string}"
end

#canonical_portObject



23
24
25
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 23

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

#canonical_protocolObject



15
16
17
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 15

def canonical_protocol
  ZhuleiCanonicalRails.protocol || request.protocol
end

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



37
38
39
40
41
42
43
44
45
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 37

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

#path_without_html_extensionObject



11
12
13
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 11

def path_without_html_extension
  request.path.sub(/\.html$/, '')
end

#trailing_slash_if_neededObject



7
8
9
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 7

def trailing_slash_if_needed
  "/" if trailing_slash_needed? && request.path != '/'
end

#trailing_slash_needed?Boolean

Returns:

  • (Boolean)


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

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

#whitelisted_paramsObject



47
48
49
50
51
52
53
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 47

def whitelisted_params
  selected_params = params.select do |key, value|
    value.present? && ZhuleiCanonicalRails.sym_whitelisted_parameters.include?(key.to_sym)
  end

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

#whitelisted_query_stringObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/zhulei_canonical_rails/tag_helper.rb', line 55

def whitelisted_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

  wl_params = whitelisted_params

  "?" + Rack::Utils.build_nested_query(convert_numeric_params(wl_params)) if wl_params.present?
end