Skip to content

Instantly share code, notes, and snippets.

@scottmessinger
Created January 11, 2012 17:29
Show Gist options
  • Save scottmessinger/1595745 to your computer and use it in GitHub Desktop.
Save scottmessinger/1595745 to your computer and use it in GitHub Desktop.
thoughts on Jeff's module tutorial

http://tutorials.jumpstartlab.com/topics/models/modules.html

Typically the classes would be stored in a subfolder of models with the name of the namespace, so here app/models/packager/*.rb

Isn't it required? As in, won't Rails throw an error if it doesn't find the modules in the right folder structure?

We want to extract the common code into a module, but it has to add a class method, not an instance method like the module we wrote before. There are two approaches.

You write there are two approaches then address one and then jump to a larger heading. For clarity, I'd note the two approaches. e.g. There are two approaches: using a dedicated module that holds class methods and using a module that holds both class and instance methods.

Then add in the self.included method which will tell the including class to extend itself with the ClassMethods module.

How does the child module know the parent module has been included? I think it's 10x clearer to include the self.included in the parent class a la: http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Also, this allows you to avoid .send

included In the original, we define the method callback self.included(including_class). With ActiveSupport::Concern, instead we call a class method on the module itself named included:

If I follow this correctly, included is a method defined by AS::Concern. You use the method without telling what it does or how it works. Unless it's exactly like the self.included method you wrote above (which it doesn't seem to be), I think it deserves to be explained because it's a new concept. When I see tutorials that introduce but don't explain Rails methods, I see Rails as a black box of magic. I'm left not understanding what's actually happening or all that a method can do.

I would definitely write a bit about what included does and how it differs from the method you wrote above. Specifically, I would note that it seems to pass the block to a class_eval method. Now that I think about it...is that, in fact, all included does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment