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

Allow Configurable Serializer Function (Solves #153) #155

Merged
merged 1 commit into from
Jul 31, 2015
Merged

Allow Configurable Serializer Function (Solves #153) #155

merged 1 commit into from
Jul 31, 2015

Conversation

jordanstephens
Copy link
Contributor

Here's a first pass on a configurable serializer for issue #153.

Usage is as described in that issue.

JsRoutes.setup do |config|
  config.serializer = "myApp.myCustomSerializerFn"
end

Also note that I removed a bunch of "if jQuery present" specs as they should no longer be relevant.

If everything looks good to you guys, I'll update the readme.

Let me know if you guys have any questions.

Thanks!

@@ -19,7 +19,8 @@ class JsRoutes
url_links: nil,
camel_case: false,
default_url_options: {},
compact: false
compact: false,
serializer: "this.default_serializer"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should use nil here. This value feels too magical if someone will try to debug it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bogdan If we set the default to nil here, we will have to do a check somewhere else to set the default initializer. It has to get set somewhere, we can't just have a nil initializer floating around. Where would you prefer it get set?

Something like this on line 108?

js.gsub!("SERIALIZER", @options[:serializer] || "this.default_serializer")

I think setting in the defaults is better than this option. This feels like a magic value. And really, this.default_serializer is the default—not nil. It doesn't make sense to have a nil initializer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The way we can make it clear is to make default_serializer a public method and let default be JsRoutes.default_serialize. In this way people can clearly see where is the default and even test in console it in console comparing to other serializers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bogdan I'm not totally sure I follow. How does this work with the configurable namespace? We can't set the default to a public method on JsRoutes because we don't know that JsRoutes exists.

It might be better to just make default_serializer public when we run createGlobalJsRoutesObject, and then just add a README note that it is public on the namespace that the user defines, rather than try to get the default value to include the name of the configurable namespace.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, you are right. namespace config value makes it not static. Let me think till tomorrow.

Copy link
Member

Choose a reason for hiding this comment

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

js.gsub!("SERIALIZER", @options[:serializer] || "null")

and in coffeescript:

serializer = SERIALIZER
unless serializer?
  #use default

Copy link
Collaborator

Choose a reason for hiding this comment

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

I still vote for nil as default and implementation as @le0pard suggested.

@jordanstephens
Copy link
Contributor Author

@le0pard @bogdan alright, I set the default to nil, and moved the override logic into the javascript.

I think you guys are making the wrong call setting the default value to be a nonsense nil value rather than the actual default, and then hiding the override logic in the javascript. I don't see the benefit to the increased complexity in doing it this way. However, as it's your project, I'll follow your lead here.

I hope we can get this merged in soon.

Thanks

@le0pard
Copy link
Member

le0pard commented Jul 8, 2015

Funny, @jordanstephens . You think line "this.default_serializer" should mean something for developers, but one condition can slowdown all codebase :)

I think to not set initializer each time in function variable with serializer, we can cache it in Utils in this way (also better check, what this defined serializer is really function, and not something else):

custom_serializer: SERIALIZER

serialize: (object) ->
  if @custom_serializer? and @get_object_type(@custom_serializer) is "function"
    @custom_serializer(object)
  else
    @default_serializer(object)

bogdan added a commit that referenced this pull request Jul 31, 2015
Allow Configurable Serializer Function (Solves #153)
@bogdan bogdan merged commit 2b42621 into railsware:master Jul 31, 2015
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.

3 participants