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