Skip to content
This repository has been archived by the owner on Jun 25, 2019. It is now read-only.

icaruswings/mm-concerns

Repository files navigation

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

  • 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 © 2010 Luke Cunningham. See LICENSE.txt for further details.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages