Commit Graph

3892 Commits

Author SHA1 Message Date
Rafael França
428509700b
Merge pull request #38442 from roramirez/routes-in-scaffold_controller
scaffold_controller: Include the routes when create a generator Controller
2020-02-13 14:45:19 -05:00
Rodrigo Ramírez Norambuena
55b2264746 scaffold_controller: Include the routes when create a generator Controller
When is use a scaffold_controller add the routes as resources to the
config/route.rb

Also enable to use --skip-routes if doesn't want include the resources
into the config/routes.rb file.
2020-02-13 11:07:21 -03:00
eileencodes
c1a215a06b
Add schema cache tests
The schema cache tests test the following scenarios:

1) The default case works (single db, primary spec name (dev is default
to primary in 2-tier config), standard default schema cache filename)
2) Primary always wins over other entries
3) A custom schema cache filename works when set in the configuration
4) A custom schema cache filename works when set in the ENV

Cases that don't work:

1) A non-primary database entry picks up a namespaced schema cache file

This can't work currently because there's no way of knowing which cache
we actually want. In this railtie we can only load ActiveRecord::Base's
schema cache. If we grab the first config we risk loading a cache for
another connection because order is not guaranteed.

2) Multi-db schema caches

The reasons are similar to above. In addition we can't loop through the
configs, establish a connection, and load the cache because we don't
know what parent class to establish a connection to. In that case AR
Base will always get the cache and it would cause the last one to win
and therefore be loaded on the wrong connection.

The real fix for these issues is to get rid of the railtie entirely, but
for now we needed to set this back to what the behavior was before
recent changes but with the ability to pass a custom key.

Co-authored-by: Katrina Owen <kytrinyx@github.com>
2020-02-13 09:06:18 -05:00
Kasper Timm Hansen
e8070990ef Merge pull request #37516 from jonathanhefner/generator-generate-avoid-shell-out
Avoid shelling out for generator `generate` action
2020-02-09 23:29:23 +01:00
Kasper Timm Hansen
b7027099b1
Update git init when generating plugins
* Extract separate test, test with `--skip-git`
* Move `version_control` beneath `gitignore` to match `AppBuilder` from 8989a505
* Add changelog entry
2020-02-09 23:04:29 +01:00
Kasper Timm Hansen
71b6d721f7
Merge pull request #38396 from okuramasafumi/plugin-generator-git-init
Initialize Git when generating plugins
2020-02-09 22:51:34 +01:00
OKURA Masafumi
8541ad319a Initialize Git when generating plugins
When generating applications, it initializes Git repository since
8989a5057b
However, it doesn't initialize Git when creating plugins.
Plugins are mostly libraries and are likely hosted on GitHub,
so initializing Git for plugins makes sense.
2020-02-06 20:17:32 +09:00
Ryuta Kamizono
fa986ae0ca Simplify lookup_store
I think allowing nested array parameters is almost useless.
2020-02-05 14:07:56 +09:00
Narendra Rajput
f8059d2383 Fix the environment.rb file path in the benchmark generator template 2020-02-03 17:36:56 +05:30
Katrina Owen
e95b3fd21f
Use correct filename for schema cache on load
The initializer that loads the default schema cache on the default
connection doesn't account for the case where an app overrides the
default filename either via ENV["SCHEMA_PATH"], or via the
:schema_cache_path defined in the db config.

Note that as discussed in #34449 this initializer doesn't work for
applications using multiple databases, and this change doesn't fix that.
2020-01-29 21:43:19 +00:00
Kevin Jalbert
cc8eac90cd
Add benchmark generator
Introduce benchmark generator to add benchmarks to Rails applications.

The generator makes use of `benchmark-ips`, and will automatically add
the gem to the Gemfile as needed.

Co-authored-by: Gannon McGibbon <gannon.mcgibbon@gmail.com>
2020-01-24 12:44:33 -05:00
Eileen M. Uchitelle
a6bd34a708
Merge pull request #38250 from vikasvr/skip_keep_fix
Skip keeps option shouldn't restrict from creating empty directories
2020-01-23 11:07:00 -05:00
Jorge Manrubia
de46effc9a Cache compiled view templates when running tests by default
When generating a new app without `--skip-spring`, caching classes was
disabled in `environments/test.rb`. This was implicitly disabling caching
view templates too. This change will enable view template caching by adding
this to the generated `environments/test.rb`:

    config.action_view.cache_template_loading = true

See 65344f254cde87950c7f176cb7aa09c002a6f882
2020-01-21 11:21:47 +01:00
eileencodes
2a53fe638d
Deprecate and replace #default_hash and #[]
Database configurations are now objects almost everywhere, so we don't
need to fake access to a hash with `#default_hash` or it's alias `#[]`.
Applications should `configs_for` and pass `env_name` and `spec_name` to
get the database config object. If you're looking for the default for
the test environment you can pass `configs_for(env_name: "test", spec_name:
"primary")`. Change test to developement to get the dev config, etc.

`#default_hash` and `#[]` will be removed in 6.2.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-01-17 16:08:12 -05:00
Vikas Verma
603278626d generate empty directory for image only 2020-01-17 16:07:08 +05:30
Vikas Verma
44b819616a Add failing test cases 2020-01-17 02:57:47 +05:30
Jonathan Hefner
dcc3c85c2f WIP inline option 2020-01-09 15:49:58 -06:00
Jonathan Hefner
8ac5a44133 Avoid shelling out for generator generate action
This change addresses a few issues:

First, shelling out to the `rails` command requires the destination
directory to contain certain files that the command uses to initialize
itself.  While this is not an issue when running generators normally, it
is troublesome when testing generator-invoking generators which output
to ephemeral destination directories.

Second, shelling out to the `rails` command is very slow.  This also is
not a particular concern when running generators normally, but it makes
test suites for generator-invoking generators painfully slow.

Third, shelling out to the `rails` command fails silently by default.
Such silent failures can be surprising, and can lead to confusing
downstream failures.
2020-01-09 15:40:06 -06:00
Jonathan Hefner
5fbf14b92f Put dev dependencies in generated plugin Gemfile
The Gemfile offers more flexibility than the gemspec in terms of gem
groups and platforms.  Putting the default development dependencies in
the Gemfile encourages users to add their own development dependencies
to the Gemfile.  This is similar to the current behavior of the
`bundle gem` command (see bundler/bundler#7222).

This change also fixes a corner case where using the "--skip-gemspec"
and "--skip-active-record" options together would incorrectly generate a
"sqlite3" dependency in the Gemfile.
2020-01-09 15:17:04 -06:00
Rafael França
031763ab5c
Merge pull request #38169 from gsamokovarov/rails-middleware-move-before-after
Delayed middleware delete does not allow move operations
2020-01-08 12:17:26 -03:00
Genadi Samokovarov
40fc1651ad Delayed middleware delete does not allow move operations
While trying to fix #16433, we made the middleware deletions always
happen at the end. While this works for the case of deleting the
Rack::Runtime middleware, it makes operations like the following
misbehave.

```ruby
gem "bundler", "< 1.16"

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails"
end

require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  middleware.insert_after ActionDispatch::Session::CookieStore, ::Rails::Rack::Logger, config.log_tags
  middleware.delete ::Rails::Rack::Logger
end

require "minitest/autorun"
require "rack/test"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_returns_success
    get "/"
    assert last_response.ok?
  end

  private
    def app
      Rails.application
    end
end
```

In the case ☝️  the ::Rails::Rack::Logger would be deleted instead of
moved, because the order of middleware stack building execution will be:

```ruby
[:insert, ActionDispatch::Session::CookieStore, [::Rails::Rack::Logger]]
[:delete, ::Rails::Rack::Logger, [config.log_tags]]
```

This is pretty surprising and hard to reason about behaviour, unless you
go spelunking into the Rails configuration code.

I have a few solutions in mind and all of them have their drawbacks.

1. Introduce a `Rails::Configuration::MiddlewareStackProxy#delete!` that
delays the deleted operations. This will make `#delete` to be executed
in order. The drawback here is backwards incompatible behavior and a new
public method.

2. Just revert to the old operations. This won't allow people to delete
the `Rack::Runtime` middleware.

3. Legitimize the middleware moving with the new `#move_after` and
`#move_before` methods. This does not breaks any backwards
compatibility, but includes 2 new methods to the middleware stack.

I have implemented `3.` in this pull request.

Happy holidays! 🎄
2020-01-08 11:30:02 +02:00
Xavier Noria
ad04bc0d62 addresses RuboCop feedback 2020-01-07 21:41:33 +01:00
Xavier Noria
c0d91a4f9d restores the ability to manually eager load applications
The main interface to eager loading is config.eager_load. The logic that
implies happens during the boot process.

With the introduction of Zeitwerk, application code is loaded in the
finisher as everything else, but in previous versions of Rails users
could eager load the application code regardless of config.eager_load.

Use cases:

   * Some gems like indexers need to have everything in memory and would
   be a bad user experience to ask users to conditionally set the eager
   load flag.

   * Some tests may need to have everything in memory and would be a bad
   experience to have the flag enabled globally in the test environment.

I personally feel that the contract between this method and the entire
eager loading process is ill-defined. I believe this method is
essentially internal. The purpose of this patch is simply to restore this
functionality emulating what it did before because rethinking the design
of this interface may need time.
2020-01-07 21:34:28 +01:00
Rafael Mendonça França
667b150c20
Merge pull request #37404 from joshmn/respect_the_force
Fix collision check when using a generator and using the force option
2020-01-06 21:32:43 -03:00
Rafael Mendonça França
f718ab6c72
Fix rubocop violations 2019-12-27 18:05:21 -03:00
Rafael Mendonça França
a29ac90738
Deal with Ruby 2.5 and Ruby 2.6 in a different way 2019-12-27 17:54:59 -03:00
Rafael Mendonça França
69d98f9a63
Split keyword argument in the geenrators action tests 2019-12-27 17:23:30 -03:00
Haroon Ahmed
db1ae8cbb4 remove reference to global rails command and replace with bin/rails 2019-12-27 19:32:37 +00:00
Ryuta Kamizono
5324f2cb09 Revert "Merge pull request #37215 from utilum/avoid_test_flunking_on_warning"
This reverts commit ed78e96408f3f83e779a71c65b86aeb1cfc5616e, reversing
changes made to eca6c273fe2729b9634907562c2717cf86443b6b.
2019-12-25 17:13:09 +09:00
yuuji.yaginuma
c8b88dd4df Use the single line editor in console test
It's a bit difficult to deal properly with IRB's multi line and color,
and I think we don't need to check it out in `rails console` tests.
2019-12-24 20:47:21 +09:00
Brian Buchalter
53e9438ef2 Ignore test env in DatabaseTasks when DATABASE_URL is present
Fixes https://github.com/rails/rails/issues/28827.

The steps to reproduce are as follows:

git clone git@github.com:bbuchalter/rails-issue-28827.git
cd rails-issue-28827
bundle install
bin/rails db:create

Observe that we create two databases when invoking db:create: development and test. Now observe what happens when we invoke our drop command while using DATABASE_URL.

DATABASE_URL=sqlite3://$(pwd)/db/database_url.sqlite3 bin/rails db:create

As expected, the development environment now uses the DATABASE_URL. What is unexpected is that the test environment does not.

It's unclear what the expected behavior should be in this case, but the cause of it is this: 9f2c74eda0/activerecord/lib/active_record/tasks/database_tasks.rb (L494)

Because of each_local_configuration, there seems to be no way invoke these database rake on only the development environment to ensure DATABASE_URL is respected.

The smallest scope of change I can think to make would be to conditionalize this behavior so it does not get applied when DATABASE_URL is present.
2019-12-20 14:22:31 -08:00
Kouhei Sutou
4334299b65 Make Rails::Configuration::Generators accessor consistent
Rails::Configuration::Generators provides method_missing based accessor.

The following code sets `orm` value:

    Rails.application.config.generators.orm :data_mapper

The following code does NOT return `orm` value:

    Rails.application.config.generators.orm # => {}

It's better that the reader returns the value set by writter in terms of
consistency:

    Rails.application.config.generators.orm # => :data_mapper
2019-12-20 06:21:09 +09:00
Rafael França
e36d4a0381
Merge pull request #38026 from Edouard-chin/ec-av-base-loadorder
Don't require "action_view/base" in action pack:
2019-12-19 13:40:56 -03:00
Edouard CHIN
88ee52f9d9 Don't require "action_view/base" in action pack:
- ### Problem

  ActionPack requires "action_view/base" at boot time, this
  causes a variety of issue that I described in detail in #38024.

  There is no real reason to require av/base in the
  ActionDispatch::Debugexceptions class.

  ### Solution

  Like any other components (such as ActiveRecord, ActiveJob...),
  ActionView::Base shouldn't be loaded at boot time.

  Here are the two main changes needed for this:

  1) Actionview has a special initializer that needs to run
     before the app is fully booted (adding a executor needs to be done
     before application is done booting)
  63ec70e700/actionview/lib/action_view/railtie.rb (L81-L84)

     That initializer used a lazy load hooks but we can't do that anymore
     because Action::Base view won't be triggered during booting process.
     When it will get triggered, (presumably on the first request),
     it's too late to add an executor.

  ------------------------------------------------

  2) Compare to other components, ActionView doesn't use `Base` for
     configuration flag. A lot of flags ares instead set on modules
     (FormHelper, FormTagHelper).
     The problem is that those module depends on AV::Base to be
     loaded, as otherwise configuration set by the user aren't applied.
     (Since the lazy load hooks hasn't been triggered)
     63ec70e700/actionview/lib/action_view/railtie.rb (L66-L69)

     We shouldn't wait for AB::Base to be loaded in order to set these
     configuration. However, we need to do it inside an
     `after_initialize` block in order to let application
     set it to the value they want.

  Closes #28538

  Co-authored-by: betesh <iybetesh@gmail.com>"
2019-12-19 17:28:24 +01:00
eileencodes
3dffacd16e Fix remaining connection_config calls
We missed these in rails/rails#38005 because deprecation warnings are
silently swallowed by these tests.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-12-18 09:12:27 -05:00
Rafael França
6da2152d60
Merge pull request #28704 from unfunco/remove-superfluous-keep
Remove superfluous keep file
2019-12-18 11:02:26 -03:00
yuuji.yaginuma
1555fcf32c Load an application before use
Without this, `Rails.application` returns `nil`.
Ref: https://buildkite.com/rails/rails/builds/65683#e86ec58e-53a5-41be-8aeb-11c5705ee580/1042-1053
2019-12-18 18:01:02 +09:00
Ryuta Kamizono
72af0bbc3d Fix typos 2019-12-18 16:47:18 +09:00
Rafael Mendonça França
ed5f2c6192
Merge pull request #28209 from tjoyal/railties/add-config-rake_eager_load
[Railties] Add config rake_eager_load
2019-12-17 22:10:14 -03:00
Rafael França
399b7be1fc
Merge pull request #36377 from y-yagi/remove-spring-watcher-listen
Remove `spring-watcher-listen` from default Gemfile
2019-12-17 20:42:40 -03:00
Louis-Michel Couture
85cab2a319
Ensure migration generator respects primary key config in references
When rails is configured to use a specific primary key type:
```
config.generators do |g|
  g.orm :active_record, primary_key_type: :uuid
end
```

Previously:

    $ bin/rails g migration add_location_to_users location:references

The references line in the migration would not have `type: :uuid`.
This change causes the type to be applied appropriately.

Co-authored-by: Dermot Haughey <hderms@gmail.com>
2019-12-17 11:17:41 -05:00
Kasper Timm Hansen
6cde5fd30a
Merge pull request #37850
Closes #37850
2019-12-17 02:03:24 +01:00
Kasper Timm Hansen
5c3b6c73c4
Add helper method to slim down config_for test set up 2019-12-17 02:02:27 +01:00
Jean Boussier
21c7199c0f
Allow non-hash values in config files
Fix: https://github.com/rails/rails/issues/37800
2019-12-17 01:46:15 +01:00
Eileen M. Uchitelle
3578f6929b
Merge pull request #37963 from eileencodes/deprecate-config-in-dbconsole
Deprecate config in dbconsole
2019-12-16 09:27:24 -05:00
Kasper Timm Hansen
f2b69d80ab
Merge pull request #37979 from jonathanhefner/use-thor-run-env-option
Leverage new :env option for Thor::Actions#run
2019-12-15 22:08:11 +01:00
Jonathan Hefner
fe9abe4436 Leverage new :env option for Thor::Actions#run
Follow-up to #34980.

Also, refactor tests to be less brittle.
2019-12-15 13:18:06 -06:00
Kasper Timm Hansen
2da5439b85
Fix test name and test 6.1 default 2019-12-15 02:43:35 +01:00
Cédric Fabianski
7ccaa125ba
Add SameSite protection to every written cookie
Enabling `SameSite` cookie protection is an addition to CSRF protection,
where cookies won't be sent by browsers in cross-site POST requests when set to `:lax`.

`:strict` disables cookies being sent in cross-site GET or POST requests.

Passing `:none` disables this protection and is the same as previous versions albeit a `; SameSite=None` is appended to the cookie.

See upgrade instructions in config/initializers/new_framework_defaults_6_1.rb.

More info [here](https://tools.ietf.org/html/draft-west-first-party-cookies-07)

_NB: Technically already possible as Rack supports SameSite protection, this is to ensure it's applied to all cookies_
2019-12-15 01:37:24 +01:00
eileencodes
0983daa4aa Deprecate config in dbconsole
This change deprecates config in dbconsole and updates usage in the
dbconsole tests so that we can remove an accessor on the
`configuration_hash`.

Eventually we want to pass objects around everywhere instead of
hashes. Callers can use `db_config` to access the hash if necessary.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-12-14 09:57:07 -05:00