Class: Consently::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/consently/configuration.rb

Overview

Everything the host application sets in config/initializers/consently.rb.

Defined Under Namespace

Classes: Scope

Constant Summary collapse

DEFAULT_CATEGORIES =
%i[necessary analytics marketing].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/consently/configuration.rb', line 73

def initialize
  @cookie_name = "consently"
  @cookie_max_age = 60 * 60 * 24 * 180 # six months, the usual guidance
  @cookie_path = "/"
  @consent_version = 1
  @enabled = true
  @google_consent_mode = true
  @stylesheet = true
  @log_consents = false
  @respect_do_not_track = false
  @respect_global_privacy_control = true
  @consent_required = true
  @reload_after_choice = false
  @consent_subject = nil
  @policy_url = nil
  @scope_resolver = nil
  @categories = DEFAULT_CATEGORIES.dup
  @scopes = {}
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



71
72
73
# File 'lib/consently/configuration.rb', line 71

def categories
  @categories
end

Whether this visitor has to be asked at all. false means no banner and everything runs - the answer for traffic outside the EU when your legal advice says so:

c.consent_required = ->(request) { EU_COUNTRIES.include?(request.headers["CF-IPCountry"]) }

Careful: this switches tags on without asking, so it is opt-in.



48
49
50
# File 'lib/consently/configuration.rb', line 48

def consent_required
  @consent_required
end

Who made the decision, for the consent log. A callable taking the request; return anything that identifies the visitor in your own system (a global id, "User#42", an account number). Left nil the log stays anonymous, which is the right default for a public site.



61
62
63
# File 'lib/consently/configuration.rb', line 61

def consent_subject
  @consent_subject
end

Bump this whenever the policy changes: an older consent stops counting and the banner asks again.



12
13
14
# File 'lib/consently/configuration.rb', line 12

def consent_version
  @consent_version
end

Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.



8
9
10
# File 'lib/consently/configuration.rb', line 8

def cookie_max_age
  @cookie_max_age
end

Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.



8
9
10
# File 'lib/consently/configuration.rb', line 8

def cookie_name
  @cookie_name
end

Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.



8
9
10
# File 'lib/consently/configuration.rb', line 8

def cookie_path
  @cookie_path
end

#enabledObject

true, false, or a callable taking the request - e.g. c.enabled = ->(request) { Rails.env.production? }



16
17
18
# File 'lib/consently/configuration.rb', line 16

def enabled
  @enabled
end

Emits Google's consent mode v2 defaults (everything denied) before any Google tag, and updates them when the visitor chooses. Leave it on if you use any Google product.



21
22
23
# File 'lib/consently/configuration.rb', line 21

def google_consent_mode
  @google_consent_mode
end

#log_consentsObject

Store a row per decision, as proof of consent. Needs the engine mounted and the migration from rails g consently:consent_log.



25
26
27
# File 'lib/consently/configuration.rb', line 25

def log_consents
  @log_consents
end

#policy_urlObject

Where the cookie policy lives. A string, or a callable taking the view context - handy when the URL is locale dependent.



65
66
67
# File 'lib/consently/configuration.rb', line 65

def policy_url
  @policy_url
end

#reload_after_choiceObject

Reload the page once a choice is made. Off by default - releasing the blocked tags in place is the whole point, and a reload throws away whatever the visitor was doing. Turn it on when the page itself renders differently depending on consent (an embedded map, a video, a status list) and you would rather let the server decide again.



55
56
57
# File 'lib/consently/configuration.rb', line 55

def reload_after_choice
  @reload_after_choice
end

#respect_do_not_trackObject

Honour the browser's Do Not Track header as a rejection. Off by default: DNT is advisory and widely ignored, so treating it as a legal signal is your call, not the gem's.



34
35
36
# File 'lib/consently/configuration.rb', line 34

def respect_do_not_track
  @respect_do_not_track
end

#respect_global_privacy_controlObject

Global Privacy Control, which - unlike DNT - is a binding opt-out signal in California and Colorado. On by default, and a visitor who sends it is never shown the banner: their answer already arrived.



39
40
41
# File 'lib/consently/configuration.rb', line 39

def respect_global_privacy_control
  @respect_global_privacy_control
end

#scope_resolverObject

Which scope a request belongs to, e.g. ->(request) { request.host }. Nil means every request sees the default configuration.



69
70
71
# File 'lib/consently/configuration.rb', line 69

def scope_resolver
  @scope_resolver
end

#stylesheetObject

Whether consently_tags links the banner's stylesheet. Turn it off if you have taken the views over and styled them yourself.



29
30
31
# File 'lib/consently/configuration.rb', line 29

def stylesheet
  @stylesheet
end

Instance Method Details

#category(name) ⇒ Object

A category of your own, shown in the preferences panel alongside the built-in ones. Give it a name in your locale file.



95
96
97
98
99
# File 'lib/consently/configuration.rb', line 95

def category(name)
  name = name.to_sym
  @categories << name unless @categories.include?(name)
  name
end

#default_scopeObject



129
130
131
# File 'lib/consently/configuration.rb', line 129

def default_scope
  @default_scope ||= Scope.new
end

#optional_categoriesObject



101
102
103
# File 'lib/consently/configuration.rb', line 101

def optional_categories
  categories - [ Consent::NECESSARY ]
end

#scope(name) {|scope| ... } ⇒ Object

Tags for one shop, host or whatever your scope_resolver returns. Falls back to the tags declared outside any scope, and may override them by declaring the same provider again.

c.scope "trixbrix" do |s|
s.tag :google_analytics, id: "G-TRIX"
end

Yields:



117
118
119
120
121
# File 'lib/consently/configuration.rb', line 117

def scope(name)
  scope = (@scopes[name.to_s] ||= Scope.new)
  yield scope if block_given?
  scope
end

#tag(key, **options) ⇒ Object

c.tag :google_analytics, id: "G-XXXX"



106
107
108
# File 'lib/consently/configuration.rb', line 106

def tag(key, **options)
  default_scope.tag(key, **options)
end

#tags_for(scope_name = nil) ⇒ Object



123
124
125
126
127
# File 'lib/consently/configuration.rb', line 123

def tags_for(scope_name = nil)
  tags = default_scope.tags.dup
  tags.merge!(@scopes[scope_name.to_s].tags) if scope_name && @scopes.key?(scope_name.to_s)
  tags.values
end