Search code examples
rubyruby-on-rails-3rails-engines

Determine given module is rails engine


Hi I have two modules

  1. Admin
  2. Blog (Blog is a rails engine) where Admin is a module for namespacing admin features of app, but Blog is a module representing rails engine. Is there a better way to determine which among them is engine, like a function "is_engine?"

Admin.is_engine?

=> false

Blog.is_engine?

=> true

Definately I can have a try catch thing to determine this

def is_engine? module
  module::Engine
  true
rescue NameError
  false
end

here

is_engine? Admin

will return false

is_engine? Blog

will return true

Thanks


Solution

  • I'm not sure I understand what you are trying to do: a Rails Engine is a class (a subclass of Rails::Engine), not a module.

    If you have an instance, you could use:

    admin.kind_of?(Rails::Engine)
    

    If you have a class, you can use:

    Something.ancestors.include?(Rails::Engine)
    

    If what you have is a module, then it cannot be a subclass of Rails::Engine, and it's not an engine.

    EDIT

    If you have a module or constant something and want to know if there's a constant with a certain name in its namespace, you can use:

    something.constants.include?(:Engine)