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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/flash_truncation.rb', line 41

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
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
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/concerns/flash_truncation.rb', line 26

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  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
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/concerns/flash_truncation.rb', line 54

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end