Skip to content

Commit

Permalink
Merge pull request #458 from y-yagi/respect_changing_scaffold_generator
Browse files Browse the repository at this point in the history
Respect changing scaffold generator
  • Loading branch information
rafaelfranca authored Apr 4, 2019
2 parents b1c9e51 + 98e3dfe commit 7ff5e25
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
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

0 comments on commit 7ff5e25

Please sign in to comment.