Skip to content

Commit

Permalink
Use AB Testing Gem
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed Feb 9, 2017
1 parent 5eaf1a1 commit 70dd2b9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 31 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gem 'unicorn', '4.8'
gem 'rails-i18n', '>= 4.0.4'
gem 'rails_translation_manager', '~> 0.0.2'
gem 'rails-controller-testing', '~> 0.1'
gem 'govuk_ab_testing', '0.1.0'

if ENV['API_DEV']
gem 'gds-api-adapters', path: '../gds-api-adapters'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ GEM
govuk-lint (1.2.0)
rubocop (~> 0.39.0)
scss_lint
govuk_ab_testing (0.1.0)
govuk_frontend_toolkit (5.1.0)
rails (>= 3.1.0)
sass (>= 3.2.0)
Expand Down Expand Up @@ -253,6 +254,7 @@ DEPENDENCIES
gds-api-adapters (= 39.1.0)
govuk-content-schema-test-helpers (= 1.1.0)
govuk-lint
govuk_ab_testing (= 0.1.0)
govuk_frontend_toolkit (= 5.1.0)
govuk_navigation_helpers (~> 2.1.0)
jasmine-rails (~> 0.13.0)
Expand Down
17 changes: 3 additions & 14 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'gds_api/content_store'

class ContentItemsController < ApplicationController
include ABTestable
include EducationNavigationABTestable

rescue_from GdsApi::HTTPForbidden, with: :error_403
rescue_from GdsApi::HTTPNotFound, with: :error_notfound

attr_accessor :content_item

def show
load_content_item
set_expiry
Expand All @@ -15,11 +17,6 @@ def show
end
end

# Visible for testing
def should_present_new_navigation_view?
(education_navigation_ab_testing_group == "B") && new_navigation_enabled? && content_is_tagged_to_a_taxon?
end

private

def load_content_item
Expand Down Expand Up @@ -74,12 +71,4 @@ def error_notfound
def set_page_variant
request.variant = :new_navigation if should_present_new_navigation_view?
end

def new_navigation_enabled?
ENV['ENABLE_NEW_NAVIGATION'] == 'yes'
end

def content_is_tagged_to_a_taxon?
@content_item.taxons.any?
end
end
5 changes: 0 additions & 5 deletions app/presenters/ab_testable.rb

This file was deleted.

8 changes: 0 additions & 8 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ def content_id
content_item.parsed_content["content_id"]
end

def taxons
if content_item["links"].include?("taxons")
content_item["links"]["taxons"]
else
[]
end
end

def breadcrumbs
@nav_helper.breadcrumbs[:breadcrumbs]
end
Expand Down
30 changes: 30 additions & 0 deletions app/presenters/education_navigation_ab_testable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module EducationNavigationABTestable
def education_navigation_ab_test
@ab_test ||= GovukAbTesting::AbTest.new("educationnavigation")
end

def should_present_new_navigation_view?
education_navigation_variant.variant_b? && new_navigation_enabled? && content_is_tagged_to_a_taxon?
end

def education_navigation_variant
@education_navigation_variant ||= education_navigation_ab_test.requested_variant request
end

def new_navigation_enabled?
ENV['ENABLE_NEW_NAVIGATION'] == 'yes'
end

def content_is_tagged_to_a_taxon?
content_item.content_item.dig("links", "taxons").present?
end

def set_education_navigation_response_header
education_navigation_variant.configure_response response
end

def self.included(base)
base.helper_method :education_navigation_variant
base.after_filter :set_education_navigation_response_header
end
end
12 changes: 8 additions & 4 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
class ContentItemsControllerTest < ActionController::TestCase
include GdsApi::TestHelpers::ContentStore

setup do
ENV['ENABLE_NEW_NAVIGATION'] = 'yes'
end

teardown do
ENV['ENABLE_NEW_NAVIGATION'] = nil
end

test "routing handles translated content paths" do
translated_path = 'government/case-studies/allez.fr'

Expand Down Expand Up @@ -76,8 +84,6 @@ class ContentItemsControllerTest < ActionController::TestCase
end

test "honours Education Navigation AB Testing cookie" do
ENV['ENABLE_NEW_NAVIGATION'] = 'yes'

content_item = content_store_has_schema_example('case_study', 'case_study')
path = 'government/abtest/easy-as-1-2'
content_item['base_path'] = "/#{path}"
Expand All @@ -97,8 +103,6 @@ class ContentItemsControllerTest < ActionController::TestCase
end

test "does not show new navigation when no taxons tagged to content" do
ENV['ENABLE_NEW_NAVIGATION'] = 'yes'

content_item = content_store_has_schema_example('case_study', 'case_study')
path = 'government/abtest/easy-as-1-2'
content_item['base_path'] = "/#{path}"
Expand Down

0 comments on commit 70dd2b9

Please sign in to comment.