Skip to content

Conversation

mhenrixon
Copy link
Contributor

This fixes that I could not turn off root elements in my json serialization.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) when pulling 5d0add3 on mhenrixon:master into 74d19e7 on rails-api:master.

@spastorino
Copy link
Contributor

This should be better moved to configs. I think @arthurnn was going to do something like that.

@mhenrixon
Copy link
Contributor Author

You mean something like:

ActiveModel::Serializer.setup do |config|
  config.root = false
  config.array_root = false
end

Doesn't that mean the array serializer runs into some serious ugliness?

# lib/array_serializer.rb
module ActiveModel
  class ArraySerializer
    include Serializable

    def initialize(object, options={})
      @object          = object
      @root            = options[:root]
      # *Below gets real ugly*
      @root            = ActiveModel::Serializer::CONFIG.array_root if @root.nil?
      @root            = options[:resource_name] if @root.nil?
      @meta_key        = options[:meta_key] || :meta
      @meta            = options[@meta_key]
      @each_serializer = options[:each_serializer]
      @options         = options.merge(root: nil)
    end
end

The alternative isn't pretty either, having the following code makes the code base unnecessary complex imho.

ActiveModel::Serializer.setup do |config|
  config.root = false
end

ActiveModel::ArraySerializer.setup do |config|
  config.root = false
end

The current Config class is locked to Serializer so it can't be reused and I don't see any real benefit in having it dynamic as it is now. I can't fathom a scenario where AMS developers need to provide a way to set variables they don't know the name of up front.

I am always available on Skype as mhenrixon or send me an email mikael at zoolutions dot se feel like it might be a good idea to discuss some general design thoughts before trying to help further.

mkrisher and others added 8 commits October 31, 2013 20:06
This commit adds a convert_hash method during the serialization
process. It can be overwritten to support converting keys to any
format. Two helper methods are included, one for converting the
serialized hash keys to camelcase and another to convert them to upcase.
This commit removes the commenter poro and updates the post poro to
include snakecase attrs (created_at and updated_at) to test converting
keys to various formats such as camelizing.
This commit updates the tests to use the Post poro rather than
introducing a new Commenter poro.
This commit updates the test to expect the same key conversion for
associations as the parent. This required passing the convert type from
the parent to the associations via the serializer method. If it is
included in the options, it is set during initialize.
This commit removes the to_sym when converting keys. Tests were updated.
This commit applies the conversion to the root key the same as the
model convert type.
Add @options back into serializers for passing custom options
@aaronchi
Copy link

aaronchi commented Dec 2, 2013

The documentation needs to be updated in the Readme. The intializer method doesn't seem to work at all.

Subclassing the serializer works but not as documented.

You have to use

def root
  false
end

instead of

self.root = false

@spastorino
Copy link
Contributor

Fixed in 700e6b8

@spastorino spastorino closed this Dec 16, 2013
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.

6 participants