Shared Model Methods

Älphä Blüë wrote:

Hi all,

I just have a question about shared model methods. I have a method that
will be used for quite a few models in my application (once I've cleaned
it up a bit). Where can I place shared model methods? I know that
controllers can use helpers or even the application_controller. But,
what do models use for a shared mechanism? Is it config/initialize?

Can someone explain a quick process for it?

Depending on how big the sharing ends up (my app is about 85% shared
functionality among models), you can always create an abstract model
class that defines those capabilities, then inherit concrete models from
that.

class GenericModel < ActiveRecord::Base
  self.abstract_class = true
  # generic implementations shared by all models
  # my app has some 30 'generic' model methods, relationship
  # management, cache management, pdf generation, etc
end

class Project < GenericModel
  # project-specific methods
  # project has only 5, 2 of which override GenericModel
  # definitions
end

You can do the same thing with controllers as well...
GenericController has all the common stuff (index, show, edit, etc, etc)
ProjectsController has just the project specific stuff.

Whatever works for you.

As a short elucidation, consider a stripped down version of the index
method in the GenericController -- all the models use this vanilla index
method from
GenericController.

  def index
    # index_setup is a routine that looks at session and params to
    # return the proper order claause, condition clause, and a filtered
    # list of ids for one or two special cases
    order, cond, filtered = index_setup session, params
    # user_rows is a user-defined rows per display table (we have widely
    # varying monitor resolutions in use at the office)
    rows = user_rows
    # since I unfortunately named one model Filter, I have to direct to
    # my filter class, not Rails' class

Glad that it's working for you.

If I could remember the site where I first read about constantize, I'd
credit that person... I'm just a messenger.

Or as an alternative resource_controller

Marnen Laibow-Koser wrote:

Älphä Blüë wrote:

Wow, that's pretty powerful stuff there.

I created a Universal Templates controller with the following test:

class UniversalTemplatesController < ApplicationController

[...]

Before you get too far with this, you might want to take a look at
make_resourceful, which encapsulates some similar features.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

I actually had looked into that yesterday when I was doing searches for
controllers. The big turnoff with it was that it uses haml. Now I know
for the most part many people love using haml because they feel it's
faster on development and cleaner. For me, it's not. Html, Xml, Css
are like walking down the street to me. I tried haml a couple of weeks
back and simply hated it. It wasn't for me. I have a photographic
memory and my brain revolves around code in a set learning process. I
don't want to reinvent the wheel to make something shorter for something
that's not broke and properly written.

Thanks for the tip though Marnen.

--- generic_models
=> nil

To preserve ARs default table naming and related conventions a model
class MUST inherit directly from ActiveRecord. This is apparently an
artifact of how the AR class is actually assembled from modules "on-
the-fly" as it were. If you do use the generic_model class then you
must provide in each descendant class the actual table name the class
should use. The project class should include this:

--->
class Project < GenericModel

   set_table_name "projects" # do not forget to pluralise the table
name!

   # project-specific methods
   # project has only 5, 2 of which override GenericModel
   # definitions
end
<---

HTH