Skip to content

Commit

Permalink
fix(author): only override subclass undefined props
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed May 6, 2024
1 parent 82a8496 commit a68d616
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
27 changes: 24 additions & 3 deletions db/model/Gdoc/GdocAuthor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DEFAULT_GDOC_FEATURED_IMAGE,
OwidGdocBaseInterface,
excludeNullish,
defaults,
} from "@ourworldindata/utils"
import { GdocBase } from "./GdocBase.js"
import { htmlToEnrichedTextBlock } from "./htmlToEnriched.js"
Expand All @@ -26,9 +27,29 @@ export class GdocAuthor extends GdocBase implements OwidGdocAuthorInterface {
}

static create(obj: OwidGdocBaseInterface): GdocAuthor {
const gdoc = new GdocAuthor()
Object.assign(gdoc, obj)
return gdoc
const author = new GdocAuthor()

// We need to prevent obj methods from overriding GdocAuthor methods.
// This happens when createGdocAndInsertIntoDb() passes a GdocBase
// instance to loadGdocFromGdocBase(), instead of a simple object. In
// this case, simply assigning obj to gdoc would override GdocAuthor
// methods with GdocBase methods, in particular the
// _enrichedSubclassContent. When creating a new author,
// _enrichedSubclassContent would run from the base GdocBase class
// instead of the GdocAuthor subclass, and the socials block would not
// be shaped as an ArchieML block, thus throwing an error.
// Object.assign(gdoc, omitBy(obj, isFunction))

// However this approach still lets the parent class override properties
// of the subclass, which can be conceptually surprising.

// So instead, we choose to only override the properties and methods
// that are not defined in the subclass. Note that methods are also
// technically overriden if not defined on the subclass, although we can
// ignore it as this is redundant with inheritance mechanisms.
defaults(author, obj)

return author
}
protected typeSpecificFilenames(): string[] {
return excludeNullish([this.content["featured-image"]])
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down

0 comments on commit a68d616

Please sign in to comment.