Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "::" in front of "::Rails::Engine" Mean [duplicate]

I am looking at the Rails Engine tutorial and one of the code blocks looks like the following:

module Blorgh
  class Engine < ::Rails::Engine
    isolate_namespace Blorgh
  end
end

What does the ::Rails::Engine mean? I know this is probably a trivial Ruby question, however, I don't seem to be able to find anything anywhere.

Thanks.

like image 862
El Guapo Avatar asked Feb 25 '16 03:02

El Guapo


1 Answers

So, Ray has this exactly right. I would just like to add a further example.

Let's say we have an engine called Foo (original, right?) that is mounted in a host application called Bar with something like:

#bar/config/routes.rb
Rails.application.routes.draw do
  ...
  mount Foo::Engine, at: '/'
  ...
end

Foo has an application_controller:

#foo/app/controllers/foo/application_controller.rb
module Foo
  class ApplicationController < ActionController::Base
    ...
    def foo_action
      ...
    end
    ...
  end
end

And Bar has an application_controller:

#bar/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  ...
  def bar_action
    ...
  end
  ...
end

Now, let's say we have two controllers within the Foo engine, like:

#foo/app/controllers/foo/baz_controller.rb
module Foo
  def BazController < ApplicationController
    ...
  end
end

and

#foo/app/controllers/foo/bif_controller.rb
module Foo
  def BifController < ::ApplicationController
    ...
  end
end

The BazController inherits from ApplicationController (no :: in front). This means that it looks up ApplicationController in the current namespace (Foo). And so it will have the foo_action defined in Foo::ApplicationController.

The BifController inherits from ::ApplicationController (:: in front). This means that it looks up ApplicationController in the global namespace which, in this case, is the host application Bar. And so it will have the bar_action defined in Bar's ApplicationController.

like image 117
jvillian Avatar answered Oct 27 '22 00:10

jvillian