Skip to content

Commit 4c92357

Browse files
authored
Merge branch 'main' into rails8
2 parents 04467cb + 94c7f6a commit 4c92357

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

docs/docs/getting-started/simple-mode.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ end
2828
```
2929

3030
:::caution
31-
By default, searching and sorting are authorized on any column of your model. See [Authorization (allowlisting/denylisting)](/going-further/other-notes.md#authorization-allowlistingdenylisting) on how to prevent this.
31+
As of v4.0, searching and sorting are not authorized on _any_ column of your model by default. See [Authorization (allowlisting/denylisting)](/going-further/other-notes.md#authorization-allowlistingdenylisting) on how to define searchable attributes.
32+
Prior to v4.0, searching and sorting were authorized on any column of your model by default.
3233
:::
3334

3435
### Default search options

docs/docs/going-further/other-notes.md

+14-17
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ to `jsonb`, as [recommended by the PostgreSQL documentation](https://www.postgre
126126

127127
### Authorization (allowlisting/denylisting)
128128

129-
By default, searching and sorting are authorized on any column of your model
130-
and no class methods/scopes are whitelisted.
129+
By default, searching and sorting are not authorized on any column of your model
130+
and no class methods/scopes are allowlisted.
131131

132132
Ransack adds four methods to `ActiveRecord::Base` that you can redefine as
133133
class methods in your models to apply selective authorization:
@@ -137,36 +137,33 @@ class methods in your models to apply selective authorization:
137137
- `ransackable_scopes`
138138
- `ransortable_attributes`
139139

140-
Here is how these four methods are implemented in Ransack:
140+
Here is how these four methods could be implemented in your application:
141141

142142
```ruby
143-
# `ransackable_attributes` by default returns all column names
143+
# `ransackable_attributes` returns searchable column names
144144
# and any defined ransackers as an array of strings.
145-
# For overriding with a whitelist array of strings.
146145
#
147146
def ransackable_attributes(auth_object = nil)
148-
column_names + _ransackers.keys
147+
%w(title body) + _ransackers.keys
149148
end
150149

151-
# `ransackable_associations` by default returns the names
152-
# of all associations as an array of strings.
153-
# For overriding with a whitelist array of strings.
150+
# `ransackable_associations` returns the names
151+
# of searchable associations as an array of strings.
154152
#
155153
def ransackable_associations(auth_object = nil)
156-
reflect_on_all_associations.map { |a| a.name.to_s }
154+
%w[author]
157155
end
158156

159157
# `ransortable_attributes` by default returns the names
160158
# of all attributes available for sorting as an array of strings.
161-
# For overriding with a whitelist array of strings.
162159
#
163160
def ransortable_attributes(auth_object = nil)
164161
ransackable_attributes(auth_object)
165162
end
166163

167164
# `ransackable_scopes` by default returns an empty array
168165
# i.e. no class methods/scopes are authorized.
169-
# For overriding with a whitelist array of *symbols*.
166+
# For overriding with an allowlist, return an array of *symbols*.
170167
#
171168
def ransackable_scopes(auth_object = nil)
172169
[]
@@ -190,11 +187,11 @@ In an `Article` model, add the following `ransackable_attributes` class method
190187
class Article < ActiveRecord::Base
191188
def self.ransackable_attributes(auth_object = nil)
192189
if auth_object == :admin
193-
# whitelist all attributes for admin
194-
super
190+
# allow all attributes for admin
191+
column_names + _ransackers.keys
195192
else
196-
# whitelist only the title and body attributes for other users
197-
super & %w(title body)
193+
# allow only the title and body attributes for other users
194+
%w(title body)
198195
end
199196
end
200197

@@ -241,7 +238,7 @@ Trying it out in `rails console`:
241238
=> SELECT "articles".* FROM "articles" WHERE "articles"."id" = 1
242239
```
243240

244-
That's it! Now you know how to whitelist/blacklist various elements in Ransack.
241+
That's it! Now you know how to allow/block various elements in Ransack.
245242

246243
### Handling unknown predicates or attributes
247244

0 commit comments

Comments
 (0)