Skip to content
This repository has been archived by the owner on Oct 4, 2018. It is now read-only.

Commit

Permalink
Added ActionController::CookieJar.defaults Hash to affect the options…
Browse files Browse the repository at this point in the history
… of every created cookie.

Useful for using the javascript flash message and uname/uid (which are stored in cookies) in
a multi site setting with multiple subdomains (in case you need multiple sites to share the
session, etc).

Also modified Cookie js object to be able to use those same cookies from the client side
(otherwise the flash cookie would never get erased).
  • Loading branch information
nachokb committed Jun 3, 2009
1 parent 5bca4a0 commit 3fc3c99
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions engines/adva_cms/lib/rails_ext.rb
@@ -1,3 +1,4 @@
require 'rails_ext/action_controller/cookie_defaults'
require 'rails_ext/action_controller/cacheable_flash'
require 'rails_ext/action_controller/default_params'
require 'rails_ext/action_controller/event_helper'
Expand Down
@@ -0,0 +1,52 @@
# Makes cookies hash assume default domain/path. Add "defaults" hash to
# ActionController::CookieJar.
#
# Put this into an initializer:
# ActionController::CookieJar.defaults[:domain] = ".example.com"
#
# then this in some controller:
# cookies['uname'] = "foo"
# is equivalent to:
# cookies['uname'] = { :value = "foo", :domain => ".example.com" }
#
# FIXME: This shouldn't be necessary. Don't forget to put
# ActionController::Base.session = {
# ...
# }.reverse_merge! ActionController::CookieJar.defaults
#
# in your session_store initializer, and
# Cookie.defaultDomain = ".domain.com"
#
# in javascripts/application.js if you are using Javascript with cookies, as Adva does by default; FIXME: neither should this be necessary

ActionController::CookieJar.class_eval do
cattr_accessor :defaults
self.defaults = { :path => '/' }
end

module Adva
module CookieJarDefaults
# TODO use include_into (howto alias_method_chain :[]=???)
def self.included(base)
base.class_eval do
include InstanceMethods
alias_method :set_without_defaults, :[]=
alias_method :[]=, :set_with_defaults
end
end

module InstanceMethods
def set_with_defaults(key, options)
if options.is_a?(Hash)
options.symbolize_keys!
else
options = { :value => options }
end

options.reverse_merge! ActionController::CookieJar.defaults
set_without_defaults(key.to_s, options) # super :(
end
end
end
end
ActionController::CookieJar.send :include, Adva::CookieJarDefaults
15 changes: 12 additions & 3 deletions engines/adva_cms/public/javascripts/adva_cms/cookie.js
@@ -1,13 +1,22 @@
// From http://wiki.script.aculo.us/scriptaculous/show/Cookie
var Cookie = {
set: function(name, value, daysToExpire) {
set: function(name, value, daysToExpire, domain) {
var expire = '';
if(!daysToExpire) daysToExpire = 365;
var d = new Date();
d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
expire = 'expires=' + d.toGMTString();
var path = "path=/"
var cookieValue = escape(name) + '=' + escape(value || '') + '; ' + path + '; ' + expire + ';';
var path = "path=/";
if (domain === undefined) {
if (Cookie.defaultDomain === undefined) {
domain = "";
} else {
domain = "domain=" + Cookie.defaultDomain +"; ";
}
} else {
domain = "domain=" + domain + "; ";
}
var cookieValue = escape(name) + '=' + escape(value || '') + '; ' + path + '; ' + expire + ';' + domain;
return document.cookie = cookieValue;
},
get: function(name) {
Expand Down

0 comments on commit 3fc3c99

Please sign in to comment.