tags:

views:

25

answers:

2

The framework I'm using on my project follows the MVC Pattern. I"M building JSON feeds and need to structure them in a different way then what the system gives me by default from the ORM. Where should I be handling the task of mangling and shaping my data that I'll serve up, in the model, view or controller?

Right now I'm doing it in my controller, then passing that data to the view. I can see this fitting better under the Model or the View but not sure which one.

A: 

I'd write a model method to do it if I were you. Having it in the controller will make your controller fat, which is bad, and means you can't call that functionality from other controller actions or anywhere else. Although it could be considered presentation logic, I prefer to keep my views really simple with just conditionals and iterators at most. There may be an argument for having it in a helper, but I'd still stick with the model.

neilcrookes
I see you point and completely agree with you about keeping it out of the controller. However, I don't see why I would ever need to call that functionality again except for when I'm being asked for that feed. It's nice having it all in the controller since it's already doing my SQL queries, it would be a pain to have to edit two files to A) get the data I want from the database in my controller and B) reformat that date in a view to get it the way I want, but I guess that's the MVC way?
Brian Wigginton
+1  A: 

If this different structure is only relevant to the view, you should keep it in the view.
If this structure is used in more than one view, make a Helper for it.

Internally your app should standardize on one data format, so models should always return a standardized format. If you were to do something with that data in your controller, you'd need to change the logic for interacting with the data just in that one controller function, which in this case doesn't make much sense. If you later decide to change the format in the model, you'd also need to change code in the controller that interacts with it. Don't create dependencies when there's no advantage to do so.

deceze
I'm going to go this direction, thanks for your input. This makes since as this restructuring is only done for feed output.
Brian Wigginton