Skip to content

Conversation

mhenrixon
Copy link
Contributor

I'd love some quick feedback on this since I really need this functionality in a current project. I do not want to have to set the key for every attribute in every serializer in the project I am working on :)

I will later add possibility of overriding the render json: @someobject, camelize: true but that got a little more complicated than I have time for right now. I still need that functionality just not critical right now.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.04%) when pulling 2f27d39 on mhenrixon:master into 919bb38 on rails-api:master.

@mhenrixon
Copy link
Contributor Author

There is one issue remaining as I discovered today. Associations won't get their keys camel cased. Any ideas where I could hook into that to make sure camelization happens?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) when pulling 6665660 on mhenrixon:master into 919bb38 on rails-api:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.23%) when pulling dba9aa4 on mhenrixon:master into 919bb38 on rails-api:master.

@spastorino
Copy link
Contributor

Can you try to implement something like this against new master and open a new pr?. If you can't no worries it is in the roadmap :).

I'm closing this issue for now but in case you want to implement it ...

We would like a generic convert_keys and conveniences like camelize_keys.

@spastorino spastorino closed this Oct 23, 2013
@mhenrixon
Copy link
Contributor Author

Ok let me try this against master then

@mhenrixon
Copy link
Contributor Author

WOW incredible improvements in master since last I checked, nice refactorings and good work everyone involved in that 👍

@spastorino
Copy link
Contributor

@mhenrixon thanks

@mhenrixon
Copy link
Contributor Author

I am almost done with a new version that works with master and in my opinion the result is a whole lot cleaner but I had a couple of questions.

  1. I want to enable a global setting like for root ActiveModel::Serializer.convert_keys :lower_camelcase however I can't make that passed down to the serializer when it in my current project. (yes I haven't written any tests yet because of number 2 below :)
  2. Where do you want the tests for this? Should I create new test files for this or add it to the attributes_test.rb?

@spastorino
Copy link
Contributor

About 2. I'd add a new file named something like test/unit/active_model/serializer/conversions_test.rb
About 1 you don't need a setting I'd implement it similar to the way filter works.

@mhenrixon
Copy link
Contributor Author

That is actually a great suggestion but don't that mean I would have to duplicate this effort for all my 20 serializers? I could of course create a base serializer that the others inherit from but it still feels wrong.

@mhenrixon
Copy link
Contributor Author

FYI the global settings are broken in master.

ActiveSupport.on_load(:active_model_serializers) do
  # Disable for all serializers (except ArraySerializer)
  ActiveModel::Serializer.root = false

  # Disable for ArraySerializer
  ActiveModel::ArraySerializer.root = false
end

Neither of those work for me using the master branch locally. It still adds the root elements to be serialized.

@spastorino
Copy link
Contributor

@mhenrixon there's no run_load_hooks inside AMS because there's no lazy load. So the code you pasted doesn't work. All you have to do is put

  # Disable for all serializers (except ArraySerializer)
  ActiveModel::Serializer.root = false

  # Disable for ArraySerializer
  ActiveModel::ArraySerializer.root = false

Inside an config/initializers/something.rb

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