Commit Graph

78536 Commits

Author SHA1 Message Date
Rafael Mendonça França d04f74dd72
Make sure that attribute_for_inspect respect filter_attributes
Before this change while inspect was respecting that option, attribute_for_inspect
was not.

I also changed inspect to use attribute_for_inspect so we make sure the behavior
is consistent in the two places and simplified the implementation.

Fixes #40725.
2020-12-02 23:01:34 +00:00
Rafael França 9f1623bd34
Merge pull request #40736 from jonathanhefner/guide-action_cable_overview-link-api
Link to API docs in Action Cable Overview guide [ci-skip]
2020-12-02 17:33:37 -05:00
Jonathan Hefner 0e9530b053 Link to API docs in Action Cable Overview guide [ci-skip]
This links the first mention of each method to its API documentation,
similar to a Wikipedia article.

This also modifies the text in a few places to ensure that methods are
explicitly mentioned and linked.
2020-12-02 16:08:37 -06:00
Rafael França be11d1b6e8
Merge pull request #40733 from ghiculescu/patch-3
Incorrect default for `config.autoload_paths` [ci skip]
2020-12-02 16:00:23 -05:00
Alex Ghiculescu f6c4815e9a
Incorrect default for `config.autoload_paths` [ci skip]
The docs implied that the default for `config.autoload_paths` was the output of `Dir.glob("app/*")`. In reality, on a new Rails app the default is `[]`.

Internally, Rails sets `ActiveSupport::Dependencies.autoload_paths` based on the union of [several configs](https://github.com/rails/rails/blob/v6.0.3.4/railties/lib/rails/engine.rb#L707), which is how `app` ends up being autoloaded (it is in `config.eager_load_paths` [by default](https://github.com/rails/rails/blob/v6.0.3.4/railties/lib/rails/engine/configuration.rb#L42)).

I think this is worth clarifying, because the current docs implies code like this will do something:

```ruby
config.autoload_paths -= ["#{config.root}/app/middleware"]
```

In reality it does nothing. This style of code is [discouraged](https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#globs-in-config-autoload-paths) but that doesn't mean it's not out there :) :(
2020-12-02 12:06:20 -06:00
Ryuta Kamizono 5384bbc4ce
Merge pull request #40727 from kamipo/fix_or_to_raise_argument_error
Fix `or` to raise structurally incompatible error for single values
2020-12-02 20:40:52 +09:00
Ryuta Kamizono d99dcdd9e1 Fix `or` to raise structurally incompatible error for single values
It is a regression for f854b46, to match empty values greedy.
But that it not important part, so I've removed that to fix #40723.
2020-12-02 20:05:01 +09:00
Eileen M. Uchitelle 600132afc1
Merge pull request #40720 from eileencodes/default-scope-all-queries
Add option for `default_scope` to run on all queries
2020-12-01 16:29:44 -05:00
Rafael França dc4dd50ac3
Merge pull request #40708 from p8/form-with-generates-local-forms
Change form_with to generate non-remote forms by default
2020-12-01 16:28:28 -05:00
Petrik 0a583c73c1 Change form_with to generate non remote forms by default
`form_with` would generate a remote form by default.
This confused users because they were forced to handle remote requests.

All new 6.1 applications will generate non-remote forms by default.
When upgrading a 6.0 application you can enable remote forms by default by
setting `config.action_view.form_with_generates_remote_forms` to `true`.
2020-12-01 21:14:37 +01:00
Rafael França 366df0397f
Merge pull request #40721 from d12/optimize_hash_with_indifferent_access_initializer
55% speedup for HashWithIndifferentAccess.new when no args provided
2020-12-01 14:56:56 -05:00
Jonathan Hefner e067a2a3f6
Merge pull request #40706 from jonathanhefner/guide-active_storage_overview-link-api
Link to API docs in Active Storage Overview guide [ci-skip]
2020-12-01 12:58:43 -06:00
Nathaniel Woodthorpe 0841cdf268 Optimize HashWithIndifferentAccess.new when no args are provided 2020-12-01 13:25:11 -05:00
eileencodes 816f6194b6
Add option for `default_scope` to run on all queries
This change allows for applications to optionally run a `default_scope`
on `update` and `delete` queries. Default scopes already ran on select
and insert queries.

Applications can now run a set default scope on all queries for a model
by setting a `all_queries` option:

```ruby
class Article < ApplicationRecord
  default_scope -> { where(blog_id: 1) }, all_queries: true
end
```

Using the default scope in this way is useful for applications that need
to query by more than the primary key by default. An example of this
would be in an application using a sharding strategy like Vitess like.
For Rails sharding, we route connections first and then query the
database. However, Vitess and other solutions use a parameter in the
query to figure out how to route the queries. By extending
`default_scope` to apply to all queries we can allow applications to
optionally apply additional constraints to all queries. Note that this
only works with `where` queries as it does not make sense to select a
record by primary key with an order. With this change we're allowing
apps to select with a primary key and an additional key.

To make this change dynamic for routing queries in a tenant sharding
strategy applications can use the `Current` API or parameters in a
request to route queries:

```ruby
class Article < ApplicationRecord
  default_scope -> { where(blog_id: Current.blog.id) }, all_queries: true
end
```

In order to achieve this I created a new object when default scopes are
created. This allows us to store both the scope itself and whether we
should run this on all queries. I chose not to implement an `on:` option
that takes an array of actions because there is no simple or clear way
to turn off the default scope for create/select. It also doesn't really
make sense to only have a default scope for delete queries. The decision
to use `all_queries` here allows for the implementation to be more
flexible than it was without creating a mess in an application.
2020-12-01 11:15:08 -05:00
Akira Matsuda 6f9d4a000b
Merge pull request #40663 from amatsuda/keep_safe_buffer
Let AS::SafeBuffer#[] and * return value be an instance of SafeBuffer in Ruby 3.0
2020-12-01 17:42:37 +09:00
Akira Matsuda 4cb20843eb Mark scrub as an unsafe method on SafeBuffer 2020-12-01 17:40:17 +09:00
Rafael França 38998af1d9
Merge pull request #40127 from seanpdoyle/form-tag-ids
Declare ActionView::Helpers::FormBuilder#id and #field_id
2020-11-30 19:41:42 -05:00
Rafael Mendonça França a173a65730
Merge pull request #39472
Closes #39472.
2020-12-01 00:40:52 +00:00
Sean Doyle 59ca21c011 Declare ActionView::Helpers::FormBuilder#id
`ActionView::Helpers::FormBuilder#id`
---

Generate an HTML `id` attribute value.

Return the [`<form>` element's][mdn-form] `id` attribute.

```html+erb
<%= form_for @post do |f| %>
  <%# ... %>

  <% content_for :sticky_footer do %>
    <%= form.button(form: f.id) %>
  <% end %>
<% end %>
```

In the example above, the `:sticky_footer` content area will exist
outside of the `<form>` element. [By declaring the `form` HTML
attribute][mdn-button-attr-form], we hint to the browser that the
generated `<button>` element should be treated as the `<form>` element's
submit button, regardless of where it exists in the DOM.

[A similar pattern could be used for `<input>`
elements][mdn-input-attr-form] (or other form controls) that do not
descend from the `<form>` element.

`ActionView::Helpers::FormBuilder#field_id`
---

Generate an HTML <tt>id</tt> attribute value for the given field

Return the value generated by the <tt>FormBuilder</tt> for the given
attribute name.

```html+erb
<%= form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title, aria: { describedby: form.field_id(:title, :error) } %>
  <span id="<%= f.field_id(:title, :error) %>">is blank</span>
<% end %>
```

In the example above, the <tt><input type="text"></tt> element built by
the call to <tt>FormBuilder#text_field</tt> declares an
<tt>aria-describedby</tt> attribute referencing the <tt><span></tt>
element, sharing a common <tt>id</tt> root (<tt>post_title</tt>, in this
case).

This method is powered by the `field_id` helper declared in
`action_view/helpers/form_tag_helper`, which is made available for
general template calls, separate from a `FormBuilder` instance.

[mdn-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
[mdn-button-attr-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-form
[mdn-input-attr-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-form
[mdn-aria-describedby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute
[w3c-wai]: https://www.w3.org/WAI/tutorials/forms/notifications/#listing-errors
2020-11-30 18:47:49 -05:00
Rafael França 89414f561a
Merge pull request #40045 from sandip-mane/40041-hosts-case-fix
Adds a fix to whitelist hostnames with case-insensitive matching
2020-11-30 18:44:12 -05:00
Rafael França 363cd6d5c0
Merge pull request #40503 from juanmanuelramallo/increase-worker-timeout-in-development
Increasing Puma's worker timeout in development
2020-11-30 18:43:05 -05:00
Rafael França 10c34f07ab
Merge pull request #40709 from francois-ferrandis/update-guide-on-delivery-method-in-callbacks
Update Action Mailer guide on delivery methods in callbacks
2020-11-30 18:36:33 -05:00
Rafael França 3446efc8bc
Merge pull request #40703 from d12/fix_deep_transform_keys_for_indifferent_access_hash
Fix bad return value of `HashWithIndifferentAccess#deep_transform_keys`
2020-11-30 18:16:57 -05:00
Rafael França 8ad477aa31
Merge pull request #40716 from ghiculescu/asset-host-config
Always recommend `config.asset_host` in guides, examples, and docs
2020-11-30 18:16:20 -05:00
Alex Ghiculescu 76e432341c Always recommend config.asset_host in examples 2020-11-30 16:33:51 -06:00
Kasper Timm Hansen 79d0c17c65
Merge pull request #39929 from jonathanhefner/decorate-original-attribute-type
Unify `decorate_attribute_type` and `attribute`
2020-11-30 23:28:22 +01:00
Nathaniel Woodthorpe f5e5976388 Fix the return value of #deep_transform_keys from a Hash to a HashWithIndifferentAccess 2020-11-30 17:13:53 -05:00
Rafael França bc828f74df
Merge pull request #40069 from abhaynikam/add-test-cases-for-action-mailbox-generator
Added test cases for Action Mailbox generator
2020-11-30 16:55:10 -05:00
Jonathan Hefner 4cc9c0f504 Unify `decorate_attribute_type` and `attribute`
Follow-up to 75c309c7ad.

As a result of these changes, attributes can have their type and default
value configured separately.  Similar behavior was implemented in #39380,
but only for attributes that derive (and do not override) their type
from the database.
2020-11-30 15:51:19 -06:00
Abhay Nikam d8f182101c Added test cases for Action Mailbox generator 2020-11-30 23:15:41 +05:30
Guillermo Iguaran 761fea3822
Merge pull request #40348 from rails/no-erb-in-ujsserver
Don't evaluate ERB in the ujs test server
2020-11-30 01:11:28 -08:00
François Ferrandis 10d495499a Update Action Mailer guide on delivery methods in callbacks 2020-11-29 18:38:16 +01:00
Jonathan Hefner 8fae4f213b Link to API docs in Active Storage Overview guide [ci-skip]
This links the first mention of each method to its API documentation,
similar to a Wikipedia article.

This also modifies the text in a few places to ensure that methods are
explicitly mentioned and linked.
2020-11-28 16:50:08 -06:00
Sean Doyle 7503ef9490
Escape HTML attributes for ERB interpolation (#40657)
Transforms a Hash into HTML Attributes, ready to be interpolated into
ERB.

```html+erb
<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %> >
<%# => <input type="text" aria-label="Search"> %>
```

Utilizes the `ActionView::Helpers::TagHelper#tag_options` implementation
to enable combining ERB with attribute transformation, without requiring
templates to replace HTML strings with `tag` or `content_tag`
invocations.

Co-authored-by: David Heinemeier Hansson <david@loudthinking.com>
2020-11-28 11:01:15 +01:00
Colin Kelley 87785122a7
serialization docs: clarify YAML, JSON, custom use cases (#40661) 2020-11-28 10:59:23 +01:00
Tahsin Hasan bb148d822c
Permit hash on direct upload in active storage (#40662) 2020-11-28 10:33:44 +01:00
Kasper Timm Hansen 3524dd9789
Merge pull request #40701 from dputtick/update-thin-dependency
Update thin to 1.8.0 to fix bundle install error with xcode 12 on MacOS
2020-11-28 01:11:48 +01:00
Rafael Mendonça França 4891507b07
Do not use webpacker master on edge Rails
Webpacker is stable now and we don't need to develop webpacker and rails
at the same time anymore.
2020-11-27 23:04:12 +00:00
Kasper Timm Hansen 835dba24cb
Merge pull request #40673 from jonathanhefner/actiontext-test-install-generator
Test Action Text install generator
2020-11-27 20:35:08 +01:00
Kasper Timm Hansen 4c7778cd1f
Merge pull request #40700 from seanpdoyle/tag-options-regexp
ActionView: Serialize Regexp into HTML attribute
2020-11-27 19:24:11 +01:00
Kasper Timm Hansen 1e69cda11c
Merge pull request #40702 from jonathanhefner/actiontext-render-html-format
Always render Action Text HTML with :html format
2020-11-27 19:19:47 +01:00
David Heinemeier Hansson 184358ae33
Remove low-level trick for copying images to be available in JS
It's a useful note, but it's not at the right level of abstraction. This entire pack is about loading high level frameworks. Not adding individual functions.

We should find a way to present this information in either a guide or another type of helper that's then included here.
2020-11-27 18:52:40 +01:00
Jonathan Hefner c4bbdb1c60
Merge pull request #40680 from jonathanhefner/guide-active_job_basics-link-api
Link to API docs in Active Job Basics guide [ci-skip]
2020-11-27 11:18:25 -06:00
Jonathan Hefner 76e111395c Always render Action Text HTML with :html format
Since #40222, Action Text HTML is rendered in the context of the current
request.  This causes the Action Text template format to default to the
request format, which prevents the template from being resolved when the
request format is not `:html` (e.g. `:json`).  Therefore, override the
template format to always be `:html`.

Fixes #40695.
2020-11-27 10:59:24 -06:00
Rafael França b13897fc01
Merge pull request #40646 from vaporyhumo/patch-1
Fix `rails new` and `rails app:update` hanging on master bug.
2020-11-27 11:50:10 -05:00
Rodrigo Andrés Contreras Vilina ca81d23c2c Update bin/yarn template
Prevent script from looping onto itself
2020-11-27 11:51:15 -03:00
Dan Puttick e87e6700ce Move unused build gems from ActionCable group
- blade and sprockets-export are only used by ActionView
- blade-sauce_labs_plugin is no longer used, removed in #34440
2020-11-26 18:58:17 -05:00
Sean Doyle d2e07395b3 ActionView: Serialize Regexp into HTML attribute
When serializing a Regexp instance, encode the [Regexp#source][]. When
encoding a value into a [pattern][] attribute from ERB/Ruby, declaring
the String can be tedious. For example, one might attempt to encode
`\w+` as `"\\\w+"`, but once serialized to the browser, that is not
equivalent to the `"\w+"` HTML attribute.

Instead, enable declaring Regexp and Regexp literals as attributes, and
encoding them as their source String.

[Regexp#source]: https://ruby-doc.org/core-2.7.2/Regexp.html#method-i-source
[pattern]: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern
2020-11-26 17:52:26 -05:00
Dan Puttick f6c9bdda19 Use thin 1.8.0 2020-11-26 17:21:39 -05:00
Rafael França f75546891a
Merge pull request #40694 from alvir/travel-to-with-string-argument-lose-timezone
Use application time zone in `travel_to` when it gets time as String.
2020-11-26 14:45:25 -05:00