Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Created September 1, 2022 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtsmfm/31e45cf9cf6bb42ef5f697ac2e669a7c to your computer and use it in GitHub Desktop.
Save mtsmfm/31e45cf9cf6bb42ef5f697ac2e669a7c to your computer and use it in GitHub Desktop.
before_action with inheritance with opposite option
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "rails"
gem "pry-byebug"
end
require "rack/test"
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_key_base = "secret_key_base"
Rails.logger = Logger.new(STDOUT)
routes.draw do
get "/" => "test2#index"
end
end
class TestController < ActionController::Base
before_action :only_except, only: [:index]
before_action :only_only, only: [:index]
before_action :except_only, except: [:index]
def index
end
def only_except
@only_except ||= 0
@only_except += 1
end
def only_only
@only_only ||= 0
@only_only += 1
end
def except_only
@except_only ||= 0
@except_only += 1
end
end
class Test2Controller < TestController
before_action :only_except, except: [:index]
before_action :only_only, only: [:index]
before_action :except_only, only: [:index]
def index
render(json: {only_except: @only_except, only_only: @only_only, except_only: @except_only})
end
end
require "minitest/autorun"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_index
get "/"
assert_equal ({"only_except"=>nil, "only_only"=>1, "except_only"=>1}), JSON.parse(last_response.body)
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment