RABL: change the default date and time format to iso8601

I use rabl 0.11.6 for my API, and I find that I am doing the following over and over for each date or time object:

node :created_at do |article|
  article.created_at.iso8601
end

This will save a lot of duplicate code if the format for each date object can be set in one place, so I could just use a method attributeslike this

attributes :created_at

I found a problem with github ( https://github.com/ccocchi/rabl-rails/issues/68 ) where they suggest overwriting some Oj.default_options = { mode: :compat, time_format: :ruby }in the initializer. But I have no idea how to do it .iso8601.

Then I found this https://gist.github.com/astevens/b80225d96d8889c946ac . But this looks like a hacker solution for monkey patches.

And finally, I found this problem https://github.com/nesquena/rabl/issues/336 , where they rewrite the method as_jsonfrom ActiveSupport::TimeWithZone, is also a very monkey-patch solution.

Is there no way to tell RABL which date and time format to use? And if not, what would be the best / least-hacked method to achieve this?

+4
source share
1 answer

FWIW I fixed this problem in rails 4.2.8, Rabl 0.13.1 and OJ 2.18.5

Add an initializer with the following ..

Oj.default_options = {:use_as_json => true}
+1
source

Source: https://habr.com/ru/post/1605761/


All Articles