Skip to content

Commit

Permalink
Include rich_text, attachment, and attachments fields in json partial (
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 authored Jan 22, 2021
1 parent f6898db commit cc046c4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/generators/rails/jbuilder_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def attributes_list(attributes = attributes_names)

attributes.map { |a| ":#{a}"} * ', '
end

def virtual_attributes
attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
end
end
end
end
14 changes: 14 additions & 0 deletions lib/generators/rails/templates/partial.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
<%- virtual_attributes.each do |attribute| -%>
<%- if attribute.type == :rich_text -%>
json.<%= attribute.name %> <%= singular_table_name %>.<%= attribute.name %>.to_s
<%- elsif attribute.type == :attachment -%>
json.<%= attribute.name %> url_for(<%= singular_table_name %>.<%= attribute.name %>)
<%- elsif attribute.type == :attachments -%>
json.<%= attribute.name %> do
json.array!(<%= singular_table_name %>.<%= attribute.name %>) do |<%= attribute.singular_name %>|
json.id <%= attribute.singular_name %>.id
json.url url_for(<%= attribute.singular_name %>)
end
end
<%- end -%>
<%- end -%>
12 changes: 12 additions & 0 deletions test/jbuilder_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
assert_no_match %r{:created_at, :updated_at}, content
end
end

if Rails::VERSION::MAJOR >= 6
test 'handles virtual attributes' do
run_generator %w(Message content:rich_text video:attachment photos:attachments)

assert_file 'app/views/messages/_message.json.jbuilder' do |content|
assert_match %r{json\.content message\.content\.to_s}, content
assert_match %r{json\.video url_for\(message\.video\)}, content
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
end
end
end
end
11 changes: 11 additions & 0 deletions test/scaffold_api_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,16 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
assert_match %r{params\.fetch\(:post, \{\}\)}, content
end
end


if Rails::VERSION::MAJOR >= 6
test 'handles virtual attributes' do
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
end
end
end
end
end
10 changes: 10 additions & 0 deletions test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_match %r{params\.fetch\(:post, \{\}\)}, content
end
end

if Rails::VERSION::MAJOR >= 6
test 'handles virtual attributes' do
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
end
end
end
end

0 comments on commit cc046c4

Please sign in to comment.