lib/field_test/experiment.rb in field_test-0.3.0 vs lib/field_test/experiment.rb in field_test-0.3.1
- old
+ new
@@ -8,50 +8,58 @@
@name = attributes[:name] || @id.to_s.titleize
@description = attributes[:description]
@variants = attributes[:variants]
@weights = @variants.size.times.map { |i| attributes[:weights].to_a[i] || 1 }
@winner = attributes[:winner]
+ @closed = attributes[:closed]
+ @keep_variant = attributes[:keep_variant]
@started_at = Time.zone.parse(attributes[:started_at].to_s) if attributes[:started_at]
@ended_at = Time.zone.parse(attributes[:ended_at].to_s) if attributes[:ended_at]
@goals = attributes[:goals] || ["conversion"]
@goals_defined = !attributes[:goals].nil?
@use_events = attributes[:use_events]
end
def variant(participants, options = {})
- return winner if winner
- return variants.first if options[:exclude]
+ return winner if winner && !keep_variant?
+ return control if options[:exclude]
participants = FieldTest::Participant.standardize(participants)
check_participants(participants)
membership = membership_for(participants) || FieldTest::Membership.new(experiment: id)
+ if winner # and keep_variant?
+ return membership.variant || winner
+ end
+
if options[:variant] && variants.include?(options[:variant])
membership.variant = options[:variant]
else
- membership.variant ||= weighted_variant
+ membership.variant ||= closed? ? control : weighted_variant
end
participant = participants.first
# upgrade to preferred participant
membership.participant = participant.participant if membership.respond_to?(:participant=)
membership.participant_type = participant.type if membership.respond_to?(:participant_type=)
membership.participant_id = participant.id if membership.respond_to?(:participant_id=)
- if membership.changed?
+ if membership.changed? && (!closed? || membership.persisted?)
begin
membership.save!
rescue ActiveRecord::RecordNotUnique
membership = memberships.find_by(participant.where_values)
end
end
- membership.try(:variant) || variants.first
+ membership.try(:variant) || control
end
def convert(participants, goal: nil)
+ return false if winner
+
goal ||= goals.first
participants = FieldTest::Participant.standardize(participants)
check_participants(participants)
membership = membership_for(participants)
@@ -170,9 +178,21 @@
results
end
def active?
!winner
+ end
+
+ def closed?
+ @closed
+ end
+
+ def keep_variant?
+ @keep_variant
+ end
+
+ def control
+ variants.first
end
def use_events?
if @use_events.nil?
FieldTest.events_supported?