mm-concerns

A very simple mongomapper plugin that lets you organise your models into subdirectories

this code is a modification of code I saw in in [teambox](github.com/teambox/teambox) github.com/teambox/teambox/blob/master/config/initializers/concerns.rb

v1.1 is a mongomapper 0.9.0 update to support the new plugin system if you are using mongomapper 0.8.x please use version 1.0.1 of the gem

Usage

Install the gem

gem install mm-concerns

Or add it to your Gemfile

gem 'mm-concerns'

The ConcernsPlugin is automatically included in all your mongomapper documents.

All thats left is to split your fat models up at logical breaking points and tell your base models to require the pieces. The concerned_with method requires files from subdirectories with the same name as your model

app/models/user.rb

class User
  include MongoMapper::Document

  concerned_with :validations, :scopes

  key :email, String
  key :first_name, String
  key :last_name, String
  key :active, Boolean, :default => false
end

app/models/user/validations.rb

class User
  validates_presence_of :email
  validates_presence_of :first_name
  validates_presence_of :last_name
  belongs_to :organization
end

app/models/user/scopes.rb

class User
  scope :active, where( :active => true )
  scope :inactive, where( :active => false )
  scope :for_project, lambda { |project| where( :id => { :$in => project.user_ids } ) }
end

Why?

I find this system of organizing my functionality make my codebase much quicker and easier to maintain.

I use this plugin to organize my models like so:

app/models/
|
|-- ability.rb
|
|-- membership.rb
|
|-- organization.rb
|
|-- project.rb
|
|-- project
|   |-- associations.rb
|   |-- callbacks.rb
|   |-- scopes.rb
|   `-- validations.rb
|
|-- user.rb
|
|-- user
    |-- associations.rb
    |-- authentication.rb
    |-- scopes.rb
    `-- validations.rb

I'd be very interested to hear comments on this system, if this helps you out, or if you have a better way of organizing your code.

Contributing to mm-concerns

Copyright © 2010 Luke Cunningham. See LICENSE.txt for further details.