lib/field_test/controller.rb in field_test-0.3.0 vs lib/field_test/controller.rb in field_test-0.3.1

- old
+ new

@@ -9,10 +9,24 @@ helper_method :field_test_converted helper_method :field_test_experiments end end + def field_test_upgrade_memberships(options = {}) + participants = FieldTest::Participant.standardize(options[:participant] || field_test_participant) + preferred = participants.first + Array(participants[1..-1]).each do |participant| + # can do this in single query once legacy_participants is removed + FieldTest::Membership.where(participant.where_values).each do |membership| + membership.participant = preferred.participant if membership.respond_to?(:participant=) + membership.participant_type = preferred.type if membership.respond_to?(:participant_type=) + membership.participant_id = preferred.id if membership.respond_to?(:participant_id=) + membership.save! + end + end + end + private def field_test_participant participants = [] @@ -21,25 +35,30 @@ participants << user if user end cookie_key = "field_test" + # name not entirely accurate + # can still set cookies in ActionController::API through request.cookie_jar + # however, best to prompt developer to pass participant manually + cookies_supported = respond_to?(:cookies, true) + if request.headers["Field-Test-Visitor"] token = request.headers["Field-Test-Visitor"] - elsif FieldTest.cookies + elsif FieldTest.cookies && cookies_supported token = cookies[cookie_key] if participants.empty? && !token token = SecureRandom.uuid cookies[cookie_key] = {value: token, expires: 30.days.from_now} end - else + elsif !FieldTest.cookies # anonymity set # note: hashing does not conceal input token = Digest::UUID.uuid_v5(FieldTest::UUID_NAMESPACE, ["visitor", FieldTest.mask_ip(request.remote_ip), request.user_agent].join("/")) # delete cookie if present - cookies.delete(cookie_key) if cookies[cookie_key] + cookies.delete(cookie_key) if cookies_supported && cookies[cookie_key] end # sanitize tokens token = token.gsub(/[^a-z0-9\-]/i, "") if token