Module: FlashTruncation

Included in:
ApplicationController
Defined in:
app/controllers/concerns/flash_truncation.rb

Overview

Module FlashTruncation provides the truncate_flash method to automatically trim long flash messages to prevent them from overflowing the cookie

Author:

  • Genome Research Ltd.

Constant Summary collapse

STRING_OVERHEAD =

Encoding a json string results in a two-byte overhead for the “ either side. Taking this into account is strictly unnecessary, as we’ve already got a bit of overhead built in, but lets keep things as predictable as possible.

2

Instance Method Summary collapse

Instance Method Details

#max_flash_sizeObject

The maximum cookie size is 4096 bytes, however this is post-encryption, which increases the size. The value of 2048 was obtained by mapping the size of encrypted strings. In practice 2255 bytes was the largest size, but I rounded down to 2kb to provide a bit of overhead for array serialization, additional flash information to and allow for slight implementation changes.



41
42
43
# File 'app/controllers/concerns/flash_truncation.rb', line 41

def max_flash_size
  2048 - session.to_json.bytesize
end

#truncate_flash(message, max_size = max_flash_size) ⇒ Array, String

Truncates the flash message to avoid an ActionDispatch::Cookies::CookieOverflow. Maximum cookie size is checked against ActionDispatch::Cookies::MAX_COOKIE_SIZE which is 4096 bytes; however:

  • This is the size of the session cookie post encryption, which inflates the size

  • This cookie also needs to contain other data, such as the session_id, user_uuid and user_name

Parameters:

  • message (Array, String)

    The flash to truncate

  • max_size (Integer) (defaults to: max_flash_size)

    The maximum allowed flash size

Returns:

  • (Array, String)

    The truncated message



26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/concerns/flash_truncation.rb', line 26

def truncate_flash(message, max_size = max_flash_size)
  return message if message.to_json.bytesize <= max_size

  case message
  when String
    message.truncate(max_size - STRING_OVERHEAD)
  when Array
    truncate_flash_array(message, max_size)
  end
end

#truncate_flash_array(array, max_size = max_flash_size) ⇒ Array

Handles truncation of arrays passed to the flash. This is not intended to be used directly, instead use truncate_flash.

Parameters:

  • array (Array)

    The flash to truncate

  • max_size (Integer) (defaults to: max_flash_size)

    The maximum allowed flash size

Returns:

  • (Array)

    The truncated array

See Also:



54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/flash_truncation.rb', line 54

def truncate_flash_array(array, max_size = max_flash_size)
  array.each_with_object([]) do |message, messages|
    remaining = max_size - messages.to_json.bytesize
    return messages if remaining <= 0

    messages << truncate_flash(message, remaining)
  end
end