I’m using MongoDB as a backend for my project, but I don’t specifically want a dependency on Mongo for the life of the project in case I later decide to change it or in case unit testing is easier without the database. As written, however, my controller is strictly dependent on MongoDB.
Mostly taken from the MongoDB tutorial, I have the following in a config/initializers/mongo.rb file.
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = 'database'
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end
In a controller for querying the US states, I have the following code:
class StateController < ApplicationController
def index
states = MongoMapper.connection.db('database').collection('state').find()
render :json => states
end
end
There are a few issues I see right off the bat (there are probably more):
- As previously mentioned, the controller has a hard dependency on MongoDB.
- I’m not using the
databaseproperty of the MongoMapper class (it’s hardcoded in the controller). - I don’t necessarily want to go through an HTTP endpoint every single time I want a reference to a collection of states –though I’d like to keep this option available. For example, if a signup page has a drop down list for the user to select their home state, it seems foolish to require a client-side jQuery.get() to populate the list of states. It seems to me it would make more sense to have the states pre-fetched server-side and build the drop down list on the server.
For #3, I could certainly query the states in any action method that renders a view that requires the list of states and save them in @states (or something similar), but this would be a lot of duplicate code.
What would be the best way to architect this to allow for less coupling and more code reuse?
First, you should have a Model for states:
Then, in your controller, you should access via that:
This way, your controller has no idea what underlying datastore it’s using.
Finally, to reduce the need to make a HTTP call, but assuming you’re building this in javascript, you code:
Then load that from
$("#states").data("states")