Skip to content

Commit

Permalink
Merge pull request #3180 from tvdeyen/7.4-admin-stylesheets
Browse files Browse the repository at this point in the history
[7.4] Allow additional stylesheets to be included in the admin UI
  • Loading branch information
tvdeyen authored Feb 5, 2025
2 parents 86e276f + 573b091 commit 687a5f2
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/views/layouts/alchemy/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<%= stylesheet_link_tag('alchemy/custom-properties', media: 'screen', 'data-turbo-track' => true) %>
<%= stylesheet_link_tag('alchemy/admin', media: 'screen', 'data-turbo-track' => true) %>
<%= stylesheet_link_tag('alchemy/admin/print', media: 'print', 'data-turbo-track' => true) %>
<%= stylesheet_link_tag('alchemy/admin/custom', 'data-turbo-track' => true) %>
<% Alchemy.admin_stylesheets.each do |stylesheet| %>
<%= stylesheet_link_tag(stylesheet, 'data-turbo-track' => true) %>
<% end %>
<%= yield :stylesheets %>
<script>
// Global Alchemy JavaScript object.
Expand Down
14 changes: 14 additions & 0 deletions lib/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ def self.admin_importmaps
}])
end

# Additional stylesheets to be included in the Alchemy admin UI
#
# == Example
#
# # lib/alchemy/devise/engine.rb
# initializer "alchemy.devise.stylesheets", before: "alchemy.admin_stylesheets" do
# Alchemy.admin_stylesheets << "alchemy/devise/admin.css"
# end
#
# @return [Set<String>]
def self.admin_stylesheets
@_admin_stylesheets ||= Set.new(["alchemy/admin/custom.css"])
end

# Define page publish targets
#
# A publish target is a ActiveJob that gets performed
Expand Down
6 changes: 6 additions & 0 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class Engine < Rails::Engine
NonStupidDigestAssets.whitelist += [/^tinymce\//]
end

initializer "alchemy.admin_stylesheets" do |app|
Alchemy.admin_stylesheets.each do |stylesheet|
app.config.assets.precompile << stylesheet
end
end

initializer "alchemy.importmap" do |app|
watch_paths = []

Expand Down
26 changes: 26 additions & 0 deletions lib/alchemy/upgrader/seven_point_four.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "fileutils"
require "thor"

module Alchemy
class Upgrader::SevenPointFour < Upgrader
include Thor::Base
include Thor::Actions

class << self
def update_custom_css_config
if File.exist? "app/assets/config/manifest.js"
log "Removing alchemy/admin/custom.css from assets config file."
task.gsub_file "app/assets/config/manifest.js", %r{//= link alchemy/admin/custom.css\n}, ""
end
end

private

def task
@_task || new
end
end
end
end
1 change: 0 additions & 1 deletion lib/generators/alchemy/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def copy_yml_files
def install_assets
copy_file "all.js", app_vendor_assets_path.join("javascripts", "alchemy", "admin", "all.js")
copy_file "custom.css", app_assets_path.join("stylesheets/alchemy/admin/custom.css")
append_to_file Rails.root.join("app/assets/config/manifest.js"), "//= link alchemy/admin/custom.css\n"
end

def copy_demo_views
Expand Down
22 changes: 21 additions & 1 deletion lib/tasks/alchemy/upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace :alchemy do
desc "Upgrades your app to AlchemyCMS v#{Alchemy::VERSION}."
task upgrade: [
"alchemy:upgrade:prepare",
"alchemy:upgrade:7.3:run"
"alchemy:upgrade:7.3:run",
"alchemy:upgrade:7.4:run"
] do
Alchemy::Upgrader.display_todos
end
Expand Down Expand Up @@ -38,6 +39,14 @@ namespace :alchemy do
Alchemy::Upgrader.display_todos
end

desc "Upgrade Alchemy to v7.4"
task "7.4" => [
"alchemy:upgrade:prepare",
"alchemy:upgrade:7.4:run"
] do
Alchemy::Upgrader.display_todos
end

namespace "7.3" do
task "run" => [
"alchemy:upgrade:7.3:remove_admin_stylesheets",
Expand All @@ -56,5 +65,16 @@ namespace :alchemy do
Alchemy::Upgrader::SevenPointThree.generate_custom_css_entrypoint
end
end

namespace "7.4" do
task "run" => [
"alchemy:upgrade:7.4:update_custom_css_config"
]

desc "Update custom alchemy admin stylesheet config"
task update_custom_css_config: [:environment] do
Alchemy::Upgrader::SevenPointFour.update_custom_css_config
end
end
end
end
1 change: 0 additions & 1 deletion spec/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
//= link_tree ../images
//= link tinymce/langs/de.js
//= link_tree ../builds
//= link alchemy/admin/custom.css
11 changes: 11 additions & 0 deletions spec/libraries/alchemy/engine_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::Engine do
describe "alchemy.admin_stylesheets" do
it "includes custom css" do
expect(Rails.application.config.assets.precompile).to include("alchemy/admin/custom.css")
end
end
end

0 comments on commit 687a5f2

Please sign in to comment.