Skip to content

Commit

Permalink
Use new params.expect syntax (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedalbert authored Sep 15, 2024
1 parent 5288e8a commit 1a18149
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
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

0 comments on commit 1a18149

Please sign in to comment.