Module: Vanity::Rails::AddParticipant

Included in:
Dashboard
Defined in:
lib/vanity/frameworks/rails.rb

Overview

When configuring use_js to true, you must set up a route to add_participant_route.

Step 1: Add a new resource in config/routes.rb:

post "/vanity/add_participant" => "vanity#add_participant"

Step 2: Include Vanity::Rails::AddParticipant (or Vanity::Rails::Dashboard) in VanityController

class VanityController < ApplicationController
  include Vanity::Rails::AddParticipant
end

Step 3: Open your browser to localhost:3000/vanity

Instance Method Summary collapse

Instance Method Details

#add_participantObject

JS callback action used by vanity_js


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/vanity/frameworks/rails.rb', line 315

def add_participant
  if params[:v].nil?
    head 404
    return
  end

  h = {}
  params[:v].split(',').each do |pair|
    exp_id, answer = pair.split('=')
    exp = begin
      Vanity.playground.experiment(exp_id.to_s.to_sym)
    rescue StandardError
      nil
    end
    answer = answer.to_i

    if !exp || !exp.alternatives[answer]
      head 404
      return # rubocop:todo Lint/NonLocalExitFromIterator
    end
    h[exp] = exp.alternatives[answer].value
  end

  h.each { |e, a| e.chooses(a, request) }
  head 200
end