From 65e10224fe570c97f908ff1d3a9b044ece2d2fe3 Mon Sep 17 00:00:00 2001 From: Filippos Vasilakis Date: Wed, 21 Oct 2015 09:23:47 +0200 Subject: [PATCH] Pass fields down from constructor --- lib/active_model/serializer/adapter/attributes.rb | 1 + test/adapter/json/collection_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/active_model/serializer/adapter/attributes.rb b/lib/active_model/serializer/adapter/attributes.rb index 15577eb8e..a3843e510 100644 --- a/lib/active_model/serializer/adapter/attributes.rb +++ b/lib/active_model/serializer/adapter/attributes.rb @@ -9,6 +9,7 @@ def initialize(serializer, options = {}) def serializable_hash(options = nil) options ||= {} + options[:fields] = instance_options[:fields] if options[:fields].nil? if serializer.respond_to?(:each) serializable_hash_for_collection(options) diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb index 3b9e4b019..5d85c3eb4 100644 --- a/test/adapter/json/collection_test.rb +++ b/test/adapter/json/collection_test.rb @@ -85,6 +85,20 @@ def test_include_option assert_equal(expected, actual) end + + def test_fields_with_no_associations_include_option + actual = ActiveModel::SerializableResource.new( + [@first_post, @second_post], adapter: :json, fields: [:id], include: [] + ).as_json + + expected = { posts: [{ + id: 1 + }, { + id: 2 + }] } + + assert_equal(expected, actual) + end end end end