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

fix: allow using active_model_serializers in associations (compat) #12

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/oj_serializers/compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,53 @@ def self.one(object, options = nil)
def self.many(array, options = nil)
array.map { |object| new(object, options) }
end

# OjSerializer: Used internally to write a single object association in :hash mode.
#
# Returns nothing.
def self.one_as_hash(object)
new(object)
end

# OjSerializer: Used internally to write an association in :hash mode.
#
# Returns nothing.
def self.many_as_hash(array)
array.map { |object| new(object) }
end

# OjSerializer: Used internally to write a single object association in :json mode.
#
# Returns nothing.
def self.write_one(writer, object)
writer.push_value(new(object))
end

# OjSerializer: Used internally to write an association in :json mode.
#
# Returns nothing.
def self.write_many(writer, array)
writer.push_array
array.each do |object|
write_one(writer, object)
end
writer.pop
end

module OjOptionsCompat
def add_attribute(value_from, key: nil, **options)
options[:as] ||= key if key

if (unless_proc = options.delete(:unless))
options[:if] = -> { !instance_exec(&unless_proc) }
end

super(value_from, **options)
end
end
end

require 'oj_serializers'
require 'oj_serializers/sugar'

Oj::Serializer.singleton_class.prepend(ActiveModel::Serializer::OjOptionsCompat)
45 changes: 45 additions & 0 deletions spec/oj_serializers/compat_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'spec_helper'

require 'support/models/album'
require 'support/serializers/active_model_serializer'

class CompatSerializer < Oj::Serializer
has_one :item, key: :album, serializer: ActiveModelSerializer
has_many :items, serializer: ActiveModelSerializer, unless: -> { options[:skip_collection] }
end

class JsonCompatSerializer < CompatSerializer
default_format :json
end

RSpec.describe 'AMS Compat', type: :serializer do
def expect_encoded_json(object)
expect(Oj.dump(object).tr("\n", ''))
end

it 'can use ams serializer in associations' do
album = Album.abraxas.tap { |a| a.id = 1 }
object = double('compat', item: album, items: [album, album])
attrs = { id: 1, name: 'Abraxas' }

expect_encoded_json(CompatSerializer.one(object)).to eq({
album: attrs,
items: [attrs, attrs],
}.to_json)

expect_encoded_json(CompatSerializer.one(object, skip_collection: true)).to eq({
album: attrs,
}.to_json)

expect_encoded_json(JsonCompatSerializer.one(object)).to eq({
album: attrs,
items: [attrs, attrs],
}.to_json)

expect_encoded_json(JsonCompatSerializer.one(object, skip_collection: true)).to eq({
album: attrs,
}.to_json)
end
end