1

I have a Padrino project, that consists of multiple apps. For example:

  • Website (Models: Site, Page)
  • Blog (Models: Post, Comment)
  • Shop (Models: Category, Product, Order)
  • Tracking (Models: Visitor, Content)

Putting all models unmodified into one directory seems to me like a mess. So I thought of namespacing them like:

  • Website (Models: Site, SitePage)
  • Blog (Models: BlogPost, BlogComment)
  • Shop (Models: ShopCategory, ShopProduct, ShopOrder)
  • Tracking (Models: TrackingVisitor, TrackingContent)

But this looks very strange and produces a lot of extra typing.

What do you think? Is it good style to ignore namespacing and hope not to run into a naming conflict (e.g. "Category" model for Blog app => Error) or should I prepend the apps name to each model?

Thanks in advance.

Cheers Marc

Marc
  • 6,741
  • 3
  • 19
  • 8

2 Answers2

1

I use a module as namespace ie:

module BlogModels
  class Category
  end
end

and works quite well example with dm because I've namespaced table_name, btw your way BlogCategory is also fine to me.

DAddYE
  • 1,719
  • 11
  • 16
  • Hi, thanks for your fast answer. I'm using mongoid. Putting my models into modules for namespacing sounds great, but this does not affect MongoDB collection naming and I have to specify them manually. So I thought of applying the Namespace to the model name instead of putting it into a module. Don't you think it is inconsitent to have a shop model called "Category" and a blog model "BlogCategory"? – Marc Sep 12 '11 at 14:28
0

I found a reasonable way to namespace the models in Mongoid and keep the overhead small.

I name the models like this: BlogPost, BlogComment, BlogCategory

And in the model I make use of class_name and inverse_of:

class BlogPost

  include Mongoid::Document

  # ... lots of stuff ommitted

  has_many :comments, class_name: 'BlogComment', inverse_of: :post
end

class BlogComment

  include Mongoid::Document

  # ... lots of stuff ommitted

  belongs_to :post, class_name: 'BlogPost', inverse_of: :comments
end

And access via:

post = BlogPost.first
post.comments.first # get comments

BlogComment.first.post # get related post

This keeps the access chain short and is better then:

post = BlogPost.first
post.blog_comments.first # get comments

BlogComment.first.blog_post # get related post

More details: http://mongoid.org/docs/relations.html

Marc
  • 6,741
  • 3
  • 19
  • 8