Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Formats for DateTime support #212

Open
seniorihor opened this issue Apr 5, 2017 · 9 comments
Open

Custom Formats for DateTime support #212

seniorihor opened this issue Apr 5, 2017 · 9 comments

Comments

@seniorihor
Copy link

seniorihor commented Apr 5, 2017

Hi,

It doesn't seem possible to represent timestamp properties in certain (or default) format.

Steps to reproduce

config/initializers/date_format.rb

Time::DATE_FORMATS[:default] = "%Y-%m-%dT%H:%M:%S.%N%z!!!"

It converts timestamp to proper format:

song.created_at.to_s
=> "2017-04-05T19:10:47.146095000+0000!!!"

app/representers/song_representer.rb

class SongRepresenter < Roar::Decorator
  include Roar::JSON

  property :created_at
end

Actual behavior

Anyway, it renders not expected output:

SongRepresenter.new(song).to_json
=> "{\"created_at\":\"2017-04-05T19:10:47Z\"}"
# expected: "{\"created_at\":\"2017-04-05T19:10:47.146095000+0000!!!\"}"

Please, help me understand if it's a bug or find a way to achieve expected result.

System configuration

Roar version: 1.1.0

@fidalgo
Copy link

fidalgo commented Jun 5, 2017

Same here! Adding myself to the loop!

@apotonick
Copy link
Member

Try this:

property :created_at, exec_context: :decorator

def created_at; super.to_s; end

@nlsrchtr
Copy link

nlsrchtr commented Apr 6, 2018

Thanks for the input, @apotonick. I'm trying to change the format for DateTime to UNIX epoch format, depending on a request parameter. Adding your solution, doesn't work for me in this case, do you have maybe a different approach?

Thanks for your time and effort in setting up this gem and maintaining it!

@apotonick
Copy link
Member

@nlsrchtr Rendering or parsing?

@nlsrchtr
Copy link

nlsrchtr commented Apr 6, 2018

The idea was to have it both way. Currently I found a workaround, by changing the params before they hit the stack and changing:

module ActiveSupport
  class TimeWithZone
    def as_json(options = nil)
      if ActiveSupport::JSON::Encoding.use_standard_json_time_format
        xmlschema
      else
        time.strftime("%s%L").to_i
      end
    end

    def to_json(*args)
      as_json.to_json(*args)
    end
  end
end

It works, but feels way too hacky...

@apotonick
Copy link
Member

I want to vomit! 🤢

Can I see your representer class?

@nlsrchtr
Copy link

nlsrchtr commented Apr 6, 2018

🤣

The requirement is, that it should be possible to switch between "UNIX epoch format" and ISO8601 on a request base (based on some header attribute).

The call is also pretty plain and simple with:

API::V1::Me::UnitBookingRepresenter.new(current_user.unit_bookings.new).from_json(permitted_params.to_json)

The representer class looks like this

require "roar/json/json_api"
module API
  module V1
    module Me
      class UnitBookingRepresenter < Roar::Decorator
        include Roar::JSON::JSONAPI.resource :unit_bookings

        attributes do
          property :unit_id
          [...]
          property :starts_at
          property :ends_at
        end
      end
    end
  end
end

Thanks for your time and effort!

@apotonick
Copy link
Member

On a sidenote: You don't need strong_parameters with Roar/Representable, it filters automatically, the way it should be.

@apotonick
Copy link
Member

Hm, this is a JSONAPI issue and should be posted on https://github.com/trailblazer/roar-jsonapi - in pure Roar, my "trick" would be working™.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants