diff --git a/README.md b/README.md index 9771693..02b4c6a 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](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: + 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. diff --git a/app/helpers/abraham_helper.rb b/app/helpers/abraham_helper.rb index 7d4db7b..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}"] @@ -21,11 +23,16 @@ 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_key"] + flipper_activation = tours[key]["flipper_activation"] + + 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..86e5b8f --- /dev/null +++ b/app/helpers/flipper_helper.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +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.to_sym)) || flipper_key.nil? + when "disabled" + return (flipper_key && !Flipper.enabled?(flipper_key.to_sym)) || flipper_key.nil? + else + return false + end + end + + private + def flipper_defined? + Object.const_defined?("Flipper") + end + + def process_activation_option(flipper_activation) + return "disabled" if flipper_activation == "disabled" + "enabled" + end +end 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 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