From 040988f6d78d9f111616ed07d623962db538d049 Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 09:45:35 -0400 Subject: [PATCH 1/9] added check to reject tours based on Flipper --- app/helpers/abraham_helper.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/helpers/abraham_helper.rb b/app/helpers/abraham_helper.rb index 7d4db7b..226d1dc 100644 --- a/app/helpers/abraham_helper.rb +++ b/app/helpers/abraham_helper.rb @@ -8,6 +8,8 @@ def abraham_tour tours ||= Rails.configuration.abraham.tours["#{controller_path}.#{action_name}.#{I18n.default_locale}"] if tours + flipper_defined = Object.const_defined?("Flipper") + # Have any automatic tours been completed already? completed = AbrahamHistory.where( creator_id: current_user.id, @@ -21,11 +23,20 @@ def abraham_tour tour_html = '' tour_keys.each do |key| - tour_html += render(partial: "application/abraham", - locals: { tour_name: key, - tour_completed: tour_keys_completed.include?(key), - trigger: tours[key]["trigger"], - steps: tours[key]["steps"] }) + flipper_key = tours[key]["flipper"] + + should_add_tour = + ( (flipper_key and flipper_defined and Flipper.enabled?(flipper_key.to_sym)) || + flipper_key.nil?) + + if should_add_tour + tour_html += render(partial: "application/abraham", + locals: { tour_name: key, + tour_completed: tour_keys_completed.include?(key), + trigger: tours[key]["trigger"], + steps: tours[key]["steps"] }) + end + end tour_html.html_safe From 2d7dcf326157497e6fc1c6152a469d7930f22675 Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 13:11:20 -0400 Subject: [PATCH 2/9] added option to add tour if flipper is disabled --- app/helpers/abraham_helper.rb | 14 +++++--------- app/helpers/flipper_helper.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 app/helpers/flipper_helper.rb diff --git a/app/helpers/abraham_helper.rb b/app/helpers/abraham_helper.rb index 226d1dc..f9f2a65 100644 --- a/app/helpers/abraham_helper.rb +++ b/app/helpers/abraham_helper.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true module AbrahamHelper + include FlipperHelper + def abraham_tour # Do we have tours for this controller/action in the user's locale? tours = Rails.configuration.abraham.tours["#{controller_path}.#{action_name}.#{I18n.locale}"] @@ -8,8 +10,6 @@ def abraham_tour tours ||= Rails.configuration.abraham.tours["#{controller_path}.#{action_name}.#{I18n.default_locale}"] if tours - flipper_defined = Object.const_defined?("Flipper") - # Have any automatic tours been completed already? completed = AbrahamHistory.where( creator_id: current_user.id, @@ -23,20 +23,16 @@ def abraham_tour tour_html = '' tour_keys.each do |key| - flipper_key = tours[key]["flipper"] + flipper_key = tours[key]["flipper_key"] + flipper_activation = tours[key]["flipper_activation"] - should_add_tour = - ( (flipper_key and flipper_defined and Flipper.enabled?(flipper_key.to_sym)) || - flipper_key.nil?) - - if should_add_tour + if should_add_tour(flipper_key, flipper_activation) tour_html += render(partial: "application/abraham", locals: { tour_name: key, tour_completed: tour_keys_completed.include?(key), trigger: tours[key]["trigger"], steps: tours[key]["steps"] }) end - end tour_html.html_safe diff --git a/app/helpers/flipper_helper.rb b/app/helpers/flipper_helper.rb new file mode 100644 index 0000000..a87e1cb --- /dev/null +++ b/app/helpers/flipper_helper.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module FlipperHelper + def should_add_tour(flipper_key, flipper_activation) + case process_activation_option(flipper_activation) + when "enabled" + return (flipper_key && flipper_enabled?(key)) || flipper_key.nil? + when "disabled" + return (flipper_key && flipper_disabled?(key)) || flipper_key.nil? + else + return false + end + end + + private + def flipper_defined? + Object.const_defined?("Flipper") + end + + def flipper_enabled?(key) + flipper_defined? && Flipper.enabled?(key.to_sym) + end + + def flipper_disabled?(key) + flipper_defined? && Flipper.disabled?(key.to_sym) + end + + def process_activation_option(flipper_activation) + return "disabled" if flipper_activation == "disabled" + "enabled" + end +end From f9adc40b28c99c8d8a72825733ff4889f143c7ed Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 14:13:40 -0400 Subject: [PATCH 3/9] updated version number --- lib/abraham/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/abraham/version.rb b/lib/abraham/version.rb index 2255815..cd97839 100644 --- a/lib/abraham/version.rb +++ b/lib/abraham/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Abraham - VERSION = "2.5.0" + VERSION = "2.5.1" end From 8a72d4a13c9dee52ccf80aa2c3fb651eb12c06c1 Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 14:18:00 -0400 Subject: [PATCH 4/9] fixed misnamed variable --- app/helpers/flipper_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/flipper_helper.rb b/app/helpers/flipper_helper.rb index a87e1cb..047421a 100644 --- a/app/helpers/flipper_helper.rb +++ b/app/helpers/flipper_helper.rb @@ -4,9 +4,9 @@ module FlipperHelper def should_add_tour(flipper_key, flipper_activation) case process_activation_option(flipper_activation) when "enabled" - return (flipper_key && flipper_enabled?(key)) || flipper_key.nil? + return (flipper_key && flipper_enabled?(flipper_key)) || flipper_key.nil? when "disabled" - return (flipper_key && flipper_disabled?(key)) || flipper_key.nil? + return (flipper_key && flipper_disabled?(flipper_key)) || flipper_key.nil? else return false end From aafe25c19a5dd5ba266f70cce9be59d1b025d17f Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 14:22:27 -0400 Subject: [PATCH 5/9] fixed issue with flipper disabled --- app/helpers/flipper_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/flipper_helper.rb b/app/helpers/flipper_helper.rb index 047421a..c3290da 100644 --- a/app/helpers/flipper_helper.rb +++ b/app/helpers/flipper_helper.rb @@ -22,7 +22,7 @@ def flipper_enabled?(key) end def flipper_disabled?(key) - flipper_defined? && Flipper.disabled?(key.to_sym) + flipper_defined? && !Flipper.enabled?(key.to_sym) end def process_activation_option(flipper_activation) From 313cb0b0f8c35fda9f2e3ad028e42e14ee9623ec Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 14:35:22 -0400 Subject: [PATCH 6/9] updated readme --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 9771693..dd0e950 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,29 @@ my_tour: * `action` is one of the Shepherd tour method names, i.e. `cancel`, `next`, or `complete` * `classes` are the CSS class names you want applied to the button +### Flipper integration + +If you have Flipper installed as a dependency in your project you will be able to enable or disable tours based on a flipper useing the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive. + +```yml +walkthrough: + flipper_key: "name_of_flipper" + steps: + 1: + text: "This walkthrough will show you how to..." +``` + +If you would like to disable a tour when a flipper is active you may couple the `flipper_key` option with the `flipper_activation` option. `flipper_activation` supports "enabled" or "disabled" as options. If you enter something other than "enabled" or "disabled" it will use the default, which is "enabled". + +```yml +walkthrough: + flipper_key: "name_of_flipper" + flipper_activation: "disabled" + steps: + 1: + text: "This walkthrough will show you how to..." +``` + ### Testing your tours Abraham loads tour definitions once when you start your server. Restart your server to see tour changes. From c3bcb0c8f290ba85f95ed1083b88ea1b9d7c7b8f Mon Sep 17 00:00:00 2001 From: Jeremy Warren Date: Mon, 13 Jun 2022 16:45:48 -0400 Subject: [PATCH 7/9] updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd0e950..1c79a3b 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ my_tour: ### Flipper integration -If you have Flipper installed as a dependency in your project you will be able to enable or disable tours based on a flipper useing the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive. +If you have Flipper installed as a dependency in your project you will be able to enable or disable tours based on a flipper using the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive. ```yml walkthrough: From 7be72dfba1f2627bb5a0ea3cfe7feaaeb7293214 Mon Sep 17 00:00:00 2001 From: Jeremy Warren <96201155+jwarrenac@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:03:05 -0400 Subject: [PATCH 8/9] Update README.md Co-authored-by: Jonathan Abbett --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c79a3b..02b4c6a 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ my_tour: ### Flipper integration -If you have Flipper installed as a dependency in your project you will be able to enable or disable tours based on a flipper using the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive. +If you have [Flipper](https://github.com/jnunemaker/flipper) installed as a dependency in your project you will be able to enable or disable tours based on a flipper using the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive. ```yml walkthrough: From bb8a143943295b21b40ff1f29f7aa97dbf48acd8 Mon Sep 17 00:00:00 2001 From: Jonathan Abbett Date: Tue, 14 Jun 2022 12:25:16 -0400 Subject: [PATCH 9/9] Update: Added tests for FlipperHelper --- app/helpers/flipper_helper.rb | 14 +++----- .../dummy/test/helpers/flipper_helper_test.rb | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 test/dummy/test/helpers/flipper_helper_test.rb diff --git a/app/helpers/flipper_helper.rb b/app/helpers/flipper_helper.rb index c3290da..86e5b8f 100644 --- a/app/helpers/flipper_helper.rb +++ b/app/helpers/flipper_helper.rb @@ -2,11 +2,13 @@ module FlipperHelper def should_add_tour(flipper_key, flipper_activation) + return true if !flipper_defined? + case process_activation_option(flipper_activation) when "enabled" - return (flipper_key && flipper_enabled?(flipper_key)) || flipper_key.nil? + return (flipper_key && Flipper.enabled?(flipper_key.to_sym)) || flipper_key.nil? when "disabled" - return (flipper_key && flipper_disabled?(flipper_key)) || flipper_key.nil? + return (flipper_key && !Flipper.enabled?(flipper_key.to_sym)) || flipper_key.nil? else return false end @@ -17,14 +19,6 @@ def flipper_defined? Object.const_defined?("Flipper") end - def flipper_enabled?(key) - flipper_defined? && Flipper.enabled?(key.to_sym) - end - - def flipper_disabled?(key) - flipper_defined? && !Flipper.enabled?(key.to_sym) - end - def process_activation_option(flipper_activation) return "disabled" if flipper_activation == "disabled" "enabled" diff --git a/test/dummy/test/helpers/flipper_helper_test.rb b/test/dummy/test/helpers/flipper_helper_test.rb new file mode 100644 index 0000000..94efb08 --- /dev/null +++ b/test/dummy/test/helpers/flipper_helper_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require "test_helper" + +class FlipperHelperTest < ActionView::TestCase + test "Add tour whenever all options nil" do + assert should_add_tour(nil, nil) + end + + test "Add tour whenever Flipper is disabled " do + # Flipper is not enabled, so tour should be added regardless of options + assert should_add_tour("foo", "enabled") + assert should_add_tour("foo", "disabled") + end + + test "Respect Flipper results if Flipper enabled" do + mockFlipper = Object.new + mockFlipper.stubs(:enabled?).returns(true) + + Kernel.const_set('Flipper', mockFlipper) + assert_equal Flipper, mockFlipper + + assert should_add_tour("foo", "enabled") + refute should_add_tour("foo", "disabled") + + mockFlipper.stubs(:enabled?).returns(false) + + refute should_add_tour("foo", "enabled") + assert should_add_tour("foo", "disabled") + + Kernel.send(:remove_const, :Flipper) + end +end