Concerns vs libraries

Hey guys,

I think I might be a little confused but I cant really understand exactly when to separate code into a concern, and when to separate it into a library.

  1. What’s the exact difference?

  2. Where are they available?

  3. What is their goal? what is the goal of putting code in a concern and in a library?

Hope i am not saying something completely dumb, but I couldn’t really find a comparison and I guess I am pretty new to the concept of concerns.

all the best,

Andre

unknown wrote in post #1116591:

I think I might be a little confused but I cant really understand
exactly
when to separate code into a concern, and when to separate it into a
library.

1. What's the exact difference?

Concerns is a technique used to either share some behavior between
models or to break down "chubby" models into smaller, more coherent
pieces. Notice this is all about model objects.

Library code on the other hand should be self contained, independent
behaviors that can be use anywhere.

2. Where are they available?

Model Concerns: Any model object where you choose to include them.
Notice that concerns "extend ActiveSupport::Concern" and therefore have
that direct dependency.

Controller Concerns: Similar to the model concerns except for
controllers.

Library: Should have no direct dependencies outside of itself (other
than libraries a library directly depends upon. i.e Rails itself has
many dependencies managed by RubyGems).

You should (in theory) be able to include a library anywhere, even in
apps that don't use Rails at all. Notice that many gems (libraries) are
useful no matter what framework you use them with. You could have a
Sinatra app and include the same gem as in a Rails app. There's no
direct dependency there.

3. What is their goal? what is the goal of putting code in a concern and
in
a library?

This explains concerns pretty well I think:
http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns

I have read the post. actually I have it read it several times to try to see if I could find the particular answer.

But I guess I got my question answered. Independent is the key word here and that’s what a library should be and the ability of using it anywhere. I think I just saw a lot of bad examples of how to use libraries. I was also getting confused since you can have concerns for models and controllers and that is almost everything in rails. But well I think I got a pretty good idea and will have to make design choices on my own for that case.

But I still have the feeling that this can confuse much more people since it was introduced with one blog post and there is not big documentation about it on rails.(Concerns I mean)

But hopefully we will get more information and new ways to use it!

Thanks for your help,

all the best,

Andre