From 94a012391b67b81d5f8da16a172cc831d7112ef4 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 8 May 2020 15:15:05 +0200 Subject: [PATCH] Use Rails app root to join paths Without that it is not always ensured that the path is the hosts apps root path. --- .../alchemy/install/install_generator.rb | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/generators/alchemy/install/install_generator.rb b/lib/generators/alchemy/install/install_generator.rb index 465f3a16e7..dea0c2b447 100644 --- a/lib/generators/alchemy/install/install_generator.rb +++ b/lib/generators/alchemy/install/install_generator.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require "rails" +require "rails/generators" module Alchemy module Generators @@ -14,41 +14,41 @@ class InstallGenerator < ::Rails::Generators::Base source_root File.expand_path("files", __dir__) def copy_config - copy_file "#{config_path}/config.yml", "config/alchemy/config.yml" + copy_file "#{gem_config_path}/config.yml", app_config_path.join("alchemy", "config.yml") end def copy_yml_files %w(elements page_layouts menus).each do |file| - template "#{__dir__}/templates/#{file}.yml.tt", "config/alchemy/#{file}.yml" + template "#{__dir__}/templates/#{file}.yml.tt", app_config_path.join("alchemy", "#{file}.yml") end end def install_assets - copy_file "all.js", "vendor/assets/javascripts/alchemy/admin/all.js" - copy_file "all.css", "vendor/assets/stylesheets/alchemy/admin/all.css" + copy_file "all.js", app_vendor_assets_path.join("javascripts", "alchemy", "admin", "all.js") + copy_file "all.css", app_vendor_assets_path.join("stylesheets", "alchemy", "admin", "all.css") end def copy_demo_views return if @options[:skip_demo_files] - copy_file "application.html.erb", "app/views/layouts/application.html.erb" - copy_file "article.scss", "app/assets/stylesheets/alchemy/elements/article.scss" + copy_file "application.html.erb", app_views_path.join("layouts", "application.html.erb") + copy_file "article.scss", app_assets_path.join("stylesheets", "alchemy", "elements", "article.scss") stylesheet_require = " *= require_tree ./alchemy/elements\n" - if File.exist?("app/assets/stylesheets/application.css") - insert_into_file "app/assets/stylesheets/application.css", stylesheet_require, + if File.exist?(app_assets_path.join("stylesheets", "application.css")) + insert_into_file app_assets_path.join("stylesheets", "application.css"), stylesheet_require, before: " */" else - create_file "app/assets/stylesheets/application.css", "/*\n#{stylesheet_require} */\n" + create_file app_assets_path.join("stylesheets", "application.css"), "/*\n#{stylesheet_require} */\n" end - copy_file "_article.html.erb", "app/views/alchemy/elements/_article.html.erb" - copy_file "_standard.html.erb", "app/views/alchemy/page_layouts/_standard.html.erb" - copy_file "alchemy.en.yml", "config/locales/alchemy.en.yml" + copy_file "_article.html.erb", app_views_path.join("alchemy", "elements", "_article.html.erb") + copy_file "_standard.html.erb", app_views_path.join("alchemy", "page_layouts", "_standard.html.erb") + copy_file "alchemy.en.yml", app_config_path.join("locales", "alchemy.en.yml") end def copy_dragonfly_config - template "#{__dir__}/templates/dragonfly.rb.tt", "config/initializers/dragonfly.rb" + template "#{__dir__}/templates/dragonfly.rb.tt", app_config_path.join("initializers", "dragonfly.rb") end def install_gutentag_migrations @@ -57,8 +57,28 @@ def install_gutentag_migrations private - def config_path - @_config_path ||= File.expand_path("../../../../../config/alchemy", __dir__) + def gem_config_path + @_config_path ||= File.expand_path("../../../../config/alchemy", __dir__) + end + + def app_config_path + @_app_config_path ||= app_root.join("config") + end + + def app_views_path + @_app_views_path ||= app_root.join("app", "views") + end + + def app_assets_path + @_app_assets_path ||= app_root.join("app", "assets") + end + + def app_vendor_assets_path + @_app_vendor_assets_path ||= app_root.join("vendor", "assets") + end + + def app_root + @_app_root ||= Rails.root end end end