Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new params.expect syntax instead of params.require #573

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/generators/rails/templates/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ def destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_<%= singular_table_name %>
<%- if Rails::VERSION::MAJOR >= 8 -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params.expect(:id)") %>
<%- else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
<%- end -%>
end

# Only allow a list of trusted parameters through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.fetch(<%= ":#{singular_table_name}" %>, {})
<%- elsif Rails::VERSION::MAJOR >= 8 -%>
params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
<%- end -%>
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/rails/templates/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ def destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_<%= singular_table_name %>
<%- if Rails::VERSION::MAJOR >= 8 -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params.expect(:id)") %>
<%- else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
<%- end -%>
end

# Only allow a list of trusted parameters through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.fetch(<%= ":#{singular_table_name}" %>, {})
<%- elsif Rails::VERSION::MAJOR >= 8 -%>
params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
<%- end -%>
Expand Down
17 changes: 15 additions & 2 deletions test/scaffold_api_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
assert_match %r{@post\.destroy}, m
end

assert_match %r{def set_post}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(:id\)}, content
else
assert_match %r{params\[:id\]}, content
end

assert_match %r{def post_params}, content
if Rails::VERSION::MAJOR >= 6
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(post: \[ :title, :body, images: \[\] \]\)}, content
elsif Rails::VERSION::MAJOR >= 6
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, images: \[\]\)}, content
else
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, :images\)}, content
Expand All @@ -62,7 +71,11 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
run_generator ["Message", "content:rich_text", "video:attachment", "photos:attachments"]

assert_file 'app/controllers/messages_controller.rb' do |content|
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(message: \[ :content, :video, photos: \[\] \]\)}, content
else
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
end
end
end
end
Expand Down
17 changes: 15 additions & 2 deletions test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_match %r{format\.json \{ head :no_content \}}, m
end

assert_match %r{def set_post}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(:id\)}, content
else
assert_match %r{params\[:id\]}, content
end

assert_match %r{def post_params}, content
if Rails::VERSION::MAJOR >= 6
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(post: \[ :title, :body, images: \[\] \]\)}, content
elsif Rails::VERSION::MAJOR >= 6
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, images: \[\]\)}, content
else
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, :images\)}, content
Expand Down Expand Up @@ -92,7 +101,11 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
run_generator %w(Message content:rich_text video:attachment photos:attachments)

assert_file 'app/controllers/messages_controller.rb' do |content|
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
if Rails::VERSION::MAJOR >= 8
assert_match %r{params\.expect\(message: \[ :content, :video, photos: \[\] \]\)}, content
else
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
end
end
end
end
Expand Down