Where to put (and how to require) sub-classes

I have a ThermalAnalysis class which is NOT a subclass of
ActiveRecord::Base. ThermalAnalysis has a dozen or so sub-classes. I
want to know the best places to put the files, and how to automatically
require all the subclass files.

Currently, I have:

app/models/
thermal_analysis.rb

app/models/thermal_analyses/
subclass1.rb
subclass2.rb

First, is it stylistically okay to put thermal_analysis.rb in the models
directory, considering that it’s not an ActiveRecord model? (It IS
intimately tied to a few AR models.) If not in models, then where would
you suggest?

Second, what’s the best technique for requiring all the ThermalAnalysis
subclasses? I could push a new value onto $LOAD_PATH from within
thermal_analysis.rb, as in:

$LOAD_PATH << File.expand_path("…/thermal_analyses", FILE)

…but that will only get triggered when ThermalAnalysis is first
referenced, which is usually too late. (FWIW, ThermalAnalysis is
essentially an abstract class: only its subclasses get instantiated.)

TIA.

  • ff

On Jan 5, 12:59am, Fearless F. [email protected] wrote:
subclass2.rb

First, is it stylistically okay to put thermal_analysis.rb in the models
directory, considering that it’s not an ActiveRecord model? (It IS
intimately tied to a few AR models.) If not in models, then where would
you suggest?

/models is definitely not restricted to ActiveRecord subclasses, for
example I’d 100% put mongodb models in there. You might be able to
reasonably claim that this class does model data, the data itself is
just a layer of abstraction away. If not, then lib is where I’d put
things.

Rails maps sub directories to namespaces - it will automatically look
for ThermalAnalysis::Foo in thermal_analysis/foo.rb. If you do add
something to the load path (you probably want to add it to active
support’s load path so that auto require, preloading etc. work) you
could do it from an initializer ( a file in config/initializers)

Fred

Frederick C. wrote in post #972354:

/models is definitely not restricted to ActiveRecord subclasses…

Rails maps sub directories to namespaces - it will automatically look
for ThermalAnalysis::Foo in thermal_analysis/foo.rb.

Ah! Poifect! This works like a champ:

file: app/modules/thermal_analysis/base.rb

module ThermalAnalysis
class Base … ; end
end

file: app/modules/thermal_analysis/foo.rb

module ThermalAnalysis
class Foo < ThermalAnalysis::Base … ; end
end

… etc

Thanks for the explanation and solution.

  • ff