开发者

Hashing an IP for saving, should this go to the controller or model?

In my discussion board I have a Post class. There are two special functions, which I want to know where is the best place to put them.

First, I have an author field, and the user input will be tripcoded. For example name#tripcode becomes name◆3GqYIJ3Obs (Wikipedia). I currently does it in before_save in the model, but I am wondering if this should go to the controller.

Second, I have a hashed_ip field, which basically pass the user ip with md5 and encryption. I am still working on it, but the act of setting params[:hashed_ip] with a plain ip for the model to process seems semantically wrong, but at the same time it makes the code cleaner.

If I am using other languages,开发者_JS百科 I'd simply have a constructor which will do the handle this conversion, which will be semantically better and cleaner at the same time.

I am not sure if I can obtain the IP within the model directly.

Thanks


Generally your controller should be as lean as possible and the logic for preparing the data, specially in this case, does belong in the model.

Regarding your second question, you cannot directly access the IP address from the model, that data is accesible only through the controller. Of course you could create a module/class that extended from Rack/ActionController to get that data but it's just not worth it in your case.


The thumb rule is - "Fat models, skinny controllers". Read this article for a nice explanation.

  1. Tripcoding the user's input belongs to the model. You're doing it right.
  2. If you need to calculate the hashed_ip in more than one controllers, move the function to ApplicationController
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜