Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A very simple mongomapper plugin that lets you organise your models into subdirectories
Ruby
branch: master

This branch is 1 commit ahead of rlivsey:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
VERSION
mm-concerns.gemspec

README.rdoc

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

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'm 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

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

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

Something went wrong with that request. Please try again.