Module: Vanity::Rails::Helpers

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

Overview

Introduces ab_test helper (controllers and views). Similar to the generic ab_test method, with the ability to capture content (applicable to views, see examples).

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

A/B test inside ERB template (condition)

<%= if ab_test(:banner) %>100% less complexity!<% end %>

A/B test inside ERB template (value)

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

A/B test inside ERB template (capture)

<% ab_test :features do |count| %>
  <%= count %> features to choose from!
<% end %>


142
143
144
145
146
147
148
149
150
# File 'lib/vanity/frameworks/rails.rb', line 142

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

#vanity_h(text) ⇒ Object



152
153
154
# File 'lib/vanity/frameworks/rails.rb', line 152

def vanity_h(text)
  h(text)
end

#vanity_html_safe(text) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/vanity/frameworks/rails.rb', line 156

def vanity_html_safe(text)
  if text.respond_to?(:html_safe)
    text.html_safe
  else
    text
  end
end

#vanity_simple_format(text, html_options = {}) ⇒ Object



164
165
166
# File 'lib/vanity/frameworks/rails.rb', line 164

def vanity_simple_format(text, html_options={})
  vanity_html_safe(simple_format(text, html_options))
end