Class: Consently::Configuration
- Inherits:
-
Object
- Object
- Consently::Configuration
- 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
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#consent_required ⇒ Object
Whether this visitor has to be asked at all.
-
#consent_subject ⇒ Object
Who made the decision, for the consent log.
-
#consent_version ⇒ Object
Bump this whenever the policy changes: an older consent stops counting and the banner asks again.
-
#cookie_max_age ⇒ Object
Where the visitor's choice is kept.
-
#cookie_name ⇒ Object
Where the visitor's choice is kept.
-
#cookie_path ⇒ Object
Where the visitor's choice is kept.
-
#enabled ⇒ Object
true, false, or a callable taking the request - e.g.
-
#google_consent_mode ⇒ Object
Emits Google's consent mode v2 defaults (everything denied) before any Google tag, and updates them when the visitor chooses.
-
#log_consents ⇒ Object
Store a row per decision, as proof of consent.
-
#policy_url ⇒ Object
Where the cookie policy lives.
-
#reload_after_choice ⇒ Object
Reload the page once a choice is made.
-
#respect_do_not_track ⇒ Object
Honour the browser's Do Not Track header as a rejection.
-
#respect_global_privacy_control ⇒ Object
Global Privacy Control, which - unlike DNT - is a binding opt-out signal in California and Colorado.
-
#scope_resolver ⇒ Object
Which scope a request belongs to, e.g.
-
#stylesheet ⇒ Object
Whether consently_tags links the banner's stylesheet.
Instance Method Summary collapse
-
#category(name) ⇒ Object
A category of your own, shown in the preferences panel alongside the built-in ones.
- #default_scope ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #optional_categories ⇒ Object
-
#scope(name) {|scope| ... } ⇒ Object
Tags for one shop, host or whatever your scope_resolver returns.
-
#tag(key, **options) ⇒ Object
c.tag :google_analytics, id: "G-XXXX".
- #tags_for(scope_name = nil) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
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
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
71 72 73 |
# File 'lib/consently/configuration.rb', line 71 def categories @categories end |
#consent_required ⇒ Object
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. = ->(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 end |
#consent_subject ⇒ Object
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 end |
#consent_version ⇒ Object
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 end |
#cookie_max_age ⇒ Object
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 end |
#cookie_name ⇒ Object
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 end |
#cookie_path ⇒ Object
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 end |
#enabled ⇒ Object
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 |
#google_consent_mode ⇒ Object
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 end |
#log_consents ⇒ Object
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 end |
#policy_url ⇒ Object
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_choice ⇒ Object
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_track ⇒ Object
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_control ⇒ Object
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_resolver ⇒ Object
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 |
#stylesheet ⇒ Object
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_scope ⇒ Object
129 130 131 |
# File 'lib/consently/configuration.rb', line 129 def default_scope @default_scope ||= Scope.new end |
#optional_categories ⇒ Object
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
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, **) default_scope.tag(key, **) end |
#tags_for(scope_name = nil) ⇒ Object
123 124 125 126 127 |
# File 'lib/consently/configuration.rb', line 123 def (scope_name = nil) = default_scope..dup .merge!(@scopes[scope_name.to_s].) if scope_name && @scopes.key?(scope_name.to_s) .values end |