Skip to content

Commit

Permalink
Merge pull request #1957 from tvdeyen/publish-targets
Browse files Browse the repository at this point in the history
Add Publish Targets
  • Loading branch information
tvdeyen committed Nov 2, 2020
2 parents 3b4a8fe + 58de2a5 commit 50f6437
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def visit
def publish
# fetching page via before filter
@page.publish!

# Send publish notification to all registered publish targets
Alchemy.publish_targets.each { |p| p.perform_later(@page) }

flash[:notice] = Alchemy.t(:page_published, name: @page.name)
redirect_back(fallback_location: admin_pages_path)
end
Expand Down
29 changes: 29 additions & 0 deletions lib/alchemy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Alchemy
YAML_WHITELIST_CLASSES = %w(Symbol Date Regexp)

# Define page publish targets
#
# A publish target is a ActiveJob that gets performed
# whenever a user clicks the publish page button.
#
# Use this to trigger deployment hooks of external
# services in an asychronous way.
#
# == Example
#
# # app/jobs/publish_job.rb
# class PublishJob < ApplicationJob
# def perform(page)
# RestClient.post(ENV['BUILD_HOOK_URL'])
# end
# end
#
# # config/initializers/alchemy.rb
# Alchemy.publish_targets << PublishJob
#
def self.publish_targets
@_publish_targets ||= Set.new
end
end
5 changes: 2 additions & 3 deletions lib/alchemy_cms.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

# Instantiate the global Alchemy namespace
module Alchemy
YAML_WHITELIST_CLASSES = %w(Symbol Date Regexp)
end
require "alchemy"

# Require globally used external libraries
require "acts_as_list"
Expand Down
38 changes: 38 additions & 0 deletions spec/controllers/alchemy/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::Admin::PagesController do
routes { Alchemy::Engine.routes }

before do
authorize_user(:as_admin)
end

describe "#publish" do
let(:page) { create(:alchemy_page) }

it "publishes the page" do
expect_any_instance_of(Alchemy::Page).to receive(:publish!)
post :publish, params: { id: page }
end

context "with publish targets" do
class FooTarget < ActiveJob::Base
def perform(_page)
end
end

around do |example|
Alchemy.publish_targets << FooTarget
example.run
Alchemy.instance_variable_set(:@_publish_targets, nil)
end

it "performs each target" do
expect(FooTarget).to receive(:perform_later).with(page)
post :publish, params: { id: page }
end
end
end
end

0 comments on commit 50f6437

Please sign in to comment.