SmartModel - (nathanpro.github.com/SmartModel)
What is
SmartModel is a Rails 3 Library(plugin and gem) made to improve the way you use MongoMapper. It gives you aliases, and even a model generator for MongoDB models.
Setup
As plugin
To setup SmartModel as a plugin, you need mongo_mapper (that needs bson_ext), so add this to your Gemfile:
gem 'mongo_mapper'
gem 'bson_ext'
And then run
$ rails plugin install http://github.com/nathanPro/SmartModel.git
As a gem
Just add:
gem 'smart_model'
To your Gemfile and run:
$ [sudo] bundle install
Setting MongoMapper DB
As we use MongoMapper, you still need to set the database name, what can be easily done in a config/initializers/mongodb.rb like that
MongoMapper.database = "wtv-#{Rails.env}"
And you are done!
Generator
SmartModel gives you the smart_model generator:
$ rails g smart_model project name:string content:string created_at:date in_progress:done
That creates a model Project with the SmartModel::Base module included
SmartModel::Base
Is a class with all the MongoMapper::Document methods, and with SmartModel::Attributes and SmartModel::Relationships included
SmartModel::Attributes
It is responsible for handling the columns of yout database. To add a title columns to your project model, that keeps a string ,in example, you can just type this:
class Project
include SmartModel::Attributes # Load SmartModel
string :title , :required => true # Create title column ,that accept strings
end
Or if you have many columns with the same behaviour , you can just manage them just like this:
class Project
include SmartModel::Attributes
strings [:title , : content] , :required => true # Create :title and :content columns
end
The given methods are
string(s) , integer(s) , boolean(s) , array(s) , objectid(s) , date(s)
SmartModel::Relationships
It is responsible for making the belongs_to method also create de column in the database for the id
class Project
include SmartModel::Relationships
belongs_to :task
# however , if you call , in example
# string :title
# it will raise an error , because you didn't included the Attributes module
end
TO-DO
Create a scaffold generator
Why have I done it?
I was on vacation, so, I decided to code something usefull, and this was my first idea.
Contributors / Maintainers
-
Nathan Proença ([email protected])
-
Rodrigo Alves Vieira ([email protected])
Copyright
Copyright © 2010 Nathan Proença. Read LICENCE for details.