Skip to content

Instantly share code, notes, and snippets.

@csexton
Created July 28, 2016 14:48

Revisions

  1. csexton created this gist Jul 28, 2016.
    16 changes: 16 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    class ApplicationController < ActionController::Base
    before_action :set_radius_user_cache_key

    # bunch-o-auth stuff goes here

    private

    def set_radius_user_cache_key
    if current_user
    cookies[:_radius_user_cache_key] = { value: current_user.cache_key, domain: :all, tld_length: 2 }
    else
    cookies[:_radius_user_cache_key] = { value: "none", domain: :all, tld_length: 2 }
    end
    end
    end

    31 changes: 31 additions & 0 deletions application_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'rails_helper'

    describe ApplicationController, :type => :controller do

    let(:centurion) { create(User) }

    def stub_sign_in(user)
    allow(controller).to receive(:current_user).and_return(user)
    sign_in user
    end

    controller do
    public :cookies
    def index
    render nothing: true
    end
    end

    describe "GET 'index'" do
    it "sets the user cache key cookie" do
    sign_in centurion
    allow(controller).to receive(:current_user).and_return(centurion)

    get 'index'

    key = controller.cookies[:_radius_user_cache_key]
    expect(key).to eq centurion.cache_key
    end
    end

    end