Class: CookieAlert::CookiesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/cookie_alert/cookies_controller.rb

Instance Method Summary collapse

Instance Method Details

Implement cookie acceptance when a visitor click the 'accept' button



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/cookie_alert/cookies_controller.rb', line 7

def cookie_accepted

  # Get the visitor's current page URL or, if nil?,  default to the application root. Resue block needed for bots
  visitor_current_url = begin
    cookies.signed[CookieAlert.config.cookie_name.to_sym].split(CookieAlert.config.cookie_value_text_separator)[1]
  rescue
    main_app.root_path
  end

  # Set the Cookie value to 'accepted'
  if CookieAlert.config.cookie_type == 'permanent'
 
    # Set a permanent cookie
    cookies.permanent.signed[CookieAlert.config.cookie_name.to_sym] = 'accepted'
 
  elsif CookieAlert.config.cookie_type == 'fixed_duration'
 
    # Set a fixed duration cookie
    cookies.permanent.signed[CookieAlert.config.cookie_name.to_sym] = { value: 'accepted', expires: CookieAlert.config.num_days_until_cookie_expires.days.from_now }
 
  else
 
    # Set a session cookie
    cookies.signed[CookieAlert.config.cookie_name.to_sym] = 'accepted'
 
  end
  
  # If the request is HTML then redirect the visitor back to their original page
  # If the request is javascript then render the javascript partial
  respond_to do |format|

    format.html { redirect_to(visitor_current_url) }
    format.js { render template: "cookie_alert/cookies/cookie_accepted" }

  end
  
end