Skip to content

Commit

Permalink
Create draft version for public pages in page seeder
Browse files Browse the repository at this point in the history
If you pass a public_on date to the page seeder,
Alchemy will create a public version (because of the
public_on= attribute writer). The before_create filter
kicks in too late, leading to public pages without a
draft version.

Tried to use different before filters in the page model,
but nothing worked. So I fixed the problem where it
happens in the page seeder.
  • Loading branch information
tvdeyen committed May 26, 2023
1 parent 39a565f commit fe847d7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/alchemy/seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def user_seeds_file

def create_page(draft, attributes = {})
children = draft.delete("children") || []
page = Alchemy::Page.create!(draft.merge(attributes))
page = Alchemy::Page.new(draft.merge(attributes))
page.versions.build
page.save!
log "Created page: #{page.name}"
children.each do |child|
create_page(child, parent: page, language: page.language)
Expand Down
7 changes: 7 additions & 0 deletions spec/features/page_seeder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
expect(Alchemy::Page.find_by(name: "Footer")).to be_present
end

it "public pages have two page versions" do
seed
home_page = Alchemy::Page.find_by(name: "Home")
expect(home_page.public_version).to be_present
expect(home_page.draft_version).to be_present
end

context "when more then one content root page is present" do
let(:seeds_file) do
"spec/fixtures/pages_with_two_roots.yml"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/pages.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---

- name: Index
page_layout: index
children:
- name: Home
public_on: 2015-01-01
page_layout: standard
- name: About
page_layout: about
Expand Down

0 comments on commit fe847d7

Please sign in to comment.