-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52f688d
commit dd63441
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module StimulusReflex | ||
module ConcernEnhancer | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def method_missing(name, *args) | ||
case ancestors | ||
when ->(a) { !(a & [StimulusReflex::Reflex]).empty? } | ||
if (ActiveRecord::Base.public_methods + ActionController::Base.public_methods).include? name | ||
nil | ||
else | ||
super | ||
end | ||
when ->(a) { !(a & [ActiveRecord::Base, ActionController::Base]).empty? } | ||
if StimulusReflex::Reflex.public_methods.include? name | ||
nil | ||
else | ||
super | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(name, include_all = false) | ||
case ancestors | ||
when ->(a) { !(a & [StimulusReflex::Reflex]).empty? } | ||
(ActiveRecord::Base.public_methods + ActionController::Base.public_methods).include?(name) || super | ||
when ->(a) { !(a & [ActiveRecord::Base, ActionController::Base]).empty? } | ||
StimulusReflex::Reflex.public_methods.include?(name) || super | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "test_helper" | ||
|
||
class StimulusReflex::ConcernTest < ActiveSupport::TestCase | ||
module TestConcern | ||
extend ActiveSupport::Concern | ||
include StimulusReflex::ConcernEnhancer | ||
end | ||
|
||
class TestReflex < StimulusReflex::Reflex | ||
include TestConcern | ||
|
||
def initialize | ||
end | ||
end | ||
|
||
class TestController < ActionController::Base | ||
include TestConcern | ||
end | ||
|
||
class TestModel < ActiveRecord::Base | ||
include TestConcern | ||
end | ||
|
||
test "included in a reflex it stubs controller and model methods" do | ||
assert_nil TestReflex.helper_method | ||
assert_nil TestReflex.before_action | ||
assert_nil TestReflex.around_action | ||
assert_nil TestReflex.after_action | ||
|
||
assert_nil TestReflex.before_save | ||
assert_nil TestReflex.around_save | ||
assert_nil TestReflex.after_save | ||
|
||
refute_nil TestReflex.before_reflex | ||
end | ||
|
||
test "included in a controller it stubs reflex methods" do | ||
assert_nil TestController.before_reflex | ||
assert_nil TestController.around_reflex | ||
assert_nil TestController.after_reflex | ||
|
||
refute_nil TestController.before_action | ||
end | ||
|
||
test "included in a model it stubs reflex methods" do | ||
assert_nil TestModel.before_reflex | ||
assert_nil TestModel.around_reflex | ||
assert_nil TestModel.after_reflex | ||
|
||
refute_nil TestModel.after_save | ||
end | ||
end |