Skip to content

Commit 952f675

Browse files
committed
Include rich_text, attachment, and attachments fields in json partial
1 parent 7ff5e25 commit 952f675

5 files changed

+51
-0
lines changed

lib/generators/rails/jbuilder_generator.rb

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def attributes_list(attributes = attributes_names)
5050

5151
attributes.map { |a| ":#{a}"} * ', '
5252
end
53+
54+
def virtual_attributes
55+
attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
56+
end
5357
end
5458
end
5559
end
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
22
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
3+
<%- virtual_attributes.each do |attribute| -%>
4+
<%- if attribute.type == :rich_text -%>
5+
json.<%= attribute.name %> <%= singular_table_name %>.<%= attribute.name %>.to_s
6+
<%- elsif attribute.type == :attachment -%>
7+
json.<%= attribute.name %> url_for(<%= singular_table_name %>.<%= attribute.name %>)
8+
<%- elsif attribute.type == :attachments -%>
9+
json.<%= attribute.name %> do
10+
json.array!(<%= singular_table_name %>.<%= attribute.name %>) do |<%= attribute.singular_name %>|
11+
json.id <%= attribute.singular_name %>.id
12+
json.url url_for(<%= attribute.singular_name %>)
13+
end
14+
end
15+
<%- end -%>
16+
<%- end -%>

test/jbuilder_generator_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
4343
assert_no_match %r{:created_at, :updated_at}, content
4444
end
4545
end
46+
47+
if Rails::VERSION::MAJOR >= 6
48+
test 'handles virtual attributes' do
49+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
50+
51+
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
52+
assert_match %r{json\.content message\.content\.to_s}, content
53+
assert_match %r{json\.video url_for\(message\.video\)}, content
54+
assert_match %r{json\.photos do\n json\.array!\(message\.photos\) do \|photo\|\n json\.id photo\.id\n json\.url url_for\(photo\)\n end\nend}, content
55+
end
56+
end
57+
end
4658
end

test/scaffold_api_controller_generator_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,16 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
5555
assert_match %r{params\.fetch\(:post, \{\}\)}, content
5656
end
5757
end
58+
59+
60+
if Rails::VERSION::MAJOR >= 6
61+
test 'handles virtual attributes' do
62+
run_generator ["Message", "content:rich_text", "video:attachment", "photos:attachments"]
63+
64+
assert_file 'app/controllers/messages_controller.rb' do |content|
65+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
66+
end
67+
end
68+
end
5869
end
5970
end

test/scaffold_controller_generator_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
6767
assert_match %r{params\.fetch\(:post, \{\}\)}, content
6868
end
6969
end
70+
71+
if Rails::VERSION::MAJOR >= 6
72+
test 'handles virtual attributes' do
73+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
74+
75+
assert_file 'app/controllers/messages_controller.rb' do |content|
76+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
77+
end
78+
end
79+
end
7080
end

0 commit comments

Comments
 (0)