module Vanity::Rails::Filters
Vanity
needs these filters. They are includes in ActionController and automatically added when you use use_vanity in your controller.
Public Instance Methods
Filter to track metrics. Pass _track param along to call track! on that alternative.
# File lib/vanity/frameworks/rails.rb, line 187 def vanity_track_filter if request.get? && params[:_track] Vanity.track! params[:_track] end end
Protected Instance Methods
Around filter that sets Vanity.context
to controller.
# File lib/vanity/frameworks/rails.rb, line 139 def vanity_context_filter previous, Vanity.context = Vanity.context, self yield ensure Vanity.context = previous end
This filter allows user to choose alternative in experiment using query parameter.
Each alternative has a unique fingerprint (run vanity list command to see them all). A request with the _vanity query parameter is intercepted, the alternative is chosen, and the user redirected to the same request URL sans _vanity parameter. This only works for GET requests.
For example, if the user requests the page example.com/?_vanity=2907dac4de, the first alternative of the :null_abc experiment is chosen and the user redirected to example.com/.
# File lib/vanity/frameworks/rails.rb, line 159 def vanity_query_parameter_filter query_params = request.query_parameters if request.get? && query_params[:_vanity] hashes = Array(query_params.delete(:_vanity)) Vanity.playground.experiments.each do |id, experiment| if experiment.respond_to?(:alternatives) experiment.alternatives.each do |alt| if hashes.delete(experiment.fingerprint(alt)) experiment.chooses(alt.value) break end end end break if hashes.empty? end path_parts = [url_for, query_params.to_query] redirect_to(path_parts.join('?')) end end
Before filter to reload Vanity
experiments/metrics. Enabled when cache_classes is false (typically, testing environment).
# File lib/vanity/frameworks/rails.rb, line 181 def vanity_reload_filter Vanity.playground.reload! end