Reputation: 4922
I'm implementing several classes which does not have data by itself, just logics. These classes implements access control policy to date which depends on several parameters taken from data from other models.
I initially try to find answer to "Where to store such classes?" here, and the answer was apps/models
directory. That's ok, but I like to clearly separate these classes from ActiveRecord inherited classes in hierarchy, both as file and class.
So, I created classes inside Logic
module, like Logic::EvaluationLogic
or Logic::PhaseLogic
. I also wanted to have constants which passed between these logics. I prefer to place these constants into Logic
module too. Thus, I implemented like this:
# in logic/phase_logic.rb
module Logic
PHASE_INITIAL = 0
PHASE_MIDDLE = 1000
class PhaseLogic
def self.some_phase_control_code
end
end
end
# in logic/evaluation_logic.rb
module Logic
class EvaluationLogic
def self.some_other_code
Logic::PhaseLogic.self.some_phase_control_code(Logic::PHASE_INITIAL)
end
end
end
Now, it work just fine with rspec
(It passes tests I wrote without issues), but not with development server, since it can't find the Logic::PHASE_INITIAL
constant.
I suspect it's related to the mismatch of the autoloading scheme of Rails and what I wanted to do. I tried to tweak rails, but no luck, ended-up with eliminating module Logic
wrap.
Now the question I want to ask: How I can organize these classes with Rails? I'm using 3.2.1 at this moment.
Posted a follow-up question "How I can organize namespace of classes in app/modules with rails?"
Upvotes: 2
Views: 1191
Reputation: 5192
Don't name your classes or modules Logic
use specific names. Start with extracting logic into separate classes and then try to break them into smaller ones. Use namespaces to distinguish them from each other in lib
folder, after this steps you would be able to extract some logic parts to separate gems and reduce codebase and complexity of application. Also take a look into presenter pattern.
Upvotes: 0
Reputation: 12820
I am not sure whether I really understand your classes, but couldn't you create a Logic
module or (I would rather do this:) PhaseLogic
and EvaluationLogic
objects in /lib
directory?
It is not said that "Model" is always descendant of ActiveRecord. If the object belongs to "business logic" then it is a model. You can have models which do not touch database in any way. So, if your classes are "business objects", place them in 'app/models' and use like any other model.
Another question is whether you should use inheritance or modules - but I would rather think about including a module in PhaseLogic
, and not about defining PhaseLogic
in a module. Of course, all this depends heavily on the intended role of your objects.
Because in Ruby the class of object is not important, you do not need to use inheritance. If you want to 'plug' the logic objects into other objects, just take care that all '*Logic' classes have the required methods. I know that all I said is very vague, but I think I cannot give you some more concrete suggestions without knowing more about the role of these objects.
Ah, and one more thing!
If you find yourself fighting with Rails class autoloading, just use the old require "lib/logic.rb"
in all the classes where you are using Logic::PHASE_INITIAL
constants.
In this case I suppose that your problem was caused by different order of loading. The logic/evaluation_logic.rb
has been loaded before logic/phase_logic.rb
. The problem may disappear if you create logic.rb
somewhere, where class autoloading can find it, and define these constants in that file.
Upvotes: 1