Skip to content

Commit

Permalink
Fix exclude option in #field_test helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
falusi94 committed Nov 12, 2021
1 parent 18ff417 commit 81cb833
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,6 +1,7 @@
## 0.5.2 (unreleased)

- Fixed error with `events` association
- Fixed explicit exclusion by custom logic

## 0.5.1 (2021-09-22)

Expand Down
2 changes: 1 addition & 1 deletion lib/field_test/helpers.rb
Expand Up @@ -13,7 +13,7 @@ def field_test(experiment, **options)
end

if FieldTest.exclude_bots?
options[:exclude] = Browser.new(request.user_agent).bot?
options[:exclude] ||= Browser.new(request.user_agent).bot?
end

options[:exclude] ||= FieldTest.excluded_ips.any? { |ip| ip.include?(request.remote_ip) }
Expand Down
7 changes: 7 additions & 0 deletions test/controller_test.rb
Expand Up @@ -45,6 +45,13 @@ def test_bad_param
refute_includes response.body, "Button: bad"
end

def test_custom_exclude_logic
get api_users_url("exclude" => "true")
assert_response :success

assert_equal 0, FieldTest::Membership.count
end

def test_exclude_bots
get users_url, headers: {"HTTP_USER_AGENT" => "Googlebot"}
assert_response :success
Expand Down
15 changes: 15 additions & 0 deletions test/internal/app/controllers/api/users_controller.rb
@@ -0,0 +1,15 @@
module Api
class UsersController < ActionController::API
def index
button_color = field_test(:button_color, exclude: params[:exclude])

render json: { button_color: button_color }
end

private

def current_user
@current_user ||= User.last
end
end
end
4 changes: 4 additions & 0 deletions test/internal/config/routes.rb
@@ -1,3 +1,7 @@
Rails.application.routes.draw do
resources :users, only: [:index]

namespace :api do
resources :users, only: :index
end
end

0 comments on commit 81cb833

Please sign in to comment.