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

rename context to serialization_context and add url helpers #1289

Merged
merged 1 commit into from
Oct 29, 2015

Conversation

tchak
Copy link
Contributor

@tchak tchak commented Oct 21, 2015

No description provided.

@tchak
Copy link
Contributor Author

tchak commented Oct 21, 2015

I am not sure about the best way to include url_helpers in the context object. If some people with more knowledge about rails internals have a better idea I am all 👂 :)

@tchak
Copy link
Contributor Author

tchak commented Oct 21, 2015

I also feel that in the end serialization_context should be exposed just like scope directly on the serializer instances. I would like to add serialization_context_name just like for scope and expose it as context by default.


def self.default_url_options
ActionMailer::Base.default_url_options
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not include the url helpers and instead stick 'em in a url_helpers method

We could probably set it and default_url_iptions in the railtie.

Also, i don't believe we include action mailer nor do we need it. There should be a similar methid on the controller

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a context test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry about ActionMailer this is me trying things by copy/pasting from SO in a hurry :( Will look in to how do it from the railtie!

@bf4
Copy link
Member

bf4 commented Oct 21, 2015

Awesome work!

@tchak tchak force-pushed the serialization_context branch 3 times, most recently from b5f42fa to 88617c7 Compare October 22, 2015 00:31
@beauby
Copy link
Contributor

beauby commented Oct 22, 2015

Pretty cool 👍

@NullVoxPopuli
Copy link
Contributor

LGTM 👍

@NullVoxPopuli
Copy link
Contributor

@tchak I'm good with merging this, unless you think serialization_context_name should be included in this PR.. maybe a different PR? All tests pass, so... :-)

@tchak
Copy link
Contributor Author

tchak commented Oct 22, 2015

@NullVoxPopuli let me add a few more tests tonight. Yes serialization_context_name will go in a separate PR.

@NullVoxPopuli
Copy link
Contributor

cool. thanks!

@tchak tchak force-pushed the serialization_context branch from 88617c7 to d08ee59 Compare October 22, 2015 20:29
module ActionController
module Serialization
class Context
attr_reader :request_url, :query_parameters, :url_helpers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather have url_helpers a class method on the Context. It feels more natural here. Also, no need to include them. you could just

class Context
  class << self
    attr_accessor :url_helpers, :default_url_options
  end
end

and in the Railtie

Context.url_helpers = Rails.application.routes.url_helpers # for now, later would like to encapsulate it to control the boundary e.g. UrlHelpers.new(Rails.application.routes.url_helpers)
Context.default_url_options =  ActionController::Base.default_url_options

I'm trying to keep in mind how someone using grape would configure this. If we keep a good interface, the details of the implementation don't matter as much. That means more composition and less just mixing things in. Alternatively, there could be a UrlHelpers module that you mix in to the Context when the railtie is called, which allows others to mix in a different helper object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm as far as I understand default_url_options needs to be defined on the same object where url_helpers are included in order to be taken in to account. Am I not understanding it correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I usually use url helpers in serialisers a lot, so I am thinking that at some point I would like to be able to do in some serializer code something like this:

def validation_url
  context.validation_url(object)
end

or at least

def validation_url
  context.url_helpers.validation_url(object)
end

Are we on the same page here? Or do you see the usage differently?

@tchak
Copy link
Contributor Author

tchak commented Oct 22, 2015

I am trying to add some tests to url_helpers thing and I feel a bit confused :) I am going to need a better understanding of how rails routes works and compose with everything else... I hope to be able to figure it out in the next few days.

@beauby
Copy link
Contributor

beauby commented Oct 29, 2015

In my opinion this PR is worth merging without url_helpers (that we could add later).

@NullVoxPopuli
Copy link
Contributor

I agree, @beauby

NullVoxPopuli added a commit that referenced this pull request Oct 29, 2015
rename context to serialization_context and add url helpers
@NullVoxPopuli NullVoxPopuli merged commit a063cbe into rails-api:master Oct 29, 2015
@beauby
Copy link
Contributor

beauby commented Oct 29, 2015

Wait, it wasn't ready though (there were references to url_helpers and stuff). Could you revert the merge until we clear it for merging?

@NullVoxPopuli
Copy link
Contributor

that's what I get for not re-reviewing. ha

@bf4 bf4 mentioned this pull request Oct 30, 2015
@bf4
Copy link
Member

bf4 commented Oct 30, 2015

@tchak would you mind re-submitting this PR, referencing it, and referring to the comments in #1289 (comment) ? Maybe skip the do it in two parts, without the url helpers, then adding them

@beauby
Copy link
Contributor

beauby commented Apr 20, 2016

So in the end, how does one access the url_helpers from within an attribute definition in a serializer?
As follows?

attribute :url do
  serialization_context.url_helpers.posts_route
end

@bf4
Copy link
Member

bf4 commented Apr 20, 2016

I think I left this open for the uri templates

The serialization context url helpers are mixed into Link. Examples in docs

B mobile phone

On Apr 20, 2016, at 8:22 AM, Lucas Hosseini notifications@github.com wrote:

So in the end, how does one access the url_helpers from within an attribute definition in a serializer?
As follows?

attribute :url do
serialization_context.url_helpers.posts_route
end

You are receiving this because you commented.
Reply to this email directly or view it on GitHub

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

Successfully merging this pull request may close these issues.

4 participants