Skip to content

Commit 83256f4

Browse files
sblackstonerwz
authored andcommitted
DRY generated view files
* Dry up generated json.jbuilder files * Partial should use attributes_list_with_timestamps * clean up whitespace
1 parent da700c9 commit 83256f4

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

lib/generators/rails/jbuilder_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def copy_view_files
2020
filename = filename_with_extensions(view)
2121
template filename, File.join('app/views', controller_file_path, filename)
2222
end
23+
template filename_with_extensions('partial'), File.join('app/views', controller_file_path, filename_with_extensions("_#{singular_table_name}"))
2324
end
2425

2526

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
json.array!(@<%= plural_table_name %>) do |<%= singular_table_name %>|
2-
json.extract! <%= singular_table_name %>, <%= attributes_list %>
3-
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
4-
end
1+
json.array! @<%= plural_table_name %>, partial: '<%= plural_table_name %>/<%= singular_table_name %>', as: :<%= singular_table_name %>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! <%= singular_table_name %>, <%= attributes_list_with_timestamps %>
2+
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
json.extract! @<%= singular_table_name %>, <%= attributes_list_with_timestamps %>
1+
json.partial! "<%= plural_table_name %>/<%= singular_table_name %>", <%= singular_table_name %>: @<%= singular_table_name %>

test/jbuilder_generator_test.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
1414
%w(index show).each do |view|
1515
assert_file "app/views/posts/#{view}.json.jbuilder"
1616
end
17+
assert_file "app/views/posts/_post.json.jbuilder"
1718
end
1819

1920
test 'index content' do
2021
run_generator
2122

2223
assert_file 'app/views/posts/index.json.jbuilder' do |content|
23-
assert_match /json\.array!\(@posts\) do \|post\|/, content
24-
assert_match /json\.extract! post, :id, :title, :body/, content
25-
assert_match /json\.url post_url\(post, format: :json\)/, content
24+
assert_match /json.array! @posts, partial: 'posts\/post', as: :post/, content
2625
end
2726

2827
assert_file 'app/views/posts/show.json.jbuilder' do |content|
29-
assert_match /json\.extract! @post, :id, :title, :body, :created_at, :updated_at/, content
28+
assert_match /json.partial! \"posts\/post\", post: @post/, content
29+
end
30+
31+
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
32+
assert_match /json\.extract! post, :id, :title, :body/, content
33+
assert_match /json\.url post_url\(post, format: :json\)/, content
3034
end
35+
36+
3137
end
3238
end

0 commit comments

Comments
 (0)