Sha256: 2ca9960e8468289a5d0d908c197368ccc857f2d4f791cbc95c485e3f74ac6af5

Contents?: true

Size: 1.71 KB

Versions: 15

Compression:

Stored size: 1.71 KB

Contents

module Vanity  
  # Helper methods available on Object.
  #
  # @example From ERB template
  #   <%= ab_test(:greeting) %> <%= current_user.name %>
  # @example From Rails controller
  #   class AccountController < ApplicationController
  #     def create
  #       track! :signup
  #       Acccount.create! params[:account]
  #     end
  #   end
  # @example From ActiveRecord
  #   class Posts < ActiveRecord::Base
  #     after_create do |post|
  #       track! :images if post.type == :image
  #     end
  #   end
  module Helpers
    
    # This method returns one of the alternative values in the named A/B test.
    #
    # @example 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
    # @example Similar, alternative value is page name
    #   def index
    #     render action: ab_test(:new_page)
    #   end
    # @since 1.2.0
    def ab_test(name, &block)
      if Vanity.playground.using_js?
        @_vanity_experiments ||= {}
        @_vanity_experiments[name] ||= Vanity.playground.experiment(name).choose
        value = @_vanity_experiments[name].value
      else
        value = Vanity.playground.experiment(name).choose.value
      end

      if block
        content = capture(value, &block)
        block_called_from_erb?(block) ? concat(content) : content
      else
        value
      end
    end

    # Tracks an action associated with a metric.
    #
    # @example
    #   track! :invitation
    # @since 1.2.0
    def track!(name, count = 1)
      Vanity.playground.track! name, count
    end
  end
end

Object.class_eval do
  include Vanity::Helpers
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
vanity-1.8.4 lib/vanity/helpers.rb
vanity-1.8.3 lib/vanity/helpers.rb
vanity-1.8.3.beta2 lib/vanity/helpers.rb
vanity-1.8.3.beta lib/vanity/helpers.rb
vanity-1.8.2 lib/vanity/helpers.rb
vanity-1.8.1 lib/vanity/helpers.rb
moses-vanity-1.8.1 lib/vanity/helpers.rb
moses-vanity-1.8.0 lib/vanity/helpers.rb
vanity-1.8.0 lib/vanity/helpers.rb
moses-vanity-1.7.1 lib/vanity/helpers.rb
fiverr-vanity-1.7.2 lib/vanity/helpers.rb
vanity-1.7.1 lib/vanity/helpers.rb
vanity-1.7.0 lib/vanity/helpers.rb
vanity-1.6.1 lib/vanity/helpers.rb
vanity-1.6.0 lib/vanity/helpers.rb