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

Respect changing scaffold generator #458

Merged
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/scaffold_controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class ScaffoldControllerGenerator
source_paths << File.expand_path('../templates', __FILE__)

hook_for :jbuilder, type: :boolean, default: true

private

def permitted_params
attributes_names.map { |name| ":#{name}" }.join(', ')
end unless private_method_defined? :permitted_params
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/rails/templates/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.fetch(<%= ":#{singular_table_name}" %>, {})
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
<%- end -%>
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rails/templates/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.fetch(<%= ":#{singular_table_name}" %>, {})
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
<%- end -%>
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/scaffold_api_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldControllerGenerator
arguments %w(Post title body:text --api)
arguments %w(Post title body:text images:attachments --api)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination

Expand Down Expand Up @@ -39,7 +39,11 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
end

assert_match %r{def post_params}, content
assert_match %r{params\.require\(:post\)\.permit\(:title, :body\)}, content
if 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
end
end
end

Expand Down
8 changes: 6 additions & 2 deletions test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldControllerGenerator
arguments %w(Post title body:text)
arguments %w(Post title body:text images:attachments)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination

Expand Down Expand Up @@ -51,7 +51,11 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
end

assert_match %r{def post_params}, content
assert_match %r{params\.require\(:post\)\.permit\(:title, :body\)}, content
if 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
end
end
end

Expand Down