1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add --skip-active-storage and do so automatically when --skip-active-record is used

Closes #30102

Revert part 787fe90dc0

--skip-active-storage pass throughs `rails plugin new`

Add changelog entry about default initialization of Active Storage
This commit is contained in:
bogdanvlviv 2017-08-06 20:41:31 +00:00
parent aa6bcbbac8
commit 4a835aa323
No known key found for this signature in database
GPG key ID: E4ACD76A6DB6DFDD
20 changed files with 270 additions and 65 deletions

View file

@ -1,3 +1,9 @@
* `rails new` and `rails plugin new` get `Active Storage` by default.
Add ability to skip `Active Storage` with `--skip-active-storage`
and do so automatically when `--skip-active-record` is used.
*bogdanvlviv*
* Gemfile for new apps: upgrade redis-rb from ~> 3.0 to 4.0.
*Jeremy Daer*

View file

@ -4,12 +4,12 @@ require "rails"
%w(
active_record/railtie
active_storage/engine
action_controller/railtie
action_view/railtie
action_mailer/railtie
active_job/railtie
action_cable/engine
active_storage/engine
rails/test_unit/railtie
sprockets/railtie
).each do |railtie|

View file

@ -22,6 +22,7 @@ module Rails
def generator_options
options = { api: !!Rails.application.config.api_only, update: true }
options[:skip_active_record] = !defined?(ActiveRecord::Railtie)
options[:skip_active_storage] = !defined?(ActiveStorage::Engine) || !defined?(ActiveRecord::Railtie)
options[:skip_action_mailer] = !defined?(ActionMailer::Railtie)
options[:skip_action_cable] = !defined?(ActionCable::Engine)
options[:skip_sprockets] = !defined?(Sprockets::Railtie)

View file

@ -51,6 +51,9 @@ module Rails
class_option :skip_active_record, type: :boolean, aliases: "-O", default: false,
desc: "Skip Active Record files"
class_option :skip_active_storage, type: :boolean, default: false,
desc: "Skip Active Storage files"
class_option :skip_puma, type: :boolean, aliases: "-P", default: false,
desc: "Skip Puma related files"
@ -193,11 +196,29 @@ module Rails
end
def include_all_railties? # :doc:
options.values_at(:skip_active_record, :skip_action_mailer, :skip_test, :skip_sprockets, :skip_action_cable).none?
[
options.values_at(
:skip_active_record,
:skip_action_mailer,
:skip_test,
:skip_sprockets,
:skip_action_cable
),
skip_active_storage?
].flatten.none?
end
def comment_if(value) # :doc:
options[value] ? "# " : ""
question = "#{value}?"
comment =
if respond_to?(question, true)
send(question)
else
options[value]
end
comment ? "# " : ""
end
def keeps? # :doc:
@ -208,6 +229,10 @@ module Rails
!options[:skip_active_record] && options[:database] == "sqlite3"
end
def skip_active_storage? # :doc:
options[:skip_active_storage] || options[:skip_active_record]
end
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
def initialize(name, version, comment, options = {}, commented_out = false)
super

View file

@ -114,7 +114,7 @@ module Rails
template "cable.yml" unless options[:skip_action_cable]
template "puma.rb" unless options[:skip_puma]
template "spring.rb" if spring_install?
template "storage.yml"
template "storage.yml" unless skip_active_storage?
directory "environments"
directory "initializers"
@ -139,7 +139,7 @@ module Rails
template "config/cable.yml"
end
if !active_storage_config_exist
if !skip_active_storage? && !active_storage_config_exist
template "config/storage.yml"
end
@ -355,6 +355,10 @@ module Rails
build(:system_test) if depends_on_system_test?
end
def create_storage_files
build(:storage) unless skip_active_storage?
end
def create_tmp_files
build(:tmp)
end

View file

@ -20,9 +20,10 @@ ruby <%= "'#{RUBY_VERSION}'" -%>
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
<% unless skip_active_storage? -%>
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
<% end -%>
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

View file

@ -12,7 +12,9 @@
//
<% unless options[:skip_javascript] -%>
//= require rails-ujs
<% unless skip_active_storage? -%>
//= require activestorage
<% end -%>
<% unless options[:skip_turbolinks] -%>
//= require turbolinks
<% end -%>

View file

@ -8,10 +8,10 @@ require "rails"
require "active_model/railtie"
require "active_job/railtie"
<%= comment_if :skip_active_record %>require "active_record/railtie"
<%= comment_if :skip_active_storage %>require "active_storage/engine"
require "action_controller/railtie"
<%= comment_if :skip_action_mailer %>require "action_mailer/railtie"
require "action_view/railtie"
require "active_storage/engine"
<%= comment_if :skip_action_cable %>require "action_cable/engine"
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
<%= comment_if :skip_test %>require "rails/test_unit/railtie"

View file

@ -27,8 +27,10 @@ Rails.application.configure do
config.cache_store = :null_store
end
<%- unless skip_active_storage? -%>
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
<%- end -%>
<%- unless options.skip_action_mailer? -%>
# Don't care if the mailer can't send.

View file

@ -44,9 +44,11 @@ Rails.application.configure do
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
<%- unless skip_active_storage? -%>
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
<%- end -%>
<%- unless options[:skip_action_cable] -%>
# Mount Action Cable outside main process or domain
# config.action_cable.mount_path = nil

View file

@ -28,8 +28,11 @@ Rails.application.configure do
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
<%- unless skip_active_storage? -%>
# Store uploaded files on the local file system in a temporary directory
config.active_storage.service = :test
<%- end -%>
<%- unless options.skip_action_mailer? -%>
config.action_mailer.perform_caching = false

View file

@ -21,8 +21,10 @@
!/tmp/.keep
<% end -%>
<% unless skip_active_storage? -%>
# Ignore uploaded files in development
/storage/*
<% end -%>
<% unless options.skip_yarn? -%>
/node_modules

View file

@ -87,7 +87,7 @@ task default: :test
end
PASSTHROUGH_OPTIONS = [
:skip_active_record, :skip_action_mailer, :skip_javascript, :skip_action_cable, :skip_sprockets, :database,
:skip_active_record, :skip_active_storage, :skip_action_mailer, :skip_javascript, :skip_action_cable, :skip_sprockets, :database,
:javascript, :skip_yarn, :api, :quiet, :pretend, :skip
]

View file

@ -11,5 +11,8 @@ pkg/
<%= dummy_path %>/node_modules/
<%= dummy_path %>/yarn-error.log
<% end -%>
<% unless skip_active_storage? -%>
<%= dummy_path %>/storage/
<% end -%>
<%= dummy_path %>/tmp/
<% end -%>

View file

@ -8,10 +8,10 @@ require "rails"
require "active_model/railtie"
require "active_job/railtie"
<%= comment_if :skip_active_record %>require "active_record/railtie"
<%= comment_if :skip_active_storage %>require "active_storage/engine"
require "action_controller/railtie"
<%= comment_if :skip_action_mailer %>require "action_mailer/railtie"
require "action_view/railtie"
require "active_storage/engine"
<%= comment_if :skip_action_cable %>require "action_cable/engine"
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
<%= comment_if :skip_test %>require "rails/test_unit/railtie"

View file

@ -10,4 +10,7 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
<% unless skip_active_storage? -%>
//= require activestorage
<% end -%>
//= require_tree .

View file

@ -75,6 +75,7 @@ DEFAULT_APP_FILES = %w(
log
package.json
public
storage
test/application_system_test_case.rb
test/test_helper.rb
test/fixtures
@ -89,6 +90,7 @@ DEFAULT_APP_FILES = %w(
tmp
tmp/cache
tmp/cache/assets
tmp/storage
)
class AppGeneratorTest < Rails::Generators::TestCase
@ -296,6 +298,65 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
def test_active_storage_mini_magick_gem
run_generator
assert_file "Gemfile", /^# gem 'mini_magick'/
end
def test_app_update_does_not_generate_active_storage_contents_when_skip_active_storage_is_given
app_root = File.join(destination_root, "myapp")
run_generator [app_root, "--skip-active-storage"]
FileUtils.cd(app_root) do
quietly { system("bin/rails app:update") }
end
assert_file "#{app_root}/config/environments/development.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{app_root}/config/environments/production.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{app_root}/config/environments/test.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_no_file "#{app_root}/config/storage.yml"
assert_file "#{app_root}/Gemfile" do |content|
assert_no_match(/gem 'mini_magick'/, content)
end
end
def test_app_update_does_not_generate_active_storage_contents_when_skip_active_record_is_given
app_root = File.join(destination_root, "myapp")
run_generator [app_root, "--skip-active-record"]
FileUtils.cd(app_root) do
quietly { system("bin/rails app:update") }
end
assert_file "#{app_root}/config/environments/development.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{app_root}/config/environments/production.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{app_root}/config/environments/test.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_no_file "#{app_root}/config/storage.yml"
assert_file "#{app_root}/Gemfile" do |content|
assert_no_match(/gem 'mini_magick'/, content)
end
end
def test_application_names_are_not_singularized
run_generator [File.join(destination_root, "hats")]
assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/

View file

@ -230,7 +230,7 @@ class PluginGeneratorTest < Rails::Generators::TestCase
end
def test_ensure_that_tests_work
run_generator
run_generator [destination_root, "--skip-active-storage"]
FileUtils.cd destination_root
quietly { system "bundle install" }
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bin/test 2>&1`)
@ -240,8 +240,7 @@ class PluginGeneratorTest < Rails::Generators::TestCase
run_generator [destination_root, "--full", "--skip_active_record"]
FileUtils.cd destination_root
quietly { system "bundle install" }
# FIXME: Active Storage will provoke a test error without ActiveRecord (fix by allowing to skip active storage)
assert_match(/1 runs, 0 assertions, 0 failures, 1 errors/, `bundle exec rake test 2>&1`)
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test 2>&1`)
end
def test_ensure_that_migration_tasks_work_with_mountable_option

View file

@ -7,7 +7,7 @@ class PluginTestRunnerTest < ActiveSupport::TestCase
def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --skip-bundle` }
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --skip-bundle --skip-active-storage` }
plugin_file "test/dummy/db/schema.rb", ""
end

View file

@ -189,6 +189,97 @@ module SharedGeneratorTests
end
end
def test_generator_for_active_storage
run_generator
assert_file "#{application_path}/app/assets/javascripts/application.js" do |content|
assert_match(/^\/\/= require activestorage/, content)
end
assert_file "#{application_path}/config/environments/development.rb" do |content|
assert_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/production.rb" do |content|
assert_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/test.rb" do |content|
assert_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/storage.yml"
assert_directory "#{application_path}/db/migrate"
assert_directory "#{application_path}/storage"
assert_directory "#{application_path}/tmp/storage"
assert_file ".gitignore" do |content|
assert_match(/\/storage\//, content)
end
end
def test_generator_if_skip_active_storage_is_given
run_generator [destination_root, "--skip-active-storage"]
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
assert_file "#{application_path}/app/assets/javascripts/application.js" do |content|
assert_no_match(/^\/\/= require activestorage/, content)
end
assert_file "#{application_path}/config/environments/development.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/production.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/test.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_no_file "#{application_path}/config/storage.yml"
assert_no_directory "#{application_path}/db/migrate"
assert_no_directory "#{application_path}/storage"
assert_no_directory "#{application_path}/tmp/storage"
assert_file ".gitignore" do |content|
assert_no_match(/\/storage\//, content)
end
end
def test_generator_does_not_generate_active_storage_contents_if_skip_active_record_is_given
run_generator [destination_root, "--skip-active-record"]
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
assert_file "#{application_path}/app/assets/javascripts/application.js" do |content|
assert_no_match(/^\/\/= require activestorage/, content)
end
assert_file "#{application_path}/config/environments/development.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/production.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{application_path}/config/environments/test.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_no_file "#{application_path}/config/storage.yml"
assert_no_directory "#{application_path}/db/migrate"
assert_no_directory "#{application_path}/storage"
assert_no_directory "#{application_path}/tmp/storage"
assert_file ".gitignore" do |content|
assert_no_match(/\/storage\//, content)
end
end
def test_generator_if_skip_action_mailer_is_given
run_generator [destination_root, "--skip-action-mailer"]
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailer\/railtie["']/