Skip to content

Commit

Permalink
Stimulus reflex concern (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch authored Mar 30, 2021
1 parent 52f688d commit dd63441
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.5)
mimemagic (0.3.10)
nokogiri (~> 1)
rake
mini_mime (1.0.2)
mini_portile2 (2.5.0)
minitest (5.14.4)
Expand Down Expand Up @@ -191,4 +193,4 @@ DEPENDENCIES
stimulus_reflex!

BUNDLED WITH
2.1.4
2.2.4
1 change: 1 addition & 0 deletions lib/stimulus_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "cable_ready"
require "stimulus_reflex/version"
require "stimulus_reflex/cable_ready_channels"
require "stimulus_reflex/concern_enhancer"
require "stimulus_reflex/configuration"
require "stimulus_reflex/callbacks"
require "stimulus_reflex/reflex"
Expand Down
37 changes: 37 additions & 0 deletions lib/stimulus_reflex/concern_enhancer.rb
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
54 changes: 54 additions & 0 deletions test/concern_enhancer_test.rb
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

0 comments on commit dd63441

Please sign in to comment.