FIX: Always serialize the correct attributes for DirectoryItems (#13510)

FIX: Always serialize the correct attributes for DirectoryItems (#13510)

diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rb
index ab82011..326fffc 100644
--- a/app/controllers/directory_items_controller.rb
+++ b/app/controllers/directory_items_controller.rb
@@ -28,7 +28,8 @@ class DirectoryItemsController < ApplicationController
 
     order = params[:order] || DirectoryColumn.automatic_column_names.first
     dir = params[:asc] ? 'ASC' : 'DESC'
-    if DirectoryColumn.active_column_names.include?(order.to_sym)
+    active_directory_column_names = DirectoryColumn.active_column_names
+    if active_directory_column_names.include?(order.to_sym)
       result = result.order("directory_items.#{order} #{dir}, directory_items.id")
     elsif params[:order] === 'username'
       result = result.order("users.#{order} #{dir}, directory_items.id")
@@ -108,6 +109,8 @@ class DirectoryItemsController < ApplicationController
       serializer_opts[:plugin_column_ids] = params[:plugin_column_ids]&.split("|")&.map(&:to_i)
     end
 
+    serializer_opts[:attributes] = active_directory_column_names
+
     serialized = serialize_data(result, DirectoryItemSerializer, serializer_opts)
     render_json_dump(directory_items: serialized,
                      meta: {
diff --git a/app/controllers/edit_directory_columns_controller.rb b/app/controllers/edit_directory_columns_controller.rb
index b80b230..b40d13c 100644
--- a/app/controllers/edit_directory_columns_controller.rb
+++ b/app/controllers/edit_directory_columns_controller.rb
@@ -30,17 +30,11 @@ class EditDirectoryColumnsController < ApplicationController
       end
     end
 
-    update_directory_item_serializer_attributes
-
     render json: success_json
   end
 
   private
 
-  def update_directory_item_serializer_attributes
-    ::DirectoryItemSerializer.attributes(*DirectoryColumn.active_column_names)
-  end
-
   def ensure_user_fields_have_columns
     user_fields_without_column =
       UserField.left_outer_joins(:directory_column)
diff --git a/app/serializers/directory_item_serializer.rb b/app/serializers/directory_item_serializer.rb
index aa833e1..c4923cb 100644
--- a/app/serializers/directory_item_serializer.rb
+++ b/app/serializers/directory_item_serializer.rb
@@ -25,21 +25,27 @@ class DirectoryItemSerializer < ApplicationSerializer
     end
   end
 
-  attributes :id,
-             :time_read
-
   has_one :user, embed: :objects, serializer: UserSerializer
-  attributes *DirectoryColumn.active_column_names
+
+  attributes :id
 
   def id
     object.user_id
   end
 
-  def time_read
-    object.user_stat.time_read
-  end
+  private
+
+  def attributes
+    hash = super
+
+    @options[:attributes].each do |attr|
+      hash.merge!("#{attr}": object[attr])
+    end
+
+    if object.period_type == DirectoryItem.period_types[:all]
+      hash.merge!(time_read: object.user_stat.time_read)
+    end
 
-  def include_time_read?
-    object.period_type == DirectoryItem.period_types[:all]
+    hash
   end
 end
diff --git a/app/serializers/directory_serializer.rb b/app/serializers/directory_serializer.rb
deleted file mode 100644
index 320e525..0000000
--- a/app/serializers/directory_serializer.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-# frozen_string_literal: true
-
-class DirectorySerializer < ApplicationSerializer
-  attributes :id
-  has_many :directory_items, serializer: DirectoryItemSerializer, embed: :objects
-
-  def id
-    object.filter
-  end
-
-end
diff --git a/spec/serializers/directory_item_serializer_spec.rb b/spec/serializers/directory_item_serializer_spec.rb
new file mode 100644
index 0000000..733048f
--- /dev/null
+++ b/spec/serializers/directory_item_serializer_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe DirectoryItemSerializer do
+  fab!(:user) { Fabricate(:user) }
+
+  before do
+    DirectoryItem.refresh!
+  end
+
+  let :serializer do
+    directory_item = DirectoryItem.find_by(user: user, period_type: DirectoryItem.period_types[:all])
+    DirectoryItemSerializer.new(directory_item, { attributes: DirectoryColumn.active_column_names })
+  end
+
+  it "Serializes attributes for enabled directory_columns" do
+    DirectoryColumn.update_all(enabled: true)
+
+    payload = serializer.as_json
+    expect(payload[:directory_item].keys).to include(*DirectoryColumn.pluck(:name).map(&:to_sym))
+  end
+
+  it "Doesn't serialize attributes for disabled directory columns" do
+    DirectoryColumn.update_all(enabled: false)
+    directory_column = DirectoryColumn.first
+    directory_column.update(enabled: true)
+
+    payload = serializer.as_json
+    expect(payload[:directory_item].keys.count).to eq(4)
+    expect(payload[:directory_item]).to have_key(directory_column.name.to_sym)
+    expect(payload[:directory_item]).to have_key(:id)
+    expect(payload[:directory_item]).to have_key(:user)
+    expect(payload[:directory_item]).to have_key(:time_read)
+  end
+end

GitHub sha: 60a76737dcbd2004960ae46cca718d916869e3ad

This commit appears in #13510 which was approved by eviltrout. It was merged by markvanlan.

This commit has been mentioned on Discourse Meta. There might be relevant details there: