Skip to content

Commit

Permalink
Use rails root in install generator (AlchemyCMS#1822)
Browse files Browse the repository at this point in the history
* Move generators out of rails folder

* Use Rails app root to join paths

Without that it is not always ensured that the path is
the hosts apps root path.

* Refactor elements generator naming validation
  • Loading branch information
tvdeyen authored May 8, 2020
1 parent 5a86ae5 commit 81ae05f
Show file tree
Hide file tree
Showing 44 changed files with 99 additions and 74 deletions.
4 changes: 3 additions & 1 deletion app/models/alchemy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
File renamed without changes.
85 changes: 85 additions & 0 deletions lib/generators/alchemy/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -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
65 changes: 0 additions & 65 deletions lib/rails/generators/alchemy/install/install_generator.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/dummy/vendor/assets/javascripts/alchemy/admin/all.js
2 changes: 1 addition & 1 deletion spec/dummy/vendor/assets/stylesheets/alchemy/admin/all.css

0 comments on commit 81ae05f

Please sign in to comment.