Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the dummy app uses the same template as real apps. #3180

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ Gemfile.lock

# rspec failures
.rspec_failures

db/
config/initializers
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ before_install: gem install bundler
bundler_args: --without development
before_script:
- export RETRY_COUNT=3
- bin/rake refinery:testing:dummy_app
- yes | bin/rake refinery:testing:dummy_app
script:
- bin/rspec $EXTENSION/spec
env:
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ group :test do
gem 'rspec-retry'
end

group :development, :test do
gem 'activejob'
end

# Load local gems according to Refinery developer preference.
if File.exist? local_gemfile = File.expand_path('../.gemfile', __FILE__)
eval File.read(local_gemfile)
Expand Down
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

begin
load File.join(File.expand_path('../../', __FILE__), 'spec/dummy/bin/rails')
rescue LoadError => load_error
rescue LoadError => _load_error
warn "No dummy Rails application found! \n" \
"To create one in spec/dummy, please run: \n\n" \
" rake refinery:testing:dummy_app"
Expand Down
36 changes: 9 additions & 27 deletions core/lib/generators/refinery/cms/cms_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pathname'
require 'mkmf'
require 'refinery/core/environment_checker'

module Refinery
class CmsGenerator < Rails::Generators::Base
Expand All @@ -17,6 +18,8 @@ class CmsGenerator < Rails::Generators::Base
:desc => "Skip over any database creation, migration or seeding."
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_seeds, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over seeding."

def generate
start_pretending?
Expand Down Expand Up @@ -190,36 +193,14 @@ def deploy_to_hosting_heroku!(message = nil)
run "heroku restart"
end

# Helper method to quickly convert destination_root to a Pathname for easy file path manipulation
# Helper method to quickly convert destination_root to a Pathname
# for easy file path manipulation
def destination_path
@destination_path ||= Pathname.new(self.destination_root)
end

def ensure_environments_are_sane!
# Massage environment files
%w(development test production).map{ |e| "config/environments/#{e}.rb"}.each do |env|
next unless destination_path.join(env).file?

# Refinery does not necessarily expect action_mailer to be available as
# we may not always require it (currently only the authentication extension).
# Rails, however, will optimistically place config entries for action_mailer.
current_mailer_config = File.read(destination_path.join(env)).to_s.
match(%r{^\s.+?config\.action_mailer\..+([\w\W]*\})?}).
to_a.flatten.first

if current_mailer_config.present?
new_mailer_config = [
" if config.respond_to?(:action_mailer)",
current_mailer_config.gsub(%r{\A\n+?}, ''). # remove extraneous newlines at the start
gsub(%r{^\ \ }) { |line| " #{line}" }, # add indentation on each line
" end"
].join("\n")

gsub_file env, current_mailer_config, new_mailer_config, :verbose => false
end

gsub_file env, "config.assets.compile = false", "config.assets.compile = true", :verbose => false
end
Refinery::Core::EnvironmentChecker.new(destination_path).call
end

def forced_overwriting?
Expand Down Expand Up @@ -266,7 +247,7 @@ def prepare_database!
command = %w[railties:install:migrations]
unless self.options[:skip_db]
command |= %w[db:create db:migrate]
command |= %w[db:seed] unless self.options[:skip_migrations]
command |= %w[db:seed] unless self.options[:skip_seeds]
end
rake command.join(' ')
end
Expand Down Expand Up @@ -294,6 +275,7 @@ def run_additional_generators!
generator_args = []
generator_args << '--quiet' if self.options[:quiet]
generator_args << '--skip-migrations' if self.options[:skip_migrations]
generator_args << '--skip-seeds' if self.options[:skip_seeds] && !self.options[:skip_migrations]
Refinery::CoreGenerator.start generator_args
Refinery::Authentication::DeviseGenerator.start generator_args if defined?(Refinery::Authentication::DeviseGenerator)
Refinery::Dragonfly::DragonflyGenerator.start generator_args if defined?(Refinery::Dragonfly::DragonflyGenerator)
Expand Down Expand Up @@ -322,7 +304,7 @@ def sanity_check_heroku_application_name!
message.join("\n")
end

options[:heroku] = '' if options[:heroku] == 'heroku'
options.delete(:heroku) if options[:heroku] == 'heroku'
end

def start_pretending?
Expand Down
41 changes: 31 additions & 10 deletions core/lib/generators/refinery/dummy/dummy_generator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'

module Refinery
class DummyGenerator < Rails::Generators::Base
desc "Creates blank Rails application, installs Refinery CMS, and all sample data"
desc "Creates a blank Rails application with Refinery CMS installed."

class_option :database, :default => ''

def self.source_paths
paths = self.superclass.source_paths
paths << File.expand_path('../templates', __FILE__)
paths.flatten
[
self.superclass.source_paths,
File.expand_path('../templates', __FILE__)
].flatten.compact
end

PASSTHROUGH_OPTIONS = [
:skip_active_record, :skip_javascript, :skip_action_cable, :skip_action_mailer, :database, :javascript, :quiet, :pretend, :force, :skip
:database,
:force,
:javascript,
:pretend,
:quiet,
:skip,
:skip_action_cable,
:skip_action_mailer,
:skip_active_record,
:skip_javascript
]

def generate_test_dummy
Expand All @@ -24,8 +33,13 @@ def generate_test_dummy
opts[:skip_bundle] = true
opts[:skip_action_cable] = true
opts[:skip_action_mailer] = true
opts[:skip_keeps] = true
opts[:skip_seeds] = true
opts[:template] = refinery_path.join("templates", "refinery", "edge.rb").to_s

invoke Rails::Generators::AppGenerator, [ File.expand_path(dummy_path, destination_root) ], opts
invoke Rails::Generators::AppGenerator,
[ File.expand_path(dummy_path, destination_root) ],
opts
end

def test_dummy_config
Expand Down Expand Up @@ -67,19 +81,22 @@ def test_dummy_inherited_templates

attr :database

protected
protected

def dummy_path
'spec/dummy'
end

def dummy_application_path
File.expand_path("#{dummy_path}/config/application.rb", destination_root)
end

def module_name
'Dummy'
end

def application_definition
@application_definition ||= begin
dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
unless options[:pretend] || !File.exists?(dummy_application_path)
contents = File.read(dummy_application_path)
contents[(contents.index("module #{module_name}"))..-1]
Expand All @@ -93,7 +110,11 @@ def camelized
end

def gemfile_path
'../../../../Gemfile'
"../../../../Gemfile"
end

def refinery_path
Pathname.new File.expand_path("../../../../../../", __FILE__)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'
# Pick the frameworks you want:
require 'active_record/railtie'
require 'action_controller/railtie'
require 'sprockets/railtie'

require 'bundler/setup'

Expand Down
50 changes: 50 additions & 0 deletions core/lib/refinery/core/environment_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Check that Rails application configuration is consistent with what we expect.
require 'pathname'

module Refinery
module Core
class EnvironmentChecker

def initialize(root_path, environments: %w(development test production))
@root_path = Pathname.new(root_path)
@environments = environments
end

def call
environment_files.each do |env_file|
# Refinery does not necessarily expect action_mailer to be available as
# we may not always require it (currently only the authentication extension).
# Rails, however, will optimistically place config entries for action_mailer.
current_mailer_config = mailer_config(env_file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mailer stuff should be done by refinerycms-inquiries. The core doesn't send mail.

Maybe this whole thing shouldn't even be our concern?


if current_mailer_config.present?
new_mailer_config = [
" if config.respond_to?(:action_mailer)",
current_mailer_config.gsub(%r{\A\n+?}, ''). # remove extraneous newlines at the start
gsub(%r{^\ \ }) { |line| " #{line}" }, # add indentation on each line
" end"
].join("\n")

env_file.write(
env_file.read.gsub(current_mailer_config, new_mailer_config)
)
end
end
end

private
attr_reader :environments, :root_path

def environment_files
environments.map { |env| root_path.join("config", "environments", "#{env}.rb") }
.select(&:file?)
end

def mailer_config(environment_file)
root_path.join(environment_file).read.match(
%r{^\s.+?config\.action_mailer\..+([\w\W]*\})?}
).to_a.flatten.first
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def change
t.string :image_uid
t.string :image_ext

t.timestamps
t.timestamps null: false
end
end
end
5 changes: 3 additions & 2 deletions images/lib/generators/refinery/images/images_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class ImagesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def up
t.text :body
t.integer :position

t.timestamps
t.timestamps null: false
end

add_index :refinery_page_parts, :id
Expand All @@ -29,7 +29,7 @@ def up
t.string :view_template
t.string :layout_template

t.timestamps
t.timestamps null: false
end

add_index :refinery_pages, :depth
Expand Down
5 changes: 3 additions & 2 deletions pages/lib/generators/refinery/pages/pages_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class PagesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def change
t.string :file_uid
t.string :file_ext

t.timestamps
t.timestamps null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class ResourcesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
20 changes: 8 additions & 12 deletions templates/refinery/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@
gem "coffee-rails", :group => :assets
end

if ENV['REFINERY_PATH']
append_file 'Gemfile' do
"
gem 'refinerycms', path: '#{ENV['REFINERY_PATH']}'
"
end
refinerycms_source = if ENV['REFINERY_PATH']
"path: '#{ENV['REFINERY_PATH']}'"
else
append_file 'Gemfile' do
"
gem 'refinerycms', git: 'https://github.com/refinery/refinerycms', branch: 'master'
"
end
"git: 'https://github.com/refinery/refinerycms', branch: 'master'"
end

append_file 'Gemfile' do
"
gem 'refinerycms', #{refinerycms_source}

# Add support for searching inside Refinery's admin interface.
gem 'refinerycms-acts-as-indexed', ['~> 3.0', '>= 3.0.0']

Expand All @@ -46,10 +40,12 @@
run 'bundle install'

rake 'db:create'
require 'refinery/core/environment_checker'
Refinery::Core::EnvironmentChecker.new(destination_root).call
generate "refinery:cms --fresh-installation #{ARGV.join(' ')}"

say <<-SAY
============================================================================
Your new Refinery CMS application is now running on edge and mounted to /.
Your new Refinery CMS application is now running on edge and mounted at '/'
============================================================================
SAY
2 changes: 1 addition & 1 deletion testing/lib/refinery/tasks/testing.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace :refinery do

Refinery::DummyGenerator.start %W[--quiet --database=#{ENV['DB'].presence || 'sqlite3'}]

Refinery::CmsGenerator.start %w[--quiet --fresh-installation]
Refinery::CmsGenerator.start %w[--quiet --fresh-installation --skip-seeds]

Dir.chdir dummy_app_path
end
Expand Down