0

I have two classes app/responders/application_responder and app/responders/stripe/basic_responder

I have code in ApplicationResponder like so:

class ApplicationResponder
    def self.responds_to(event_type, event_name)
        @@registered ||= []
        payload = {
            event_type: event_type,
            event_name: event_name,
            cls: self
        }
        @@registered << payload
    end

    def self.registered
        @@registered
    end
end

Then my basic responder registeres itself:

class Stripe::BasicResponder < ApplicationResponder

    responds_to :stripe, 'account.created'

    def respond(event)
        puts "foo!"
        pp event
    end

end

If I load up rails console and call ApplicationResponder.registered I'd expect to see a list of one element. Sometimes I do, sometimes it returns an empty list which to me suggests that stripe/basic_responder has not been evaluated yet?

Any ideas how to fix this?

I've tried adding responders to the eager load path in application.rb:

config.eager_load_paths += %W(#{config.root}/app/responders)
Matthew Rathbone
  • 8,144
  • 7
  • 49
  • 79
  • `config.eager_load_paths` only loads the files if caching is enabled. You would have to explicitly require the files for this to work in dev – max May 12 '17 at 23:55
  • "config.eager_load_paths accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the app directory of the application." – max May 12 '17 at 23:56
  • You might also want to read http://stackoverflow.com/questions/40819068/why-is-using-a-class-variable-in-ruby-considered-a-code-smell/40819118 – max May 13 '17 at 00:12

0 Answers0