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

Dynamic options to to_json #7

Open
gogogarrett opened this issue Oct 22, 2014 · 1 comment
Open

Dynamic options to to_json #7

gogogarrett opened this issue Oct 22, 2014 · 1 comment
Labels

Comments

@gogogarrett
Copy link

I often find myself writing code like:

  ::FullActivityRepresenter.prepare(full_activity).to_json(student: current_student)

or..

  AttemptRepresenter.prepare(attempt).to_json(wrap: :attempt)

This gem looks great, but I can't seem to figure out an easy way to send data to the to_json method.

I would imagine some changes would need to happen here and here

Any ideas - if this is already possible - to how I can get this working?

@dblock
Copy link
Member

dblock commented Oct 24, 2014

Does as_json work? I think to_json is defined by some other gem as basically as_json.to_s.

I use a custom representer to always force Grape's present ... to return JSON like so:

module Grape
  module Roar
    module Representer
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def represent(object, options = {})
          object.extend self
          # our own version of Grape::Roar::Representer
          # returns a serializable object
          object.to_json(options)
        end
      end
    end
  end
end

This is related to #3.

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

No branches or pull requests

2 participants