Curious about what makes require_dependency work in Discourse


(Aaron C) #1

I’m trying to learn Rails by looking at large real-world open source Rails apps.

The Ruby file discourse.rb has a line of code near the top which is executed very early in the Discourse application life cycle:

require_dependency 'plugin/instance'

Yet, I can’t see where the function require_dependency would be made globally available?

I’d tried something similar in a very simple Rails app and encounter the error:

undefined method `require_dependency' for main:Object

I can make it work by adding:

require 'active_support/depdencies' 

above the require_dependency line, but I prefer your technique.

So, there must be something I’m missing in the magic of Discourse?

(I also posted a more general question on StackOverflow, but it hasn’t gotten a response or much traffic).


(Kane York) #2

It is defined in the activesupport-4.1.6/ gem.

vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:      def require_dependency(file_name, message = "No such file to load -- %s")

(Aaron C) #3

As I had mentioned, I know that using require 'active_support/dependencies would properly add that function. My question is how Discourse uses the require_dependency function without adding the require.

There’s some magic I don’t understand (and hence cannot dispell).


(Sam Saffron) #4

Have a look at the Rails boot process.


(Aaron C) #5

Unfortunately, I’m just not seeing it (after spending more than a few hours looking). :frowning:

I’ve read through the page The Rails Initialization Process — Ruby on Rails Guides to confirm I was looking at the right files, but I don’t see where the dependencies.rb file would be loaded before the first use in the file containing the function Discourse.activate_plugins! (discourse.rb) (that is called from application.rb).


(Sam Saffron) #6

PROTIP

go to dependencies.rb and add

puts caller.join("\n") 

at the top.


(Ross Light) #7

This actually caused failures for me in trying to set up a dev environment. Adding this line to config/boot.rb seemed to fix it:

require 'active_support/dependencies'

(This is in Discourse 1.4.5.)