From 3fc3c99437c2d19b50ce4a0d1ae070be65244551 Mon Sep 17 00:00:00 2001 From: Ignacio Carrera Date: Wed, 3 Jun 2009 01:45:49 -0300 Subject: [PATCH] Added ActionController::CookieJar.defaults Hash to affect the options 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). --- engines/adva_cms/lib/rails_ext.rb | 1 + .../action_controller/cookie_defaults.rb | 52 +++++++++++++++++++ .../public/javascripts/adva_cms/cookie.js | 15 ++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 engines/adva_cms/lib/rails_ext/action_controller/cookie_defaults.rb diff --git a/engines/adva_cms/lib/rails_ext.rb b/engines/adva_cms/lib/rails_ext.rb index 07fed8582..4ebe430c9 100644 --- a/engines/adva_cms/lib/rails_ext.rb +++ b/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' diff --git a/engines/adva_cms/lib/rails_ext/action_controller/cookie_defaults.rb b/engines/adva_cms/lib/rails_ext/action_controller/cookie_defaults.rb new file mode 100644 index 000000000..892e72c77 --- /dev/null +++ b/engines/adva_cms/lib/rails_ext/action_controller/cookie_defaults.rb @@ -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 diff --git a/engines/adva_cms/public/javascripts/adva_cms/cookie.js b/engines/adva_cms/public/javascripts/adva_cms/cookie.js index 4ff53472c..e47063fb3 100644 --- a/engines/adva_cms/public/javascripts/adva_cms/cookie.js +++ b/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) {