How can I modulate a Rails model?
I am implementing several classes that have no data per se, just logic. These classes implement today's access control policy, which depends on several parameters taken from other models.
First, I try to find an answer to the question: "Where to store such classes?" here and the answer was
. This is fine, but I like to clearly separate these classes from the inherited ActiveRecord classes in a hierarchy, both in the file and in the class.
So I created classes inside a module
. I also wanted to have constants that passed between these logics. I prefer to place these constants in a module
. Thus, I did the following:
# 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 works fine with
(it passes tests I wrote without issue) but not with the development server as it cannot find the constant
I suspect it has something to do with a mismatch between the Rails autoloading schema and what I wanted to do. I tried to set up rails but with no luck ended up canceling the wrapper
Now the question I want to ask is, how can I organize these classes using Rails? I am currently using 3.2.1.
Posted the next question " How can I organize the class namespace in applications / modules with rails? "
source to share
I'm not sure if I really understand your classes, but could you create a module
or (I would rather do this :)
objects in the directory
It is not said that Model is always a descendant of ActiveRecord. If the object belongs to "business logic", then it is a model. You might have models that don't touch the database in any way. So, if your classes are "business objects", put them in "application / models" and use like any other model.
Another question is whether to use inheritance or modules , but I'd rather think about including a module in
rather than defining
in a module. Of course, this all strongly depends on the intended role of your objects.
Because in Ruby the class of an object is not important, you don't need to use inheritance. If you want to "connect" logical objects to other objects, just make sure that all "* Logic" classes have the required methods. I know that everything I've said is very vague, but I think I cannot give you more specific suggestions without knowing more about the role of these objects.
Oh, and one more thing!
If you come across autoloading Rails class, just use the old one
in all classes where you use constants
In this case, I am assuming that your problem was caused by a different boot order.
loaded up to
. The problem can go away if you create
somewhere where class autoloading can find it and define these constants in this file.
source to share
name your classes or modules using specific names. Start by extracting the logic into separate classes and then try to break them down into smaller ones. Use namespaces to distinguish them from each other in a folder
, after these steps you could extract some logical pieces to separate gems and reduce code level and application complexity. Also consider template presenter .
source to share