module Vanity::Rails::UseVanity
The use_vanity
method will setup the controller to allow testing and tracking of the current user.
Protected Instance Methods
use_vanity(method_name = nil, &block)
click to toggle source
Defines the vanity_identity method and the vanity_context_filter filter.
Call with the name of a method that returns an object whose identity will be used as the Vanity
identity if the user is not already cookied. Confusing? Let's try by example:
class ApplicationController < ActionController::Base use_vanity :current_user def current_user User.find(session[:user_id]) end end
If that method (current_user in this example) returns nil, Vanity
will look for a vanity cookie. If there is none, it will create an identity (using a cookie to remember it across requests). It also uses this mechanism if you don't provide an identity object, by calling use_vanity
with no arguments.
You can also use a block:
class ProjectController < ApplicationController use_vanity { |controller| controller.params[:project_id] } end
# File lib/vanity/frameworks/rails.rb, line 43 def use_vanity(method_name = nil, &block) define_method(:vanity_identity_block) { block } define_method(:vanity_identity_method) { method_name } callback_method_name = respond_to?(:before_action) ? :action : :filter send(:"around_#{callback_method_name}", :vanity_context_filter) send(:"before_#{callback_method_name}", :vanity_reload_filter) unless ::Rails.configuration.cache_classes send(:"before_#{callback_method_name}", :vanity_query_parameter_filter) send(:"after_#{callback_method_name}", :vanity_track_filter) end