Module: Vanity::Helpers

Defined in:
lib/vanity/helpers.rb

Overview

Helper methods available on Object.

Examples:

From ERB template

<%= ab_test(:greeting) %> <%= current_user.name %>

From Rails controller

class AccountController < ApplicationController
  def create
    track! :signup
    Acccount.create! params[:account]
  end
end

From ActiveRecord

class Posts < ActiveRecord::Base
  after_create do |post|
    track! :images if post.type == :image
  end
end

Instance Method Summary collapse

Instance Method Details

#ab_test(name, &block) ⇒ Object

This method returns one of the alternative values in the named A/B test.

Examples:

A/B two alternatives for a page

def index
  if ab_test(:new_page) # true/false test
    render action: "new_page"
  else
    render action: "index"
  end
end

Similar, alternative value is page name

def index
  render action: ab_test(:new_page)
end

Since:

  • 1.2.0



36
37
38
39
40
41
42
43
44
# File 'lib/vanity/helpers.rb', line 36

def ab_test(name, &block)
  value = Vanity.playground.experiment(name).choose
  if block
    content = capture(value, &block)
    block_called_from_erb?(block) ? concat(content) : content
  else
    value
  end
end

#track!(name, count = 1) ⇒ Object

Tracks an action associated with a metric.

Examples:

track! :invitation

Since:

  • 1.2.0



51
52
53
# File 'lib/vanity/helpers.rb', line 51

def track!(name, count = 1)
  Vanity.playground.track! name, count
end