This repository has been archived by the owner on Oct 4, 2018. It is now read-only.
forked from svenfuchs/adva_cms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
Showing
3 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
engines/adva_cms/lib/rails_ext/action_controller/cookie_defaults.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters