# Getting a list of models, AND custom model directory

**URL:** <https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339>\
**Category:** Rails\
**Created:** [September 11, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339 "2008-09-11T21:01:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 11, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/1 "2008-09-11T21:01:41Z")

</div>

I’m building an a client/web application that will rely on custom,  
simple plugins that will be written as models with single table  
inheritance to a Widget class. Ideally somebody would just need to  
create a simple model which extends the parent, placed in the correct  
directory and it work “just work”. I’m avoiding the rails plugin  
architecture because it should be stupid simple for people to build  
and install. Unfortunately I’m not sure how to:

- Configure Rails to load models from a directory other than /app/  
models/ (is this a ‘require’ in the environment.rb?)
- Get a list of all model objects which inherit from the parent  
class. They will not have database records at first so I cannot rely  
on a DB query.

Here’s a high level look at how the model plugin would look:

class HelloWorld \< Widget

# Create a serialized hash of default options

def install  
end

# Will be called by the system automatically to execute this plugin

def run  
say “Hello World”  
end

end

Ideally it would live in RAILS\_ROOT/widgets/ directory.

The application will need to know of all the Widget sub-classes in  
order to automatically call their ‘install’ and ‘run’ functions. I’m  
also considering doing this in Merb, but I have the most experience in  
Rails and it’s for a 24 hour hackfest (at Yahoo!), so I need to build  
this quickly.

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 11, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/2 "2008-09-11T22:20:58Z")

</div>

On Sep 11, 8:00 pm, Mozmonkey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m building an a client/web application that will rely on custom,  
> simple plugins that will be written as models with single table  
> inheritance to a Widget class. Ideally somebody would just need to  
> create a simple model which extends the parent, placed in the correct  
> directory and it work “just work”. I’m avoiding the rails plugin  
> architecture because it should be stupid simple for people to build  
> and install. Unfortunately I’m not sure how to:
> 
> - Configure Rails to load models from a directory other than /app/  
> models/ (is this a ‘require’ in the environment.rb?)

add the folder(s) to config.load\_paths (have a look in environment.rb.  
There should be a commented out example of this

> - Get a list of all model objects which inherit from the parent  
> class. They will not have database records at first so I cannot rely  
> on a DB query.

easiest way to do this is probably to just grab a list of everything  
in that magic folder (you might as well just require them).  
ActiveSupport adds a subclasses method to Class, but obviously that  
will only return classes that have actually been loaded.

Fred

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 13, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/3 "2008-09-13T12:26:03Z")

</div>

For some reason that is not working. Here’s the load\_paths line in  
the environment file:

config.load\_paths += %W( #{RAILS\_ROOT}/widgets )

And the main widget model (widget.rb):

class Widget \< ActiveRecord::Base  
set\_table\_name :widget

# 

# Get a list of widgets

# 

def self.get\_widgets  
logger.debug(HelloWorld)  
end

end

And the actual widget which lives in widgets/HelloWorld.rb

class HelloWorld \< Widget

# Create a serialized hash of default options

def install  
end

# Will be called by the system automatically to execute this plugin

def run  
say “Hello World”  
end

end

Any ideas? Thanks.

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 13, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/4 "2008-09-13T12:39:29Z")

</div>

Woops, the file was named wrong. Instead of HelloWorld.rb it needed  
to be hello\_world.rb. Now Class.subclass only shows classes that have  
been instantiated, so I can’t quite get a list of subclasses without  
creating a new instance of all of them.

So how would I loop through the files in the widgets directory and get  
the class name from each of them? Furthermore, how would I execute  
that class later dynamically since the class name will be held in a  
variable?

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 13, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/5 "2008-09-13T18:49:44Z")

</div>

Woops, the file was named wrong. Instead of HelloWorld.rb it needed  
to be hello\_world.rb.

Class.subclass only seems to show classes that have  
been instantiated, so I can’t quite get a list of subclasses without  
creating a new instance of all of them.

So how would I loop through the files in the widgets directory and get  
the class name from each of them? Furthermore, how would I execute  
that class later dynamically since the class name will be held in a  
variable?

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 13, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/6 "2008-09-13T15:28:46Z")

</div>

On 13 Sep 2008, at 11:25, Mozmonkey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For some reason that is not working. Here’s the load\_paths line in  
> the environment file:
> 
> config.load\_paths += %W( #{RAILS\_ROOT}/widgets )

What’s happening?

Fred

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 13, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-models-and-custom-model-directory/148339/7 "2008-09-13T18:56:10Z")

</div>

On 13 Sep 2008, at 17:48, Mozmonkey wrote:

> Woops, the file was named wrong. Instead of HelloWorld.rb it needed  
> to be hello\_world.rb.
> 
> Class.subclass only seems to show classes that have  
> been instantiated, so I can’t quite get a list of subclasses without  
> creating a new instance of all of them.

They don’t need to be instantiated, just loaded. If you Dir.glob your  
widgets folder and require everything inside it you should be ok.

Fred
