Class: Split::Cacheable::Adapter
- Inherits:
-
Object
- Object
- Split::Cacheable::Adapter
- Defined in:
- lib/split_cacheable.rb
Constant Summary collapse
- DEFAULT_KEY =
'default/control'
Instance Method Summary collapse
-
#active_tests ⇒ Object
Get all tests which should be active on this controller for this action.
-
#get_all_possible_variations ⇒ Object
Search the Split::ExperimentCatalog to find all tests and generate every possible partial cache key.
-
#get_current_variations ⇒ Object
Use Split to return a partial cache key (used in fragment_cache_key) by calling ab_test which is an internal Split::Helper method that is now on your controller instance.
-
#initialize(controller_instance, action_name) ⇒ Adapter
constructor
A new instance of Adapter.
Constructor Details
#initialize(controller_instance, action_name) ⇒ Adapter
Returns a new instance of Adapter.
20 21 22 23 |
# File 'lib/split_cacheable.rb', line 20 def initialize(controller_instance, action_name) @controller = controller_instance @action_name = action_name end |
Instance Method Details
#active_tests ⇒ Object
Get all tests which should be active on this controller for this action
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/split_cacheable.rb', line 26 def active_tests ab_tests = @controller.class.split_cacheable_ab_tests.select { |test_obj| is_active = false if test_obj[:only].include?(@action_name) is_active = true end if test_obj[:only].empty? && !test_obj[:except].include?(@action_name) is_active = true end # The assumption here is that we should only evaluate the :if Proc or Boolean # if we are part of a live ActionController::Base. # This allows active_tests to return all possible active tests for when you call get_all_possible_variations if (defined?(@controller.request) && !@controller.request.nil?) && test_obj[:if] if is_active && (test_obj[:if].is_a?(Proc) ? test_obj[:if].call(@controller) : !!test_obj[:if]) is_active = true else is_active = false end end is_active } end |
#get_all_possible_variations ⇒ Object
Search the Split::ExperimentCatalog to find all tests and generate every possible partial cache key
Use this to clear all your action_caches
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/split_cacheable.rb', line 70 def get_all_possible_variations test_variations = Array.new active_tests.each { |test_obj| split_test = Split::ExperimentCatalog.find(test_obj[:test_name]) if split_test test_variations << split_test.alternatives.map { |alternative| "#{split_test.name}/#{alternative.name}" } end } case test_variations.length when 0 return [DEFAULT_KEY] when 1 return test_variations[0] else first_test = test_variations.shift return first_test.product(*test_variations).map{|a| a.join("/")} end end |
#get_current_variations ⇒ Object
Use Split to return a partial cache key (used in fragment_cache_key) by calling ab_test which is an internal Split::Helper method that is now on your controller instance
You should not be calling this method outside of a live ActionController::Base
58 59 60 61 62 63 64 |
# File 'lib/split_cacheable.rb', line 58 def get_current_variations if !defined?(@controller.request) || @controller.request.nil? return DEFAULT_KEY else return !active_tests.empty? ? active_tests.map{|test_obj| "#{test_obj[:test_name]}/#{@controller.ab_test(test_obj[:test_name])}"}.join('/') : DEFAULT_KEY end end |