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

Fix namespace issue when generating jbuilder views #536

Merged
merged 1 commit into from
Jun 15, 2022
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
4 changes: 4 additions & 0 deletions lib/generators/rails/jbuilder_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def attributes_list(attributes = attributes_names)
def virtual_attributes
attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
end

def partial_path_name
[controller_file_path, singular_table_name].join("/")
end
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/rails/templates/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.array! @<%= plural_table_name %>, partial: "<%= plural_table_name %>/<%= singular_table_name %>", as: :<%= singular_table_name %>
json.array! @<%= plural_table_name %>, partial: "<%= partial_path_name %>", as: :<%= singular_table_name %>
2 changes: 1 addition & 1 deletion lib/generators/rails/templates/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.partial! "<%= plural_table_name %>/<%= singular_table_name %>", <%= singular_table_name %>: @<%= singular_table_name %>
json.partial! "<%= partial_path_name %>", <%= singular_table_name %>: @<%= singular_table_name %>
12 changes: 12 additions & 0 deletions test/jbuilder_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
end
end

test 'namespaced views are generated correctly for index' do
run_generator %w(Admin::Post --model-name=Post)

assert_file 'app/views/admin/posts/index.json.jbuilder' do |content|
assert_match %r{json\.array! @posts, partial: "admin/posts/post", as: :post}, content
end

assert_file 'app/views/admin/posts/show.json.jbuilder' do |content|
assert_match %r{json\.partial! "admin/posts/post", post: @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)
Expand Down