Skip to content

Commit

Permalink
Merge pull request #2078 from tf/entry-created-hook
Browse files Browse the repository at this point in the history
Invoke hook when entry is created via admin
  • Loading branch information
tf authored Feb 20, 2024
2 parents cf3437e + 520ab37 commit efdfefd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions admins/pageflow/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ module Pageflow
end
end

after_create do |entry|
Pageflow.config.hooks.invoke(:entry_created, entry:) if entry.persisted?
end

before_update do |entry|
if entry.account_id_changed? && !authorized?(:update_site_on, resource)
entry.site = entry.account.default_site
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/admin/entries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,31 @@ def self.name
expect(Pageflow::Entry.last.permalink).to have_attributes(slug: 'custom-slug')
end

it 'invokes entry_creared hook' do
account = create(:account)
subscriber = double('subscriber', call: nil)
Pageflow.config.hooks.on(:entry_created, subscriber)

sign_in(create(:user, :manager, on: account))
post(:create, params: {entry: {title: 'some_title'}})

expect(subscriber).to have_received(:call).with(entry: kind_of(Pageflow::Entry))
end

it 'does not invoke entry_creared hook if validation fails' do
account = create(:account)
subscriber = double('subscriber', call: nil)
Pageflow.config.hooks.on(:entry_created, subscriber)

sign_in(create(:user, :manager, on: account))
expect {
post(:create, params: {entry: {title: ''}})
}.not_to(change { Pageflow::Entry.count })

expect(response.body).to have_selector('.error')
expect(subscriber).not_to have_received(:call)
end

it 'renders validation error if permalink directory belongs to different site' do
user = create(:user)
account = create(:account, with_publisher: user)
Expand Down

0 comments on commit efdfefd

Please sign in to comment.