Skip to content

Commit

Permalink
Fix/api size (#20819)
Browse files Browse the repository at this point in the history
* config: don't transpile the lib files

* lib: don't mutate the data objects we use to instantiate Member

This has cost me a day of work but it is incredibly simple: because we
initialize our Beta::Member class with the Jekyll::Document hash, it
means doing

1. @startups = value from the hash
2. def active_startups; @startups.concat(...); end

means we're actually modifying the value from the hash since it's just
a reference being passed around. Jekyll maintains that `doc.data`
reference to build up the template but that references keeps growing
with every call to the methods `active_startups` or `legacy_startup`.

My bad! I thought `Array#concat` returned a new array...
  • Loading branch information
freesteph authored Oct 16, 2024
1 parent 88470b2 commit fd774f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exclude: [
"content/templates",
"README.md",
"CONTRIBUTING.md",
"rb",
"lib",
"vendor",
"ci",
"Gemfile",
Expand Down
15 changes: 6 additions & 9 deletions lib/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,17 @@ def legacy_missions
end

def active_startups
startups.concat(
active_missions
.map { |mission| mission['startups'] }
.flatten
(
startups +
active_missions.map { |mission| mission['startups'] }.flatten
).uniq
end

def legacy_startups
previously.concat(
legacy_missions
.map { |mission| mission['startups'] }
.flatten
(
previously +
legacy_missions.map { |mission| mission['startups'] }.flatten
).uniq
end

end
end
62 changes: 37 additions & 25 deletions spec/lib/models/member_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,54 @@
end
end

context "when the member has an active mission" do
before { data['missions'][0]['end'] = Date.current.tomorrow }
describe "#active_missions" do
context "when the member has an active mission" do
before { data['missions'][0]['end'] = Date.current.tomorrow }

it "is included in active_missions" do
expect(member.active_missions).to include data["missions"].first
end
it "is included in active_missions" do
expect(member.active_missions).to include data["missions"].first
end

it "includes its startups in active_startups" do
expect(member.active_startups).to include "aplypro"
end

it "includes its startups in active_startups" do
expect(member.active_startups).to include "aplypro"
it "does not modify the original variable" do
expect { member.active_startups }.not_to change(member.startups, :count)
end
end
end

context "when the member has an inactive mission" do
before { data['missions'][0]['end'] = Date.new(2000, 1, 1) }
describe "#legacy_startups" do
context "when the member has an inactive mission" do
before { data['missions'][0]['end'] = Date.new(2000, 1, 1) }

it "is not included in active missions" do
expect(member.active_missions).to be_empty
end
it "is not included in active missions" do
expect(member.active_missions).to be_empty
end

it "is included in the legacy_startups" do
expect(member.legacy_startups).to include "aplypro"
end
end
it "is included in the legacy_startups" do
expect(member.legacy_startups).to include "aplypro"
end

context "when there are legacy-syntax startups" do
before do
data["startups"] = ["foo", "bar"]
data["previously"] = ["bat", "man"]
it "does not modify the original variable" do
expect { member.legacy_startups }.not_to change(member.previously, :count)
end
end

it "merges them in active_startups" do
expect(member.active_startups).to contain_exactly *["aplypro", "foo", "bar"]
end
context "when there are legacy-syntax startups" do
before do
data["startups"] = ["foo", "bar"]
data["previously"] = ["bat", "man"]
end

it "merges them in active_startups" do
expect(member.active_startups).to contain_exactly *["aplypro", "foo", "bar"]
end

it "merges them in legacy_startups" do
expect(member.legacy_startups).to contain_exactly *["bat", "man"]
it "merges them in legacy_startups" do
expect(member.legacy_startups).to contain_exactly *["bat", "man"]
end
end
end
end

0 comments on commit fd774f2

Please sign in to comment.