Skip to content

Instantly share code, notes, and snippets.

@brenes
Created August 8, 2016 11:42
Show Gist options
  • Save brenes/86cee6037b860bb3aad903adbc5877ea to your computer and use it in GitHub Desktop.
Save brenes/86cee6037b860bb3aad903adbc5877ea to your computer and use it in GitHub Desktop.

Revisions

  1. brenes created this gist Aug 8, 2016.
    24 changes: 24 additions & 0 deletions application_controler.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # 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