A quick question

I’m very new to Rails, and I’m having trouble deciding where to put some
code I wish to share across my applications, and possibly with the
comunity
at large.

Here’s the scenario:

I’m developing a personal website, to contain images, blogs, videos,
code,
etc. I’d like users to be able to comment on all these things.

Now I could create a ‘comments’ table and stick a ‘type’ field in there
and
inherit all the actual model classes from Comment. This gets the job
done,
but I wind up with a bucket load of redundant code, especially in the
controllers/views.

Am I missing some base Rails functionality here? Can I associate all my
other models to the Comment model simply? It’s entirely possible I’m
missing something easy. If so, please point it out.

Assuming I haven’t missed something really easy, I’ve been looking for a
way
to easily encapsulate the code to do comments.

I’ve created a ActiveRecord extension called ‘is_commentable’. Which is
just a thin wrapper around ‘has_many’ that adds the class name to the
type
field. I’ve added a helper that takes an object and checks to see if
it’s
‘commentable?’ and if so renders the associated comments.

Engines seem like a good fit, because I want end to end functionality.
I
want to drop in comment_engine and be able to start commenting my other
models.

I’m just in search of a sanity check. I’d like to learn to do things
the
correct way, and with something as flexible as Ruby/Rails the ‘right
way’
isn’t always clear.

Thanks in advance.

-Will

Hi William,

I’m not sure anyone could give you anything other than their ‘strong
opinion’, but from what you’ve written it sounds like what you want to
do could certainly be achieved with an engine. It could quite easily
encapsulate the helper, active record extensions and any
controllers/models.

Again, from what you’ve described It sounds like you need a mixture of
a component (providing the controller/view aspect) and a plugin (to
house the AR extension), and at the moment AFAIK an engine is the only
type of Rails extension that can provide all that in one bundle.

  • james

On 2/15/06, William G. [email protected] wrote:

inherit all the actual model classes from Comment. This gets the job done,
I’ve created a ActiveRecord extension called ‘is_commentable’. Which is
isn’t always clear.

  • J *
    ~

Thanks alot James, that makes good sense to me.

Absolutely great job on engines. I spent like 4 hours trying to
create a plugin from scratch to do the job. After 4 hours I was ready
to throw the keyboard out the window. I looked thru the plugins list
on the rails site and came across the engines entry.

I did script/plugin install engine, created a skeleton, and had my
engine working about 20 minutes later. I was pysched!

-g