Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including the Hooks module a second time (e.g. by including a module that also includes it) wipes out previously defined hooks #36

Open
mhgoldman opened this issue Dec 10, 2019 · 0 comments

Comments

@mhgoldman
Copy link

module SomeModule
  extend ActiveSupport::Concern
  included do
    include Hooks
    define_hooks :myhook1
  end
end

class MyClass
  include Hooks
  define_hooks :myhook2

  include SomeModule

  def run_myhook1
    run_hook :myhook1
  end

  def run_myhook2
    run_hook :myhook2
  end  
end

MyClass._hooks # => {:myhook1=>[]}
MyClass.new.run_myhook1 # => Works fine
MyClass.new.run_myhook2 # => NoMethodError: undefined method `run' for nil:NilClass

This seems to be because the following code in lib/hooks.rb runs on every include:

# lib/hooks.rb
module Hooks
  def self.included(base)
    base.class_eval do
      extend Uber::InheritableAttr
      extend ClassMethods
      inheritable_attr :_hooks
      self._hooks= HookSet.new # <-- this wipes out existing hooks
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant