Fix board serialization when milestone is set to upcoming

parent 2956f5f5
...@@ -84,9 +84,9 @@ class Projects::BoardsController < Projects::ApplicationController ...@@ -84,9 +84,9 @@ class Projects::BoardsController < Projects::ApplicationController
def serialize_as_json(resource) def serialize_as_json(resource)
resource.as_json( resource.as_json(
only: [:id, :name, :milestone_id], only: [:id, :name],
include: { include: {
milestone: { only: [:title] } milestone: { only: [:id, :title] }
} }
) )
end end
......
...@@ -17,4 +17,18 @@ class Board < ActiveRecord::Base ...@@ -17,4 +17,18 @@ class Board < ActiveRecord::Base
super super
end end
end end
def as_json(options = {})
milestone_attrs = options.fetch(:include, {})
.extract!(:milestone)
.dig(:milestone, :only)
super(options).tap do |json|
if milestone.present? && milestone_attrs.present?
json[:milestone] = milestone_attrs.each_with_object({}) do |attr, json|
json[attr] = milestone.public_send(attr)
end
end
end
end
end end
...@@ -39,7 +39,8 @@ describe Projects::BoardsController do ...@@ -39,7 +39,8 @@ describe Projects::BoardsController do
context 'when format is JSON' do context 'when format is JSON' do
it 'returns a list of project boards' do it 'returns a list of project boards' do
create_list(:board, 2, project: project) create(:board, project: project, milestone: create(:milestone, project: project))
create(:board, project: project, milestone_id: Milestone::Upcoming.id)
list_boards format: :json list_boards format: :json
......
...@@ -6,7 +6,11 @@ ...@@ -6,7 +6,11 @@
], ],
"properties" : { "properties" : {
"id": { "type": "integer" }, "id": { "type": "integer" },
"name": { "type": "string" } "name": { "type": "string" },
"milestone": {
"id": { "type": "integer" },
"title": { "type": "string" }
}
}, },
"additionalProperties": false "additionalProperties": false
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment