Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Created January 7, 2009 23:44
Show Gist options
  • Select an option

  • Save jbarnette/44506 to your computer and use it in GitHub Desktop.

Select an option

Save jbarnette/44506 to your computer and use it in GitHub Desktop.

Revisions

  1. jbarnette revised this gist Sep 25, 2009. 2 changed files with 126 additions and 124 deletions.
    60 changes: 31 additions & 29 deletions application.rb → application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,32 @@
    class ApplicationController < ActionController::Base
    before_filter :load_skin
    before_filter :require_user
    before_filter :require_matching_skin
    before_filter :require_admin

    def load_skin
    session.skin = Skin.for_request(request)
    end

    def require_user
    unless session.authenticated?
    session.bookmark(request)
    redirect_to(new_session_path)
    end
    end
    end

    def require_matching_skin
    unless session.skin.subdomain?(request.subdomains.first)
    return redirect_to(:host => session.skin.host)
    end
    end

    def require_admin
    unless session.admin?
    redirect_to(new_session_path)
    end
    end
    class ApplicationController < ActionController::Base
    include Intercession

    before_filter :load_skin
    before_filter :require_user
    before_filter :require_matching_skin
    before_filter :require_admin

    def load_skin
    session.skin = Skin.for_request(request)
    end

    def require_user
    unless session.authenticated?
    session.bookmark(request)
    redirect_to(new_session_path)
    end
    end
    end

    def require_matching_skin
    unless session.skin.subdomain?(request.subdomains.first)
    return redirect_to(:host => session.skin.host)
    end
    end

    def require_admin
    unless session.admin?
    redirect_to(new_session_path)
    end
    end
    end
    190 changes: 95 additions & 95 deletions session.rb
    Original file line number Diff line number Diff line change
    @@ -1,96 +1,96 @@
    module Transient

    # This module gets mixed in to the Rails session, and lets us
    # treat the session more like a real object. Note that not all attributes
    # of the session are necessarily session-scoped: some, like skin, are
    # set by before_filters on every request. See ApplicationController for
    # those. Note that while most things this module is mixed in to are
    # Hashlike, they vary in capabilitites. Safest to assume that the index[]
    # op is the only thing that's available.

    module Session
    attr_accessor :skin

    # Removes all user-related data from the session, making it safe to be
    # reused on logout.

    def sanitize
    self.user = nil
    end

    # When called with a destination, (example: session.bookmark(request))
    # stores the bookmarked destination in the session for later use. When
    # called as an accessor, (example: redirect_to session.bookmark) returns
    # and clears any bookmarked destination. Knows how to deal with requests,
    # strings, and url_for-style hashes.

    def bookmark(dest=nil)
    unless dest
    bookmark = self[:bookmark]
    self[:bookmark] = nil
    return bookmark
    end

    dest = dest.request_uri if dest.respond_to?(:request_uri)
    self[:bookmark] = dest
    end

    # Does this session currently have a bookmark?

    def bookmarked?
    self[:bookmark]
    end

    # Returns the authenticated user for this session, or nil.

    def user
    @user ||= User.find(self[:user_id]) if self[:user_id]
    end

    # Sets the authenticated user for this session. Set to nil to clear the
    # session's user, though you're probably better off calling sanitize.

    def user=(user)
    unless user
    self[:user_id] = @user = nil
    return
    end

    self[:user_id] = user.id
    @user = user
    end

    # Acts as a simple track clipboard. track IDs get persisted in the session
    # down in before_save if necessary.

    def tracks
    @tracks ||= Track.find(:all,
    :conditions => ["id in (?)", self[:tracks] || []], :order => :title)
    end

    # This session doesn't have an authenticated user, right?

    def anonymous?
    not user
    end

    # This session has an authenticated user, right?

    def authenticated?
    not anonymous?
    end

    # Is there a logged-in, administrative user?

    def admin?
    authenticated? && user.admin?
    end

    # Called by an after_filter in the application controller. A good
    # opportunity to turn heavy lists of stuff into IDs, etc.

    def before_save
    self[:tracks] = @tracks.collect(&:id) if @tracks
    end
    end
    module Transient

    # This module gets mixed in to the Rails session, and lets us
    # treat the session more like a real object. Note that not all attributes
    # of the session are necessarily session-scoped: some, like skin, are
    # set by before_filters on every request. See ApplicationController for
    # those. Note that while most things this module is mixed in to are
    # Hashlike, they vary in capabilitites. Safest to assume that the index[]
    # op is the only thing that's available.

    module Session
    attr_accessor :skin

    # Removes all user-related data from the session, making it safe to be
    # reused on logout.

    def sanitize
    self.user = nil
    end

    # When called with a destination, (example: session.bookmark(request))
    # stores the bookmarked destination in the session for later use. When
    # called as an accessor, (example: redirect_to session.bookmark) returns
    # and clears any bookmarked destination. Knows how to deal with requests,
    # strings, and url_for-style hashes.

    def bookmark(dest=nil)
    unless dest
    bookmark = self[:bookmark]
    self[:bookmark] = nil
    return bookmark
    end

    dest = dest.request_uri if dest.respond_to?(:request_uri)
    self[:bookmark] = dest
    end

    # Does this session currently have a bookmark?

    def bookmarked?
    self[:bookmark]
    end

    # Returns the authenticated user for this session, or nil.

    def user
    @user ||= User.find(self[:user_id]) if self[:user_id]
    end

    # Sets the authenticated user for this session. Set to nil to clear the
    # session's user, though you're probably better off calling sanitize.

    def user=(user)
    unless user
    self[:user_id] = @user = nil
    return
    end

    self[:user_id] = user.id
    @user = user
    end

    # Acts as a simple track clipboard. track IDs get persisted in the session
    # down in before_save if necessary.

    def tracks
    @tracks ||= Track.find(:all,
    :conditions => ["id in (?)", self[:tracks] || []], :order => :title)
    end

    # This session doesn't have an authenticated user, right?

    def anonymous?
    not user
    end

    # This session has an authenticated user, right?

    def authenticated?
    not anonymous?
    end

    # Is there a logged-in, administrative user?

    def admin?
    authenticated? && user.admin?
    end

    # Called by an after_filter in the application controller. A good
    # opportunity to turn heavy lists of stuff into IDs, etc.

    def before_save
    self[:tracks] = @tracks.collect(&:id) if @tracks
    end
    end
    end
  2. jbarnette created this gist Jan 7, 2009.
    30 changes: 30 additions & 0 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    class ApplicationController < ActionController::Base
    before_filter :load_skin
    before_filter :require_user
    before_filter :require_matching_skin
    before_filter :require_admin

    def load_skin
    session.skin = Skin.for_request(request)
    end

    def require_user
    unless session.authenticated?
    session.bookmark(request)
    redirect_to(new_session_path)
    end
    end
    end

    def require_matching_skin
    unless session.skin.subdomain?(request.subdomains.first)
    return redirect_to(:host => session.skin.host)
    end
    end

    def require_admin
    unless session.admin?
    redirect_to(new_session_path)
    end
    end
    end
    96 changes: 96 additions & 0 deletions session.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    module Transient

    # This module gets mixed in to the Rails session, and lets us
    # treat the session more like a real object. Note that not all attributes
    # of the session are necessarily session-scoped: some, like skin, are
    # set by before_filters on every request. See ApplicationController for
    # those. Note that while most things this module is mixed in to are
    # Hashlike, they vary in capabilitites. Safest to assume that the index[]
    # op is the only thing that's available.

    module Session
    attr_accessor :skin

    # Removes all user-related data from the session, making it safe to be
    # reused on logout.

    def sanitize
    self.user = nil
    end

    # When called with a destination, (example: session.bookmark(request))
    # stores the bookmarked destination in the session for later use. When
    # called as an accessor, (example: redirect_to session.bookmark) returns
    # and clears any bookmarked destination. Knows how to deal with requests,
    # strings, and url_for-style hashes.

    def bookmark(dest=nil)
    unless dest
    bookmark = self[:bookmark]
    self[:bookmark] = nil
    return bookmark
    end

    dest = dest.request_uri if dest.respond_to?(:request_uri)
    self[:bookmark] = dest
    end

    # Does this session currently have a bookmark?

    def bookmarked?
    self[:bookmark]
    end

    # Returns the authenticated user for this session, or nil.

    def user
    @user ||= User.find(self[:user_id]) if self[:user_id]
    end

    # Sets the authenticated user for this session. Set to nil to clear the
    # session's user, though you're probably better off calling sanitize.

    def user=(user)
    unless user
    self[:user_id] = @user = nil
    return
    end

    self[:user_id] = user.id
    @user = user
    end

    # Acts as a simple track clipboard. track IDs get persisted in the session
    # down in before_save if necessary.

    def tracks
    @tracks ||= Track.find(:all,
    :conditions => ["id in (?)", self[:tracks] || []], :order => :title)
    end

    # This session doesn't have an authenticated user, right?

    def anonymous?
    not user
    end

    # This session has an authenticated user, right?

    def authenticated?
    not anonymous?
    end

    # Is there a logged-in, administrative user?

    def admin?
    authenticated? && user.admin?
    end

    # Called by an after_filter in the application controller. A good
    # opportunity to turn heavy lists of stuff into IDs, etc.

    def before_save
    self[:tracks] = @tracks.collect(&:id) if @tracks
    end
    end
    end