ModelDiscovery is a simple yet flexible and fast solution to model discovery problem in Ruby on Rails applications. Detecting applications model in a Rails application is a bit hard because of autoload convention, specially when you use set of gems which add their own models to your application.
ModelDiscovery provides a solution to this problem without loading models in runtime. If you're familiar with Django framework, you can think ok ModelDiscovery as something like ContentType application in Django.
Add this line to your application's Gemfile:
gem 'model_discovery'
And then execute:
$ bundle
Or install it yourself as:
$ gem install model_discovery
Then add this line to your db/seeds.rb file:
ModelDiscovery::Engine.load_seedModelDiscovery will add a model to your application which will contains all the models class name. So you can use active record's query to fetch model names like:
# Fetch all models class names
models = ApplicationModels.all
# => ["User", "Group", "SomeGem::ModelExample"]
# Access to actual class of a model
model_class = models[0].constantize
# => "User"
# It's like User.all
model_class.allIt's simple isn't it ?
If you find any bug or have any issue using this gem I'll be happy to know. Please file an issue in Issue Tracker
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Model Discovery is maintained and funded by Yellowen. Whenever a code snippet is borrowed or inspired by existing code, we try to credit the original developer/designer in our source code. Let us know if you think we have missed to do this.
Model Discovery is Copyright © 2013-2015 Yellowen. It is free software, and may be redistributed under the terms specified in the LICENSE file.

