Sha256: 6adbeaca2f62d4e98e84a281e131723987415f1fa4faaddf6a312385ba83e2eb

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module FieldTest
  module Helpers
    def field_test(experiment, options = {})
      exp = FieldTest::Experiment.find(experiment)

      participants = field_test_participants(options)

      if try(:request)
        if params[:field_test] && params[:field_test][experiment]
          options[:variant] ||= params[:field_test][experiment]
        end
      end

      # cache results for request
      @field_test_cache ||= {}
      @field_test_cache[experiment] ||= exp.variant(participants, options)
    end

    def field_test_converted(experiment, options = {})
      exp = FieldTest::Experiment.find(experiment)

      participants = field_test_participants(options)

      exp.convert(participants)
    end

    def field_test_experiments(options = {})
      participants = field_test_participants(options)
      memberships = FieldTest::Membership.where(participant: participants).group_by(&:participant)
      experiments = {}
      participants.each do |participant|
        memberships[participant].each do |membership|
          experiments[membership.experiment] ||= membership.variant
        end
      end
      experiments
    end

    def field_test_participants(options = {})
      participants = []

      if options[:participant]
        participants << options[:participant]
      else
        if respond_to?(:current_user, true) && current_user
          participants << current_user
        end

        if try(:request)
          # use cookie
          cookie_key = "field_test"
          token = cookies[cookie_key]
          if participants.empty? && !token
            token = SecureRandom.uuid
            cookies[cookie_key] = {value: token, expires: 30.days.from_now}
          end
          if token
            participants << "cookie:#{token.gsub(/[^a-z0-9\-]/i, "")}"
          end
        end
      end

      FieldTest::Participant.standardize(participants)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
field_test-0.1.1 lib/field_test/helpers.rb