Skip to content

Commit

Permalink
Merge pull request #723 from splitrb/fix-context-shim-override-behavior
Browse files Browse the repository at this point in the history
Fix context shim override behavior
  • Loading branch information
andrehjr authored Mar 3, 2024
2 parents 7a075be + 397dc47 commit f34f68d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/split/encapsulated_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(context)
end

def params
request.params if request_present?
request.params if request && request.respond_to?(:params)
end

def request
Expand Down
4 changes: 2 additions & 2 deletions lib/split/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ def is_ignored_ip_address?
end

def params_present?
defined?(params) && params != nil
defined?(params) && params
end

def request_present?
defined?(request) && request != nil
defined?(request) && request
end

def active_experiments
Expand Down
17 changes: 3 additions & 14 deletions spec/encapsulated_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,23 @@
require "spec_helper"

describe Split::EncapsulatedHelper do
include Split::EncapsulatedHelper
let(:context_shim) { Split::EncapsulatedHelper::ContextShim.new(double(request: request)) }

describe "ab_test" do
before do
allow_any_instance_of(Split::EncapsulatedHelper::ContextShim).to receive(:ab_user)
.and_return(mock_user)
end

context "when params raises an error" do
before do
allow(self).to receive(:params).and_raise(NoMethodError)
end

it "should not raise an error " do
expect { params }.to raise_error(NoMethodError)
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
end

it "calls the block with selected alternative" do
expect { |block| ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
expect { |block| context_shim.ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
end

context "inside a view" do
it "works inside ERB" do
require "erb"
template = ERB.new(<<-ERB.split(/\s+/s).map(&:strip).join(" "), nil, "%")
foo <% ab_test(:foo, '1', '2') do |alt, meta| %>
foo <% context_shim.ab_test(:foo, '1', '2') do |alt, meta| %>
static <%= alt %>
<% end %>
ERB
Expand Down

0 comments on commit f34f68d

Please sign in to comment.