Hey Richard, I feel your pain on this one. I've recently tried various at JSON solutions and been underwhelmed.
One of the main issues, I reckon is that MVC doesn't say much about where to put API stuff like this. It shouldn't be in the controller, because it's not about "controlling" it's more about exposing model stuff in a particular format... ... but it's not really model stuff either, becuase differennt use case often want different subsets of data form the model, and you shouldn't bury use-case-context stuff in a model ... ... so it should be in the view: an API is a view into your app ... ... but I've come to realise that rails view templates aren't a very good way to implement API "views". For example, RABL templates strokes me as quite odd and uninituitive. It's also a pain to implement inheritance and polymorphism in a templating format and keep it zimming along at a decent rate. So I reckon implementing APIs in Presenter Pattern is the way to do it. I haven't actually implemented this yet (I'm planning it to be a Rails Camp project) but I"m seeing a presenter modules sitting between the controllers and their target models. The presenters can know there use case context and would probably evolve a standrad set of methods to cater for different patterns. Each of the methods that implement these patterns can collaborate with the as_json method in the corresponding model. As you develop, you can abstract all the common goodness into a general Presenter module and have the specific presenters include it. Apologies that I don't have some more concrete code at this point. No doubt, there'll be beasties and angels in the details. regards On 20 October 2012 14:38, Richard McGain <mcg...@gmail.com> wrote: > Hi all, > > I have just inherited a codebase and I am attempting to clean it up a bit. > I just can't figure out what to do with some controllers serving up json. > > This pattern is repeated in a bunch (10+) of controllers > > def index > users = User.find(:all) > @json_response_wrapper[:response][:users] = users > render json: @json_response_wrapper.as_json > end > > @json_response_wrapper is set by a :before_filter using this code: > > def create_json_response_structure > @json_response_wrapper = {error:{code: 0,message: ""}, response:{}} > end > > The users = User.find(:all) bit obviously changes from controller to > controller, but the other two lines are pretty much identical. > > I have looked at a few gems for handling json (such as > jsonify<https://github.com/bsiggelkow/jsonify> > , rabl <https://github.com/nesquena/rabl> and > acts_as_api<https://github.com/fabrik42/acts_as_api/>) > but I'm not convinced they solve my problems (as the json formatting of the > objects themselves is very basic). Actually, having said that, each model > defines as_json with something like > > def as_json > super(only: [:first_name, :last_name], methods: [:blah]) > end > > so that behaviour is in the wrong spot anyway. I still don't think this > justifies the use of some kind of template though (although I could be > convinced otherwise). > > The obvious answer I guess, is to just define a method in > ApplicationController that looks something like > > def response_for(entity_name, collection) > json_response_wrapper = {error:{code: 0,message: ""}, response:{}} > json_response_wrapper[:response][entity_name] = collection > json.response_wrapper.as_json > end > > and then in the controller > > render json: response_for(:users, users) > > but I feel like I am missing an opportunity if I just do that. Am I just > hiding a bad design by doing this? > > Does anyone have any better suggestions? > > Thanks in advance, > > Richard > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to rails-oceania@googlegroups.com. > To unsubscribe from this group, send email to > rails-oceania+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. > -- *Mark Ratjens* *Co-founder, Habanero Software* * * Sydney, Australia mark.ratj...@habanerohq.com @MarkRatjens <mark.ratj...@habanerohq.com> www.habanerohq.com <http://habanerohq.com> +61 414 159 357 -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to rails-oceania@googlegroups.com. To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.