Class: Compass::Breadcrumb::Strategies::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/compass/breadcrumb/strategies/cookie.rb

Overview

Stores the breadcrumb trail in a Rails signed cookie.

The trail is JSON-encoded inside a signed cookie so it cannot be tampered with client-side — the stored URLs are rendered as link hrefs and must be trusted.

This is the default strategy. It is stateless and thread-safe: it holds only its constructor options; all per-request data lives on context.request.

I.e.:

config.breadcrumb.strategy = Compass::Breadcrumb::Strategies::Cookie.new(
domain: ".example.com"
)

Constant Summary collapse

DEFAULT_NAME =
"compass_breadcrumbs".freeze
DEFAULT_MAX_ENTRIES =
10

Instance Method Summary collapse

Constructor Details

#initialize(domain: nil, name: DEFAULT_NAME, max_entries: DEFAULT_MAX_ENTRIES) ⇒ Cookie

Returns a new instance of Cookie.

Parameters:

  • domain (String, nil) (defaults to: nil)

    cookie domain. When set, the breadcrumb cookie is shared across that host scope; when omitted, Rails cookie jar defaults apply.

  • name (String) (defaults to: DEFAULT_NAME)

    cookie name.

  • max_entries (Integer) (defaults to: DEFAULT_MAX_ENTRIES)

    maximum crumbs persisted in the cookie.



31
32
33
34
35
# File 'lib/compass/breadcrumb/strategies/cookie.rb', line 31

def initialize(domain: nil, name: DEFAULT_NAME, max_entries: DEFAULT_MAX_ENTRIES)
  @domain = domain
  @name = name
  @max_entries = max_entries
end

Instance Method Details

#for(context:, max: DEFAULT_MAX_ENTRIES) ⇒ Array<Hash>

Reads the stored trail.

Parameters:

  • context (Compass::Context, nil)

    the request-scoped context.

  • max (Integer) (defaults to: DEFAULT_MAX_ENTRIES)

    maximum number of crumbs to return.

Returns:

  • (Array<Hash>)

    up to max crumbs, newest first; [] when there is no request or the cookie is missing/invalid.



43
44
45
46
47
48
49
50
51
# File 'lib/compass/breadcrumb/strategies/cookie.rb', line 43

def for(context:, max: DEFAULT_MAX_ENTRIES)
  request = context&.request
  return [] unless request

  read(request).first(max)
rescue => e
  Compass.logger&.error "Breadcrumb cookie read error: #{e.message}"
  []
end

#push(context:, url:, label:) ⇒ nil

Prepends a crumb, deduping by label and capping to max_entries.

No-ops when there is no request or the label is blank.

Parameters:

  • context (Compass::Context, nil)

    the request-scoped context.

  • url (String)

    the crumb URL.

  • label (String)

    the crumb label.

Returns:

  • (nil)


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/compass/breadcrumb/strategies/cookie.rb', line 61

def push(context:, url:, label:)
  request = context&.request
  return if request.nil? || label.blank?

  trail = read(request).reject { |crumb| crumb[:label] == label }
  trail.unshift(url: url, label: label)

  write(request, trail.first(@max_entries))
  nil
rescue => e
  Compass.logger&.error "Breadcrumb cookie write error: #{e.message}"
  nil
end