Rails: helpers and models - where to organize your code
More and more, I am putting all my code in models and helpers regarding MVC.
However, sometimes I'm not sure where to organize the code. Should he enter the model or must go to the assistant. What are the benefits of each one. Faster or they are the same. I've heard a thing or two about all the models getting cached, so it seems like it might be better to accommodate most of my code then.
For example, here's a script that runs in a model or helper:
def status
if self.purchased
"Purchased"
elsif self.confirmed
"Confirmed"
elsif self.reserved
"Reserved"
else
"Pending"
end
end
I don't need to save this status as in a database because there are boolean fields for purchase and confirmation and are reserved. So why put this in the model or why put it in the helper?
So, I'm not sure about the best practice or the benefits to get from putting code into the model or into the helper if it can be in both.
a source to share
Your specific example includes a business rule in the sense that if a model instance is both purchased and confirmed, then the corresponding status is "purchased" not "confirmed"
So, in your example, I would definitely put this method in the model as it encodes one of the business rules of your applications.
Another example:
def status_string
case status
when 0: "Purchased"
when 1: "Confirmed"
else
"Pending"
end
end
In this case, the status_string method can be definitely defined in both the helper and the model - it has nothing to do with any business rules, it changes the representation of the value. I would put it in the Model as I am only trying to put html related sw in View Helpers. But depending on your internationalization scheme, a similar method might be better placed in a view helper.
A good example of a view helper is an application-level method for converting date time values to a standard representation for your application. For instance,
# application_helper.rb
def date_long_s(d)
d.strftime("%A, %b *%d, %Y *%I:%M %p")
end
a source to share
This is really subjective and I agree, it is sometimes unclear if something in the model or helper belongs.
For instance:
# using model
status ? status.nice_name : "Pending"
# using helper
nice_name(status)
The clear advantage for the helper is that it can handle null objects gracefully keeping the look clean. The downside is that the code is now in a different location from the model
The performance is reasonable, you won't see much difference between using helpers and models. Most likely bottlenecks in DB flights for pulling status objects will be bottlenecks.
a source to share
I use constant hashes in situations like this.
The hash is defined in a model file like this
STATUS = {
1 => "Pending",
2 => "Confirmed"
}
I also declare constants for each such state.
ST_PENDING = 1
This declaration is useful when writing queries. For instance,
MyModel.all(:status=>ST_PENDING)
the status field in the database table is number.So when printing, I just use that.
MyModel::STATUS[obj.status]
a source to share