From 164c2f625742da95407c44c70a381e2c80e4e118 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 4 Feb 2021 12:26:16 +0100 Subject: [PATCH] Modernize scaffold generator (#41210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Slim down scaffold css To prevent conflicts with utility frameworks that might also be resetting base elements. * Use a box-style partial rather than a table Shows the usage of partials right from the start. Better compatibility with upgrading to Turbo frames/stream updates. * Correct use of quotes * Use modern array-of-strings declaration * Use double quotes for everything * Fix syntax * Remove outdated viewport declaration This should be handled in app stylesheets. * Use double quotes everywhere * Use symbols not strings for before_action scoping * Use human name to deal with double word records * Grab test fixes from #41219 Thanks @abhaynikam 🙏 * Fix tests * Use locar var not ivar * Fix capitalization change * Update railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt Co-authored-by: Haroon Ahmed * Update railties/test/generators/scaffold_controller_generator_test.rb Co-authored-by: Haroon Ahmed * Update railties/test/generators/scaffold_controller_generator_test.rb Co-authored-by: Haroon Ahmed * Fix test * Update railties/test/generators/scaffold_generator_test.rb Co-authored-by: Jonathan Hefner * Correct logic warning about initializers Defining initializer 4 to run before itself and after 3 is possible, but not when the before constraint contradicts the after constraint via 2. * Rename master to main in all code references * Use length == 0 instead of empty in Preloader Previously we were checking empty? on the association which would make a query. Instead we can check length == 0 to ensure we are using the length of the loaded records and not issuing extra queries. Co-authored-by: Dinah Shi * Add regression tests for preloader query count * ActionCable guides suggest test adapter for test env [ci skip] * Changing 'rails new' --master to be --main Renaming test containing flag Updating other test referencing master branch Add notice that --master is deprecated, but still working the same as --main Only set @main if it's nil Making warn wildcard I think a hidden aliaes would be just as good Improving description & fixing rubocop error Forgot comma Deprecation warning was kind of hard - so just doing alias for now rubocop -a * I think passing in the --master argument to run_generator is the way to go * Removing .count to figure out why its failing * Raise unknown type error on the definition time If unknow type is given for attribute (`attribute :foo, :unknown`), unknown type error isn't raised on the definition time but runtime. It should be raised on the definition time. * Refactor `attribute` not to reference `&block` * Use major + minor AR versions in 'Directly inheriting' error message * Raise error when passing passing a class to :source_type Raise `ArgumentError` when `:source_type` is given as class in `has_many :through` relation. Closes #41092 * Adding badges and logo to README and CONTRIBUTING page * switch references to main branch in docs * Updating references to /rails/blob/master & raiks/tree/master to point to main * Don't return query cache enabled pools in the query cache executor Follow up #41046. If query cache is enabled for all connection pools, `pools` always matches to `all_connection_pools`, returning `pools` has become redundant. * Remove trailing commas in advanced route constraints example * Search for yarn.cmd and yarn.ps1 in bin/yarn Follow-up to #40950. On Windows, Yarn actually uses `yarn.cmd` and `yarn.ps1` for PowerShell. * Run bin/yarn via Ruby Windows cannot directly run shebang scripts, such as `bin/yarn`. Therefore, run `bin/yarn` via Ruby. Actually fixes #40942. Fixes #41123. * Allow to opt-out of `strict_loading` mode on a per-record base. This is useful when strict loading is enabled application wide or on a model level. * Fix granular connection swapping when there are multiple abstract classes Some applications maybe have multiple abstract classes in the inheritance chain but only one of those abstract classes is the one we want to switch connections on. Previously, multiple abstract class inhertance would break `connected_to` and not switch models to the correct connection context. To fix this we added a boolean that is set on the class when a connection is established so we can check for whether it's identified as a `connection_class?`. This allows us to delete the `abstract_class?` check, since you cannot establish a connection on a non-abstract class. The existing tests were changed because they were not calling `connects_to` and granular swapping won't work on classes that didn't establish the connection. The issue in these tests will be prevented when #40965 is merged. Co-authored-by: John Crepezzi * Avoid testing Thor internals Thor's `apply` method is responsible for fetching a template when given a URL. Therefore, assume that `apply` behaves correctly, and simply test that `apply` is called correctly. This avoids errors like https://buildkite.com/rails/rails/builds/74245#540ecdf1-58ea-470a-a397-09f675520eb9/1100-1109 resulting from erikhuda/thor@4ce38c5478a5a13f2214ca833eafc167615b4e6a. * Fix the benchmark script to point to the main branch * Resolve default annotation tags after config loads `Rails::SourceAnnotationExtractor::Annotation.tags` may be modified by app configuration. Therefore, resolve default annotation tags after loading the app configuration. This fixes errors like https://buildkite.com/rails/rails/builds/74268#240d60bc-baa7-4b6e-ad21-b3172095f939/1083-1092 resulting from erikhuda/thor@0222fe52ed3803fe3ee0033da5b6faac5ee6299c. * Upgrade all the gems to make sure we are testing with the latest versions locally * Don't bother checking if strings respond to string methods The respond_to? calls here are checking if the unsafe method names, in this case "gsub" and "sub", respond to those same methods. This is nonsensical and unnecessary. * Update name of input to fix typo * `connected_to` shouldn't be called on the abstract class that not established connection Fixed: https://github.com/rails/rails/issues/40559#issuecomment-752056106 When abstract class hasn't own connections, calling `AbstractClass.connection` returns parent class's connection. We call `AbstractClass.connection.preventing_writes?` expecting abstract class's state to be returned, but actually it is parent's one. I think that it isn't expected behavior so I prevents call `connected_to` on the abstract class that not established the connection. * Add Webpacker to Guides list Adds a link to the Webpacker guide (added in https://github.com/rails/rails/pull/40817) to the [guides list](https://guides.rubyonrails.org/) and "Guides Index" dropdown menu. * Update documents.yaml * Fix unintialized instance variable connection_class This was throwing a warning when the test suite is run. Adding an reader fixes the issue. * Webpacker guide: remove Basecamp reference [docs] As a Rails user, if you *don't* know who Basecamp is or their relationship to the Rails framework, then this sentence is confusing. Reworded to instead just refer to Rails defaults. cc @rossta * fix broken link * Update webpacker.md * Added the unless-block for continuity * Added :status for continuity * Removed line for continuity * Added a new line after the include statement * Connection specification now passes the "url" key to the adapter If the "url" protocol is "jdbc", "http", or "https" the url option will be passed to the adapter. Previously only urls with the "jdbc" prefix were passed to the Active Record Adapter, others are assumed to be adapter specification urls. Fixes #41137. * Support hash config for `structure_dump_flags` and `structure_load_flags` flags Now that Active Record supports multiple databases configuration we need a way to pass specific flags for dump/load databases since the options are not the same for different adapters. We can use in the original way: ```ruby ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ['--no-defaults', '--skip-add-drop-table'] #or ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = '--no-defaults --skip-add-drop-table' ``` And also use it passing a hash, with one or more keys, where the key is the adapter ```ruby ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = { mysql2: ['--no-defaults', '--skip-add-drop-table'], postgres: '--no-tablespaces' } ``` * Update test docs in "contributing to Rails" guide Updated guide to recommend `bin/test` and show examples of its use in different contexts. Co-authored-by: Marivaldo Cavalheiro * Fix current_page? with kwargs on ruby3 Prevent raising an error when `options` are given as kwargs, this is done by overriding `options` with kwargs if `options` are `nil`; this implies that if both `options` and kwargs are given, `options` takes precedence. Fixes #41198 * `start_with?` allows multiple prefix values * Fix code block in Webpacker Guide [ci skip] Formatting fix: the example directory tree listing was bleeding into the previous paragraph and had extraneous characters due to a missing newline. * Remove wrong usage for `arel_table` [ci skip] This usage doesn't work properly (missing `where`, undefined `published`, `arel_table` in the scope definition accidentally lose table alias). * Restore ActiveStorage::Blob#find_signed Rails 6.0 had a [public `find_signed` method][docs], but we changed it to `find_signed!` in https://github.com/rails/rails/commit/31148cd6bef4f3a3059c51c587a8bff78a2e73e3. This commit adds back `find_signed` alongside `find_signed!` to match the corresponding [Active Record methods][]. [docs]: https://api.rubyonrails.org/v6.0.0/classes/ActiveStorage/Blob.html#method-c-find_signed [Active Record methods]: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/signed_id.rb#L42-L66 * Handle throwing in controller action in log subscriber When throw was used in a controller action, and there is matching catch around the request in a Rack middleware, then :exception won't be present in the event payload. This is because ActiveSupport::Notifications::Instrumenter.instrument sets :exception in a rescue handler, but rescue is never called in a throw/catch scenario: catch(:halt) do begin throw :halt rescue Exception => e puts "rescue" # never reached ensure puts "ensure" end end Missing :exception was actually handled prior to Rails 6.1.0, but an optimization updated the code to assume this was present. So this can be considered a regression fix. * Improve Fixture support for Active Storage (#41065) * Improve Fixture support for Active Storage Inspired by [76b33aa][], this commit extends the Active Storage documentation to elaborate on how to declare fixtures. In support of that, also introduce the `ActiveStorage::FixtureSet.blob` method for injecting in-line [ActiveStorage::Blob][] attributes directly into fixture YAML. [76b33aa]: https://github.com/rails/rails/commit/76b33aa3d102ad4ff3f766ac2d3a711d73aaee82 [ActiveStorage::Blob]: https://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob.html * Extra CR for style * Two-space indention * Explaining variable didn't explain, inline for style Co-authored-by: David Heinemeier Hansson * Improve ActionText::FixtureSet documentation (#41062) * Improve ActionText::FixtureSet documentation Support for Action Text attachments in fixtures was added by [76b33aa][] and released as part of [6.1.1][], but has not yet been documented. This commit documents the `ActionText::FixtureSet` for the API documentation, and mentions it in the Rails Guides pages. [76b33aa]: https://github.com/rails/rails/commit/76b33aa3d102ad4ff3f766ac2d3a711d73aaee82 [6.1.1]: https://github.com/rails/rails/releases/tag/v6.1.1 * Fix indention of comments Co-authored-by: David Heinemeier Hansson * Fix Flaky ActiveStorage test (#41225) Fixes a flaky Active Storage test introduced by [rails/rails#41065][], and improves the documentation. It seems that the test is covering the backwards compatibility of an older interface for retrieving records through `ActiveStorage::Record#find_signed!`. The test itself would pass unpredictably. To isolate the failure and reproduce it consistently, a see value was found after some trial and error: ``` SEED=59729 bin/test test/fixture_set_test.rb test/models/attachment_test.rb ``` This _used_ to pass consistently because [rails/rails][#41065] introduced a call to `fixtures :all`, which introduces more variation in the database's ID generation sequence. Without that line, `id` values start at `1`, so the fact that calls to `ActiveStorage::Attached::One#id` and `ActiveStorage::Blob#id` **both return `1`** is purely coincidence. The proposed resolution changes the test slightly. Prior to this change, the identifier used during retrieval and verification fetched from `@user.avatar.id`, where `@user.avatar` is an instance of `ActiveStorage::Attached::One`. The verifier/retriever combination in that test expected a signed identifier for an `ActiveStorage::Blob` instance. The change involved retrieving an instance through `@user.avatar.blob`. To better emphasize how global the `fixtures :all` declaration is, move it from the `test/fixture_set_test.rb` file to the `test/test_helper.rb` file. [rails/rails#41065]: https://github.com/rails/rails/pull/41065 * Fix doc: stylesheet_include_tag -> stylesheet_link_tag * Remove reference to globalize gem * Fix typo [ci skip] * Add small improvements to the Webpacker guide - fix typos like double spaces, accidental caps - improve some HTML links by giving them a label - etc * Fixing delegated types example. * Allow jobs to rescue all exceptions Before this commit, only StandardError exceptions can be handled by rescue_from handlers. This changes the rescue clause to catch all Exception objects, allowing rescue handlers to be defined for Exception classes not inheriting from StandardError. This means that rescue handlers that are rescuing Exceptions outside of StandardError exceptions may rescue exceptions that were not being rescued before this change. Co-authored-by: Adrianna Chang * Guides: Missing erb tags for stylesheet_pack_tag [ci skip] The section on needing to use `stylesheet_pack_tag` was lacking the erb %'s. I updated the markdown to actually show the helper being used with erb as well as improve the wording of the sentence. * Guides: Remove unneeded statement about stylesheet_pack_tag [ci skip] * Update test names to match their behaviour These tests were names `…creation_failure…` but there's no creation failing in the tests themselves. Instead creation is succeeding, via the `create_association` method which is added by the `has_one` relation. I found these test names very confusing when reading this issue: https://github.com/rails/rails/issues/13197 And the commit it links to: c6e10b0 The least we can do to make that issue less confusing is to start by fixing these test names. [ci skip] * Remove SET NAMES, set collation using variable Fixes malformed packet error that occurred with MariaDB client connecting to RDS Aurora (MySQL 5.7) database. * Update test helper to call parallelize according to fork support * Show a warning when running no migration using SCOPE When running a migration with `ENV["SCOPE"]` set returns a warning if no migrations ran. The message serves as a hint for the end-user to make sure he knows that the migration have been filtered by `SCOPE`. * Move ActiveStorage fixture hooks to on_load In a test environment, rely on the loading of `:active_support_test_case`. Introduce the `:active_record_fixture_set` hook for the Active Storage engine to listen for during the load process in a development environment (like when running `db:fixtures:load`). Since this commit moves the task-aware path resolution out of the block that provided local variables, it recreates part of the fixture directory path resolution logic. * Improve ActiveRecord strict_loading documentation Apply the same structure as the documentation of `validate` option by starting the description with "when set to true". This implies, that the value passed to the option should be a boolean. * Remove legacy media=screen default from stylesheet_link_tag. If the media attribute is omitted, the default for web browsers is "all", meaning that by default links apply to all media. Before: ```ruby > stylesheet_link_tag "style" => ``` After: ```ruby > stylesheet_link_tag "style" => ``` The current behavior is not going to change for existing applications. For newly built applications, the media attribute is not going to be added by default. Which can be configured using the following: ``` Rails.application.config.action_view.stylesheet_media_default = false ``` * Do not eagerly load Request before initializers Without those changes the configurations to ActionDispatch::Request could not be applied in initializers. * Change Request#media_type to return nil When the request don't have a Content-Type header we were returning an empty string instead of nil like Rack does. * `ActionDispatch::Request#content_type` now returned Content-Type header as it is * Disable rubocop suggestions * Fix typo in the CHANGELOG * Add config.action_view.stylesheet_media_default to baseline config * Rename the config to make clear it is a boolean * Add CHANGELOG entry for #41215 * Fix CI failure due to `app.config.action_view.delete(:apply_apply_stylesheet_media_default)` https://buildkite.com/rails/rails/builds/74429#3de35026-a6dc-4f4c-b885-9b59e0c89b96/979-1036 * Fix deprecation message s/Rails 6.1 will return/Rails 7.0 will return/ `return_only_media_type_on_content_type` will be introduced in Rails 6.2 so the changing of returning Content-Type will happen in a future version of Rails (probably 7.0). * Revert "Merge pull request #41192 from kamipo/dont_return_pools" This reverts commit 9d8ff323729f8f8fe09f14f4f45586f2d6340a70, reversing changes made to 9cde02ef5f17bf4f20aaeb5314de0b52c86496a3. Need to revert this so I can revert another PR. * Revert "Merge pull request #41046 from eileencodes/dont-check-if-qc-is-enabled" This reverts commit c97f1f195fc299d9d2ae7954d2b842ec4822382c, reversing changes made to ac7851eb58d3e821f0f378c1417bc157d7df3be8. We haven't quite tracked down why yet, but this change caused our API to not use the query cache. Our API is Sinatra mixed with Rails. We install the Exectutor in our API so it was installed. However, (some?) production requests were showing 0 query cache hits. jhawthorn found that we likely need this check because if we don't the pools returned will be a different set. He'll send a test later today. * Ensure test rake commands run immediately Before this commit, Rails test Rake tasks only load the test files, and the tests only run in an at_exit hook via minitest/autorun. This prevents conditionally running tasks only when tests pass, or even more simply in the right order. As a simple example, if you have: task default: [:test, :rubocop] The rubocop task will run after the test task loads the test files but before the tests actually run. This commit changes the test Rake tasks to shell out to the test runner as a new process. This diverges from previous behavior because now, any changes made in the Rakefile or other code loaded by Rake won't be available to the child process. However this brings the behavior of `rake test` closer to the behavior of `rails test`. Co-authored-by: Adrianna Chang * Fix typo Co-authored-by: Haroon Ahmed Co-authored-by: Jonathan Hefner Co-authored-by: Gannon McGibbon Co-authored-by: Rafael Mendonça França Co-authored-by: John Hawthorn Co-authored-by: Dinah Shi Co-authored-by: Santiago Bartesaghi Co-authored-by: Mike Rogers Co-authored-by: Ryuta Kamizono Co-authored-by: Ryan Bigg Co-authored-by: Jacopo Co-authored-by: Guillaume Briday Co-authored-by: benhayehudi Co-authored-by: wout@mick-wout.com Co-authored-by: Ayrton De Craene Co-authored-by: eileencodes Co-authored-by: John Crepezzi Co-authored-by: Chris Salzberg Co-authored-by: Tim Tilberg Co-authored-by: alpaca-tc Co-authored-by: Alex Ghiculescu Co-authored-by: Xavier Noria Co-authored-by: ijlee2 Co-authored-by: Jon Bracy Co-authored-by: Gustavo Gonzalez Co-authored-by: Marivaldo Cavalheiro Co-authored-by: Chris Houhoulis Co-authored-by: Daniel Colson Co-authored-by: Janko Marohnić Co-authored-by: Sean Doyle Co-authored-by: André Luis Leal Cardoso Junior Co-authored-by: Youssef Boulkaid Co-authored-by: Orhan Toy Co-authored-by: David Stosik Co-authored-by: Andrew Culver Co-authored-by: Étienne Barrié Co-authored-by: Adrianna Chang Co-authored-by: Tony Drake Co-authored-by: Sam Jewell Co-authored-by: Robin Roestenburg Co-authored-by: Brandon Fish Co-authored-by: Sean Doyle Co-authored-by: Cecile Veneziani Co-authored-by: st0012 --- activerecord/CHANGELOG.md | 1 - .../erb/scaffold/scaffold_generator.rb | 2 + .../erb/scaffold/templates/edit.html.erb.tt | 12 +++-- .../erb/scaffold/templates/index.html.erb.tt | 32 +++---------- .../erb/scaffold/templates/new.html.erb.tt | 10 +++-- .../scaffold/templates/partial.html.erb.tt | 20 +++++++++ .../erb/scaffold/templates/show.html.erb.tt | 23 +++------- .../app/views/layouts/application.html.erb.tt | 11 +++-- .../rails/scaffold/templates/scaffold.css | 45 +++---------------- .../templates/api_controller.rb.tt | 2 +- .../templates/controller.rb.tt | 8 ++-- railties/test/application/generators_test.rb | 1 + railties/test/application/rake_test.rb | 2 +- .../test/generators/app_generator_test.rb | 6 +-- .../test/generators/plugin_generator_test.rb | 2 +- .../scaffold_controller_generator_test.rb | 9 ++-- .../generators/scaffold_generator_test.rb | 28 ++++++------ 17 files changed, 87 insertions(+), 127 deletions(-) create mode 100644 railties/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 8d0245d516..206c3a9716 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -38,7 +38,6 @@ ```ruby class User < ApplicationRecord - has_many :bookmarks has_many :articles, strict_loading: true end diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb index 6d3bccab0b..ece5a19577 100644 --- a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -21,6 +21,8 @@ module Erb # :nodoc: template filename, File.join("app/views", controller_file_path, filename) end end + + template "partial.html.erb", File.join("app/views", controller_file_path, "_#{singular_table_name}.html.erb") end private diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt index 81329473d9..268ceeafa7 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt @@ -1,6 +1,10 @@ -

Editing <%= singular_table_name.titleize %>

+

Editing <%= human_name.downcase %>

-<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %> +<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %> -<%%= link_to 'Show', @<%= singular_table_name %> %> | -<%%= link_to 'Back', <%= index_helper %>_path %> +
+ +
+ <%%= link_to "Show this <%= human_name.downcase %>", @<%= singular_table_name %> %> | + <%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path %> +
diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt index 2cf4e5c9d0..8c5bac3093 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt @@ -1,31 +1,9 @@

<%%= notice %>

-

<%= plural_table_name.titleize %>

+

<%= human_name %>

- - - -<% attributes.reject(&:password_digest?).each do |attribute| -%> - -<% end -%> - - - +
+ <%%= render @<%= plural_table_name %> %> +
- - <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> - -<% attributes.reject(&:password_digest?).each do |attribute| -%> - -<% end -%> - - - - - <%% end %> - -
<%= attribute.human_name %>
<%%= <%= singular_table_name %>.<%= attribute.column_name %> %><%%= link_to 'Show', <%= model_resource_name %> %><%%= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>) %><%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path %> +<%%= link_to "New <%= human_name.downcase %>", new_<%= singular_route_name %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt index 9b2b2f4875..bda5405f33 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt @@ -1,5 +1,9 @@ -

New <%= singular_table_name.titleize %>

+

New <%= human_name.downcase %>

-<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %> +<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %> -<%%= link_to 'Back', <%= index_helper %>_path %> +
+ +
+ <%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path %> +
diff --git a/railties/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt b/railties/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt new file mode 100644 index 0000000000..50456edcf0 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt @@ -0,0 +1,20 @@ +
+<% attributes.reject(&:password_digest?).each do |attribute| -%> +

+ <%= attribute.human_name %>: +<% if attribute.attachment? -%> + <%%= link_to <%= singular_table_name %>.<%= attribute.column_name %>.filename, <%= singular_table_name %>.<%= attribute.column_name %> if <%= singular_table_name %>.<%= attribute.column_name %>.attached? %> +<% elsif attribute.attachments? -%> + <%% <%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| %> +

<%%= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> %>
+ <%% end %> +<% else -%> + <%%= <%= singular_table_name %>.<%= attribute.column_name %> %> +<% end -%> +

+ +<% end -%> +

+ <%%= link_to "Show this <%= human_name %>", <%= singular_table_name %> %> +

+
diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt index d3f996188c..920d186981 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt @@ -1,19 +1,10 @@

<%%= notice %>

-<% attributes.reject(&:password_digest?).each do |attribute| -%> -

- <%= attribute.human_name %>: -<% if attribute.attachment? -%> - <%%= link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> if @<%= singular_table_name %>.<%= attribute.column_name %>.attached? %> -<% elsif attribute.attachments? -%> - <%% @<%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| %> -

<%%= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> %>
- <%% end %> -<% else -%> - <%%= @<%= singular_table_name %>.<%= attribute.column_name %> %> -<% end -%> -

+<%%= render @<%= singular_table_name %> %> -<% end -%> -<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> | -<%%= link_to 'Back', <%= index_helper %>_path %> +
+ <%%= link_to "Edit this <%= human_name.downcase %>", edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> | + <%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path %> + + <%%= button_to "Destroy this <%= human_name.downcase %>", <%= singular_table_name %>_path(@<%= singular_table_name %>), method: :delete %> +
diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt index 9e5a7afb57..53402bd2a2 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt @@ -2,19 +2,18 @@ <%= camelized %> - <%%= csrf_meta_tags %> <%%= csp_meta_tag %> <%- if options[:skip_javascript] -%> - <%%= stylesheet_link_tag 'application' %> + <%%= stylesheet_link_tag "application" %> <%- else -%> <%- unless options[:skip_turbolinks] -%> - <%%= stylesheet_link_tag 'application', 'data-turbolinks-track': 'reload' %> - <%%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> + <%%= stylesheet_link_tag "application", "data-turbolinks-track": "reload" %> + <%%= javascript_pack_tag "application", "data-turbolinks-track": "reload" %> <%- else -%> - <%%= stylesheet_link_tag 'application' %> - <%%= javascript_pack_tag 'application' %> + <%%= stylesheet_link_tag "application" %> + <%%= javascript_pack_tag "application" %> <%- end -%> <%- end -%> diff --git a/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css index cd4f3de38d..1f681ff137 100644 --- a/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css +++ b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css @@ -1,42 +1,3 @@ -body { - background-color: #fff; - color: #333; - margin: 33px; -} - -body, p, ol, ul, td { - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -pre { - background-color: #eee; - padding: 10px; - font-size: 11px; -} - -a { - color: #000; -} - -a:visited { - color: #666; -} - -a:hover { - color: #fff; - background-color: #000; -} - -th { - padding-bottom: 5px; -} - -td { - padding: 0 5px 7px; -} - div.field, div.actions { margin-bottom: 10px; @@ -46,6 +7,12 @@ div.actions { color: green; } +.scaffold_record { + border: 1px solid black; + padding: 10px; + margin-bottom: 10px; +} + .field_with_errors { padding: 2px; background-color: red; diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt b/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt index 4ea95f6e74..0936314a2c 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt @@ -4,7 +4,7 @@ require_dependency "<%= namespaced_path %>/application_controller" <% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController - before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy] + before_action :set_<%= singular_table_name %>, only: %i[ show update destroy ] # GET <%= route_url %> def index diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt index d82b4730a2..dbc675e521 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt @@ -4,7 +4,7 @@ require_dependency "<%= namespaced_path %>/application_controller" <% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController - before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] + before_action :set_<%= singular_table_name %>, only: %i[ show edit update destroy ] # GET <%= route_url %> def index @@ -29,7 +29,7 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> if @<%= orm_instance.save %> - redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %> + redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> else render :new, status: :unprocessable_entity end @@ -38,7 +38,7 @@ class <%= controller_class_name %>Controller < ApplicationController # PATCH/PUT <%= route_url %>/1 def update if @<%= orm_instance.update("#{singular_table_name}_params") %> - redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> + redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %> else render :edit, status: :unprocessable_entity end @@ -47,7 +47,7 @@ class <%= controller_class_name %>Controller < ApplicationController # DELETE <%= route_url %>/1 def destroy @<%= orm_instance.destroy %> - redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> + redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> end private diff --git a/railties/test/application/generators_test.rb b/railties/test/application/generators_test.rb index 0f8c686581..aee3602596 100644 --- a/railties/test/application/generators_test.rb +++ b/railties/test/application/generators_test.rb @@ -233,6 +233,7 @@ module ApplicationTests app/views/posts/show.html.erb app/views/posts/new.html.erb app/views/posts/_form.html.erb + app/views/posts/_post.html.erb test/controllers/posts_controller_test.rb test/system/posts_test.rb app/helpers/posts_helper.rb diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 12f506025e..885d12f4a2 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -173,7 +173,7 @@ module ApplicationTests end def test_code_statistics_sanity - assert_match "Code LOC: 74 Test LOC: 3 Code to Test Ratio: 1:0.0", + assert_match "Code LOC: 73 Test LOC: 3 Code to Test Ratio: 1:0.0", rails("stats") end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 05e6acd3d5..0062843d59 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -118,8 +118,8 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_assets run_generator - assert_file("app/views/layouts/application.html.erb", /stylesheet_link_tag\s+'application', 'data-turbolinks-track': 'reload'/) - assert_file("app/views/layouts/application.html.erb", /javascript_pack_tag\s+'application', 'data-turbolinks-track': 'reload'/) + assert_file("app/views/layouts/application.html.erb", /stylesheet_link_tag\s+"application", "data-turbolinks-track": "reload"/) + assert_file("app/views/layouts/application.html.erb", /javascript_pack_tag\s+"application", "data-turbolinks-track": "reload"/) assert_file("app/assets/stylesheets/application.css") assert_file("app/javascript/packs/application.js") end @@ -667,7 +667,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_file "app/javascript" assert_file "app/views/layouts/application.html.erb" do |contents| - assert_match(/stylesheet_link_tag\s+'application' %>/, contents) + assert_match(/stylesheet_link_tag\s+"application" %>/, contents) assert_no_match(/javascript_pack_tag\s+'application'/, contents) end end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 31eec0b361..d1971644df 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -712,7 +712,7 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_file "app/models/bukkits/article.rb" assert_file "app/controllers/bukkits/articles_controller.rb" do |content| - assert_match "only: [:show, :update, :destroy]", content + assert_match "only: %i[ show update destroy ]", content end assert_no_directory "app/assets" diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index c2f18cb527..79a745ff11 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -221,14 +221,11 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/admin/users/index.html.erb" do |content| - assert_match("'Show', [:admin, user]", content) - assert_match("'Edit', edit_admin_user_path(user)", content) - assert_match("'Destroy', [:admin, user]", content) - assert_match("'New User', new_admin_user_path", content) + assert_match("\"New user\", new_admin_user_path", content) end assert_file "app/views/admin/users/new.html.erb" do |content| - assert_match("'Back', admin_users_path", content) + assert_match("\"Back to users\", admin_users_path", content) end assert_file "app/views/admin/users/_form.html.erb" do |content| @@ -267,7 +264,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/class UsersController < ApplicationController/, content) assert_no_match(/respond_to/, content) - assert_match(/before_action :set_user, only: \[:show, :update, :destroy\]/, content) + assert_match(/before_action :set_user, only: %i\[ show update destroy \]/, content) assert_instance_method :index, content do |m| assert_match(/@users = User\.all/, m) diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 37b08a5633..6d374e85e8 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -82,7 +82,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end %w(edit new).each do |view| - assert_file "app/views/product_lines/#{view}.html.erb", /render 'form', product_line: @product_line/ + assert_file "app/views/product_lines/#{view}.html.erb", /render "form", product_line: @product_line/ end assert_file "app/views/product_lines/_form.html.erb" do |test| @@ -118,7 +118,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/class ProductLinesController < ApplicationController/, content) assert_no_match(/respond_to/, content) - assert_match(/before_action :set_product_line, only: \[:show, :update, :destroy\]/, content) + assert_match(/before_action :set_product_line, only: %i\[ show update destroy \]/, content) assert_instance_method :index, content do |m| assert_match(/@product_lines = ProductLine\.all/, m) @@ -221,7 +221,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_no_file "app/helpers/product_lines_helper.rb" # Assets - assert_file "app/assets/stylesheets/scaffold.css", /:visited/ + assert_file "app/assets/stylesheets/scaffold.css", /.scaffold_record/ assert_no_file "app/assets/stylesheets/product_lines.css" end @@ -282,10 +282,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Views assert_file "app/views/admin/roles/index.html.erb" do |content| - assert_match("'Show', admin_role", content) - assert_match("'Edit', edit_admin_role_path(admin_role)", content) - assert_match("'Destroy', admin_role", content) - assert_match("'New Admin Role', new_admin_role_path", content) + assert_match(%("New role", new_admin_role_path), content) end %w(edit new show _form).each do |view| @@ -297,7 +294,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "app/helpers/admin/roles_helper.rb" # Assets - assert_file "app/assets/stylesheets/scaffold.css", /:visited/ + assert_file "app/assets/stylesheets/scaffold.css", /.scaffold_record/ assert_file "app/assets/stylesheets/admin/roles.css" end @@ -461,13 +458,14 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/accounts/index.html.erb" do |content| - assert_match(/^\W{8}<%= account\.name %><\/td>/, content) - assert_match(/^\W{8}<%= account\.user_id %><\/td>/, content) + assert_match(/^\W{2}<%= render @accounts %>/, content) end assert_file "app/views/accounts/show.html.erb" do |content| - assert_match(/^\W{2}<%= @account\.name %>/, content) - assert_match(/^\W{2}<%= @account\.user_id %>/, content) + assert_match(/<%= render @account %>/, content) + assert_match(/link_to "Edit this account"/, content) + assert_match(/button_to "Destroy this account"/, content) + assert_match(/link_to "Back to accounts"/, content) end end @@ -488,9 +486,9 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/^\W{4}<%= form\.file_field :photos, multiple: true %>/, content) end - assert_file "app/views/messages/show.html.erb" do |content| - assert_match(/^\W{2}<%= link_to @message\.video\.filename, @message\.video if @message\.video\.attached\? %>/, content) - assert_match(/^\W{4}
<%= link_to photo\.filename, photo %>/, content) + assert_file "app/views/messages/_message.html.erb" do |content| + assert_match(/^\W{4}<%= link_to message\.video\.filename, message\.video if message\.video\.attached\? %>/, content) + assert_match(/^\W{6}
<%= link_to photo\.filename, photo %>/, content) end assert_file "test/system/messages_test.rb" do |content|