diff --git a/app/models/alchemy/element.rb b/app/models/alchemy/element.rb index dcf086ef3f..4cef90a3b2 100644 --- a/app/models/alchemy/element.rb +++ b/app/models/alchemy/element.rb @@ -22,6 +22,8 @@ module Alchemy class Element < BaseRecord + NAME_REGEXP = /\A[a-z0-9_-]+\z/ + include Alchemy::Logger include Alchemy::Taggable include Alchemy::Hints @@ -89,7 +91,7 @@ class Element < BaseRecord join_table: ElementToPage.table_name validates_presence_of :name, on: :create - validates_format_of :name, on: :create, with: /\A[a-z0-9_-]+\z/ + validates_format_of :name, on: :create, with: NAME_REGEXP attr_accessor :autogenerate_contents attr_accessor :autogenerate_nested_elements diff --git a/lib/rails/generators/alchemy/base.rb b/lib/generators/alchemy/base.rb similarity index 100% rename from lib/rails/generators/alchemy/base.rb rename to lib/generators/alchemy/base.rb diff --git a/lib/rails/generators/alchemy/elements/elements_generator.rb b/lib/generators/alchemy/elements/elements_generator.rb similarity index 69% rename from lib/rails/generators/alchemy/elements/elements_generator.rb rename to lib/generators/alchemy/elements/elements_generator.rb index 94af08026a..966dc85af6 100644 --- a/lib/rails/generators/alchemy/elements/elements_generator.rb +++ b/lib/generators/alchemy/elements/elements_generator.rb @@ -14,12 +14,7 @@ def create_partials @elements.each do |element| @element = element @contents = element["contents"] || [] - if element["name"] =~ /\A[a-z0-9_-]+\z/ - @element_name = element["name"].underscore - else - raise "Element name '#{element["name"]}' has wrong format. Only lowercase and non whitespace characters allowed." - end - + @element_name = element_name(element) conditional_template "view.html.#{template_engine}", "#{elements_dir}/_#{@element_name}.html.#{template_engine}" end end @@ -29,6 +24,14 @@ def create_partials def elements_dir @_elements_dir ||= "app/views/alchemy/elements" end + + def element_name(element) + if element["name"] =~ Alchemy::Element::NAME_REGEXP + element["name"].underscore + else + raise "Element name '#{element["name"]}' has wrong format. Only lowercase and non whitespace characters allowed." + end + end end end end diff --git a/lib/rails/generators/alchemy/elements/templates/view.html.erb b/lib/generators/alchemy/elements/templates/view.html.erb similarity index 100% rename from lib/rails/generators/alchemy/elements/templates/view.html.erb rename to lib/generators/alchemy/elements/templates/view.html.erb diff --git a/lib/rails/generators/alchemy/elements/templates/view.html.haml b/lib/generators/alchemy/elements/templates/view.html.haml similarity index 100% rename from lib/rails/generators/alchemy/elements/templates/view.html.haml rename to lib/generators/alchemy/elements/templates/view.html.haml diff --git a/lib/rails/generators/alchemy/elements/templates/view.html.slim b/lib/generators/alchemy/elements/templates/view.html.slim similarity index 100% rename from lib/rails/generators/alchemy/elements/templates/view.html.slim rename to lib/generators/alchemy/elements/templates/view.html.slim diff --git a/lib/rails/generators/alchemy/essence/essence_generator.rb b/lib/generators/alchemy/essence/essence_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/essence/essence_generator.rb rename to lib/generators/alchemy/essence/essence_generator.rb diff --git a/lib/rails/generators/alchemy/essence/templates/editor.html.erb b/lib/generators/alchemy/essence/templates/editor.html.erb similarity index 100% rename from lib/rails/generators/alchemy/essence/templates/editor.html.erb rename to lib/generators/alchemy/essence/templates/editor.html.erb diff --git a/lib/rails/generators/alchemy/essence/templates/view.html.erb b/lib/generators/alchemy/essence/templates/view.html.erb similarity index 100% rename from lib/rails/generators/alchemy/essence/templates/view.html.erb rename to lib/generators/alchemy/essence/templates/view.html.erb diff --git a/lib/rails/generators/alchemy/install/files/_article.html.erb b/lib/generators/alchemy/install/files/_article.html.erb similarity index 100% rename from lib/rails/generators/alchemy/install/files/_article.html.erb rename to lib/generators/alchemy/install/files/_article.html.erb diff --git a/lib/rails/generators/alchemy/install/files/_standard.html.erb b/lib/generators/alchemy/install/files/_standard.html.erb similarity index 100% rename from lib/rails/generators/alchemy/install/files/_standard.html.erb rename to lib/generators/alchemy/install/files/_standard.html.erb diff --git a/lib/rails/generators/alchemy/install/files/alchemy.en.yml b/lib/generators/alchemy/install/files/alchemy.en.yml similarity index 100% rename from lib/rails/generators/alchemy/install/files/alchemy.en.yml rename to lib/generators/alchemy/install/files/alchemy.en.yml diff --git a/lib/rails/generators/alchemy/install/files/all.css b/lib/generators/alchemy/install/files/all.css similarity index 100% rename from lib/rails/generators/alchemy/install/files/all.css rename to lib/generators/alchemy/install/files/all.css diff --git a/lib/rails/generators/alchemy/install/files/all.js b/lib/generators/alchemy/install/files/all.js similarity index 100% rename from lib/rails/generators/alchemy/install/files/all.js rename to lib/generators/alchemy/install/files/all.js diff --git a/lib/rails/generators/alchemy/install/files/application.html.erb b/lib/generators/alchemy/install/files/application.html.erb similarity index 100% rename from lib/rails/generators/alchemy/install/files/application.html.erb rename to lib/generators/alchemy/install/files/application.html.erb diff --git a/lib/rails/generators/alchemy/install/files/article.scss b/lib/generators/alchemy/install/files/article.scss similarity index 100% rename from lib/rails/generators/alchemy/install/files/article.scss rename to lib/generators/alchemy/install/files/article.scss diff --git a/lib/generators/alchemy/install/install_generator.rb b/lib/generators/alchemy/install/install_generator.rb new file mode 100644 index 0000000000..dea0c2b447 --- /dev/null +++ b/lib/generators/alchemy/install/install_generator.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true +require "rails/generators" + +module Alchemy + module Generators + class InstallGenerator < ::Rails::Generators::Base + desc "Installs Alchemy into your App." + + class_option :skip_demo_files, + type: :boolean, + default: false, + desc: "Skip creation of demo element, page and application layout." + + source_root File.expand_path("files", __dir__) + + def copy_config + 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", app_config_path.join("alchemy", "#{file}.yml") + end + end + + def install_assets + 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_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_path.join("stylesheets", "application.css")) + insert_into_file app_assets_path.join("stylesheets", "application.css"), stylesheet_require, + before: " */" + else + create_file app_assets_path.join("stylesheets", "application.css"), "/*\n#{stylesheet_require} */\n" + end + + 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", app_config_path.join("initializers", "dragonfly.rb") + end + + def install_gutentag_migrations + rake "gutentag:install:migrations" + end + + private + + 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 +end diff --git a/lib/rails/generators/alchemy/install/templates/dragonfly.rb.tt b/lib/generators/alchemy/install/templates/dragonfly.rb.tt similarity index 100% rename from lib/rails/generators/alchemy/install/templates/dragonfly.rb.tt rename to lib/generators/alchemy/install/templates/dragonfly.rb.tt diff --git a/lib/rails/generators/alchemy/install/templates/elements.yml.tt b/lib/generators/alchemy/install/templates/elements.yml.tt similarity index 100% rename from lib/rails/generators/alchemy/install/templates/elements.yml.tt rename to lib/generators/alchemy/install/templates/elements.yml.tt diff --git a/lib/rails/generators/alchemy/install/templates/menus.yml.tt b/lib/generators/alchemy/install/templates/menus.yml.tt similarity index 100% rename from lib/rails/generators/alchemy/install/templates/menus.yml.tt rename to lib/generators/alchemy/install/templates/menus.yml.tt diff --git a/lib/rails/generators/alchemy/install/templates/page_layouts.yml.tt b/lib/generators/alchemy/install/templates/page_layouts.yml.tt similarity index 100% rename from lib/rails/generators/alchemy/install/templates/page_layouts.yml.tt rename to lib/generators/alchemy/install/templates/page_layouts.yml.tt diff --git a/lib/rails/generators/alchemy/menus/menus_generator.rb b/lib/generators/alchemy/menus/menus_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/menus/menus_generator.rb rename to lib/generators/alchemy/menus/menus_generator.rb diff --git a/lib/rails/generators/alchemy/menus/templates/node.html.erb b/lib/generators/alchemy/menus/templates/node.html.erb similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/node.html.erb rename to lib/generators/alchemy/menus/templates/node.html.erb diff --git a/lib/rails/generators/alchemy/menus/templates/node.html.haml b/lib/generators/alchemy/menus/templates/node.html.haml similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/node.html.haml rename to lib/generators/alchemy/menus/templates/node.html.haml diff --git a/lib/rails/generators/alchemy/menus/templates/node.html.slim b/lib/generators/alchemy/menus/templates/node.html.slim similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/node.html.slim rename to lib/generators/alchemy/menus/templates/node.html.slim diff --git a/lib/rails/generators/alchemy/menus/templates/wrapper.html.erb b/lib/generators/alchemy/menus/templates/wrapper.html.erb similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/wrapper.html.erb rename to lib/generators/alchemy/menus/templates/wrapper.html.erb diff --git a/lib/rails/generators/alchemy/menus/templates/wrapper.html.haml b/lib/generators/alchemy/menus/templates/wrapper.html.haml similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/wrapper.html.haml rename to lib/generators/alchemy/menus/templates/wrapper.html.haml diff --git a/lib/rails/generators/alchemy/menus/templates/wrapper.html.slim b/lib/generators/alchemy/menus/templates/wrapper.html.slim similarity index 100% rename from lib/rails/generators/alchemy/menus/templates/wrapper.html.slim rename to lib/generators/alchemy/menus/templates/wrapper.html.slim diff --git a/lib/rails/generators/alchemy/module/module_generator.rb b/lib/generators/alchemy/module/module_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/module/module_generator.rb rename to lib/generators/alchemy/module/module_generator.rb diff --git a/lib/rails/generators/alchemy/module/templates/ability.rb.tt b/lib/generators/alchemy/module/templates/ability.rb.tt similarity index 100% rename from lib/rails/generators/alchemy/module/templates/ability.rb.tt rename to lib/generators/alchemy/module/templates/ability.rb.tt diff --git a/lib/rails/generators/alchemy/module/templates/controller.rb.tt b/lib/generators/alchemy/module/templates/controller.rb.tt similarity index 100% rename from lib/rails/generators/alchemy/module/templates/controller.rb.tt rename to lib/generators/alchemy/module/templates/controller.rb.tt diff --git a/lib/rails/generators/alchemy/module/templates/module_config.rb.tt b/lib/generators/alchemy/module/templates/module_config.rb.tt similarity index 100% rename from lib/rails/generators/alchemy/module/templates/module_config.rb.tt rename to lib/generators/alchemy/module/templates/module_config.rb.tt diff --git a/lib/rails/generators/alchemy/page_layouts/page_layouts_generator.rb b/lib/generators/alchemy/page_layouts/page_layouts_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/page_layouts/page_layouts_generator.rb rename to lib/generators/alchemy/page_layouts/page_layouts_generator.rb diff --git a/lib/rails/generators/alchemy/page_layouts/templates/layout.html.erb b/lib/generators/alchemy/page_layouts/templates/layout.html.erb similarity index 100% rename from lib/rails/generators/alchemy/page_layouts/templates/layout.html.erb rename to lib/generators/alchemy/page_layouts/templates/layout.html.erb diff --git a/lib/rails/generators/alchemy/page_layouts/templates/layout.html.haml b/lib/generators/alchemy/page_layouts/templates/layout.html.haml similarity index 100% rename from lib/rails/generators/alchemy/page_layouts/templates/layout.html.haml rename to lib/generators/alchemy/page_layouts/templates/layout.html.haml diff --git a/lib/rails/generators/alchemy/page_layouts/templates/layout.html.slim b/lib/generators/alchemy/page_layouts/templates/layout.html.slim similarity index 100% rename from lib/rails/generators/alchemy/page_layouts/templates/layout.html.slim rename to lib/generators/alchemy/page_layouts/templates/layout.html.slim diff --git a/lib/rails/generators/alchemy/site_layouts/site_layouts_generator.rb b/lib/generators/alchemy/site_layouts/site_layouts_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/site_layouts/site_layouts_generator.rb rename to lib/generators/alchemy/site_layouts/site_layouts_generator.rb diff --git a/lib/rails/generators/alchemy/site_layouts/templates/layout.html.erb b/lib/generators/alchemy/site_layouts/templates/layout.html.erb similarity index 100% rename from lib/rails/generators/alchemy/site_layouts/templates/layout.html.erb rename to lib/generators/alchemy/site_layouts/templates/layout.html.erb diff --git a/lib/rails/generators/alchemy/site_layouts/templates/layout.html.haml b/lib/generators/alchemy/site_layouts/templates/layout.html.haml similarity index 100% rename from lib/rails/generators/alchemy/site_layouts/templates/layout.html.haml rename to lib/generators/alchemy/site_layouts/templates/layout.html.haml diff --git a/lib/rails/generators/alchemy/site_layouts/templates/layout.html.slim b/lib/generators/alchemy/site_layouts/templates/layout.html.slim similarity index 100% rename from lib/rails/generators/alchemy/site_layouts/templates/layout.html.slim rename to lib/generators/alchemy/site_layouts/templates/layout.html.slim diff --git a/lib/rails/generators/alchemy/views/views_generator.rb b/lib/generators/alchemy/views/views_generator.rb similarity index 100% rename from lib/rails/generators/alchemy/views/views_generator.rb rename to lib/generators/alchemy/views/views_generator.rb diff --git a/lib/rails/generators/alchemy/install/install_generator.rb b/lib/rails/generators/alchemy/install/install_generator.rb deleted file mode 100644 index 465f3a16e7..0000000000 --- a/lib/rails/generators/alchemy/install/install_generator.rb +++ /dev/null @@ -1,65 +0,0 @@ -# frozen_string_literal: true -require "rails" - -module Alchemy - module Generators - class InstallGenerator < ::Rails::Generators::Base - desc "Installs Alchemy into your App." - - class_option :skip_demo_files, - type: :boolean, - default: false, - desc: "Skip creation of demo element, page and application layout." - - source_root File.expand_path("files", __dir__) - - def copy_config - copy_file "#{config_path}/config.yml", "config/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" - 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" - 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" - - 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, - before: " */" - else - create_file "app/assets/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" - end - - def copy_dragonfly_config - template "#{__dir__}/templates/dragonfly.rb.tt", "config/initializers/dragonfly.rb" - end - - def install_gutentag_migrations - rake "gutentag:install:migrations" - end - - private - - def config_path - @_config_path ||= File.expand_path("../../../../../config/alchemy", __dir__) - end - end - end -end diff --git a/spec/dummy/vendor/assets/javascripts/alchemy/admin/all.js b/spec/dummy/vendor/assets/javascripts/alchemy/admin/all.js index 96cd38c391..5658d5f69d 120000 --- a/spec/dummy/vendor/assets/javascripts/alchemy/admin/all.js +++ b/spec/dummy/vendor/assets/javascripts/alchemy/admin/all.js @@ -1 +1 @@ -../../../../../../../lib/rails/generators/alchemy/install/files/all.js \ No newline at end of file +../../../../../../../lib/generators/alchemy/install/files/all.js \ No newline at end of file diff --git a/spec/dummy/vendor/assets/stylesheets/alchemy/admin/all.css b/spec/dummy/vendor/assets/stylesheets/alchemy/admin/all.css index d2a82e9d54..788797283b 120000 --- a/spec/dummy/vendor/assets/stylesheets/alchemy/admin/all.css +++ b/spec/dummy/vendor/assets/stylesheets/alchemy/admin/all.css @@ -1 +1 @@ -../../../../../../../lib/rails/generators/alchemy/install/files/all.css \ No newline at end of file +../../../../../../../lib/generators/alchemy/install/files/all.css \ No newline at end of file