Created
August 8, 2016 11:42
-
-
Save brenes/86cee6037b860bb3aad903adbc5877ea to your computer and use it in GitHub Desktop.
Rails multiple request variants
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
# This code is an example to combine different variants from different reasons into the request.variant setting from Rails 4.1 | |
before_action :set_versions | |
def set_versions | |
# First, we load all the variants from whatever our variant logic is (mobile support, tests ab, beta versions...) | |
variants = [] | |
variants << params[:b].to_sym if params[:b] | |
variants << params[:a].to_sym if params[:a] | |
# Now, we combine the variants | |
combined_variants = [] | |
variants.length.times do |i| | |
combined_variants << variants.combination(i+1).to_a | |
end | |
combined_variants = combined_variants.reverse.flatten(1) | |
combined_variants = combined_variants.map{|v| v.join("+").to_sym} | |
# and assign the request.variant | |
request.variant = combined_variants | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment