So, I'm writing a service that, being in rails, I'm pretty indifferent about how people connect. I do auth at the http layer, so if people want xml or json, I could care less.
I'm overriding to_json and to_xml, but I'm using the same arguments for both. 'include this', 'don't include that', etc.
How are other people doing this to check that the behavior is the same? Maybe just set one set of arguments and use them in both methods? Or maybe have methods that verify the JSON.from_xml(myobject.to_xml) == myobject.to_json? I'd like to not have to do all my tests twice if I can help it.
So, I'm answering my own question here. Found another one like it. Has a good explanation of what I was looking for. Long story short, I'm overriding serializable_hash
http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html
How to override to_json in Rails?
PS, I'm still interested in what people are doing. Are they testing JSON and XML formats at all? Are you doing integration tests?
Related
I need to render some Rails HAML templates without a request or session.
I found this class: http://rubydoc.info/docs/rails/3.1.1/ActionView/TemplateRenderer
... but I'm not having much luck with it.
I can't figure out what to pass as the "local_context" argument to the constructor (which is defined in ActionView::AbstractRenderer).
Can someone provide a little working example of how to use this class? Many thanks!
A friend passed this link along, which turns out to work for my needs.
http://haml.info/docs/yardoc/Haml/Engine.html
I'd still be interested in knowing how to use ActionView::TemplateRenderer, though, as it seems to offer support for more formats and probably has other advantages.
A google search including the phrase "render rails view in rake task" turned up a number of solutions, of which this one seems best:
http://mamykin.com/2011/06/28/how-to-render-view-from-rake.html
I would like to design a search service which applies to a multiple set of different request paramaters. It could be called with one parameter only, or with more.
The problem is that some of the params could only be used solely, or are mutually exclusive with others.
Another problem is that some params are really required, while others are only optional.
The easiest way to resolve this is to involve a series of if-statements in the controller's search method, checking for the existence of some parameters or sets of parameters, and reacting correspondingly. Coming from the world of Java however, I have been taught to disbelieve anything that ends up in a bunch of if or switch statements. Back there, we often resort to factories, to which we delegate the decision to choose a proper search strategy from a bunch of strategy classes. All that was ever necessary, was to configure a proper mapping between request parameters and strategy classes.
I also know that the Rails dev mentality tends to solve about problem in a much less abstract and more business-like manner.
What is the proper way to make complex multi-parameter search service?
Rails 2: searchlogic
Rails 3: meta_search
Then it's as easy as Model.search(params[:search])
Sleep easy.
I'm opening up a few REST API calls to others to use. Some of them include search filters.
Let's say I have a grocery endpoint When I currently make calls I might use:
/grocery_items/index.json?types[]=fruit&types[]=deli
Leaving me with params[:types] as a nice collection.
However to make things easier for the folks consuming my API I want to be able to offer something like this:
/grocery_items/index.json?types=fruit,deli
Seems trivial to just split my params into a collection but I'm wondering if there are pitfalls since this seems to be against the grain of how rails expects collections to arrive as params.
I don't see anything wrong with doing a quick params[:types].split(',') to make calling your API easier to use. It's pretty common to do tricks with the query string, and this is a really tame change.
I am using googles protocol buffers, ruby-protobuf, with rails and ActiveRecord. The protocol itself seems like a good choice, especially since we are connecting to mobile devices. Its a very compact format.
The issues arise when having to go from proto objects to ActiveRecord objects and vice versa. Seems like there is a lot of extra work here. I would like it to work similar to the way ruby-amf works.
Anyone see this done before?
...or think there would be interest in starting work on a plugin for rails?
We did a similar thing in our project. We wrote some code to convert proto_buf to rails params hash. From there we can then access any value through params and the model can take in params. The code is not really pretty but does the job.
So I know that you shouldn't use UrlWriter methods in a model, but I wasn't sure what a better way to handle my current situation is.
Basically, I want to allow a User to post something to his facebook feed, and want to write a post_to_fb_feed(object) method in class User. Now, the URL of what is actually posted depends on the object, so I also have a to_fb_feed_item method on the object classes that a user can post. The to_fb_feed_item method just returns a hash that the Facebook API expects, including the url the post should link to.
I've gotten this to work currently, by including ActionController:UrlWriter in my models, but I was wondering if anybody had a better suggestion for how to handle this.
Thanks!
Eric
including ActionController:UrlWriter is the best way to handle it. I don't know why it's not easier to generate urls from arbitrary places in Rails -- sure it might be more common for the appropriate place to be in controllers and views, but the fact of the matter is many models validly deal with urls as data, and need to generate them.