Ask A Question

Notifications

You’re not receiving notifications from this thread.

Opinion: Namespacing Areas of the Application

Chris Zempel asked in General

I'm working on an application related to a specific sport. A large deal of the value of the application revolves around being able to track different stats by player by position by season.

There are a lot of positions, the majority of which have unique stats. So I figured it would be simple to give each position its own model.

Now what I'm running into is that this turns my models/ folder in to a mess. I've a hypothesis that this app would be a good deal more understandable both to myself and other members on the team if we simply namespaced all the various positions under a "Position" folder.

So here's the underlying question: when is namespacing appropriate to help clarify and increase comprehension of an app, and what drawbacks does it introduce?

What's not clear to me are a couple things:

1) The effects this will have on views, links, forms and routing. As far as I can tell, if its like:

class Position::Goalie < ActiveRecord::Base
end

# then the link_to @goalie will need: position_goalie_path(@goalie)
<%= link_to "Goalie!", @goalie %>

# the form_for @goalie:
<%= form_for [:position, @goalie] do |f| %>

2) Should I namespace the tables, too?

Behavior in the controller/view layers seems similar whether or not I prepend the database table with the namespace:

module Positions
  def self.table_name_prefix
    'positions_'
  end
end

So it seems that's solely based off the name of the model. I suppose in the case of a gem that would be used across multiple applications, namespacing the db tables might make sense. But in this case, since this will be just relevant to this one app, guess that makes it a non-decision, and maybe even better to just leave the models to their original names? (ex: table name: goalies instead of positions_goalies)

The real question here, does the naming of this have any non obvious effects elsewhere in the app?

3) General question for everyone: any projects that you've worked on where you've namespaced different portions of the application, was it helpful? Did it suck?

Reply

I tend to use concerns for duplicate functionality rather than namespacing in this way. In your example, a positionable concern might be something to consider since you're talking about maximum 6 models (4 if in your domain defencemen and wingers are sufficiently similar to group together). Sorry I don't have more insight on the proposed namespacing technique.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.