Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
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: '<%= plural_table_name.gsub('_', '/') %>/<%= singular_table_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! "<%= plural_table_name.gsub('_', '/') %>/<%= singular_table_name %>", <%= singular_table_name %>: @<%= singular_table_name %>
38 changes: 38 additions & 0 deletions test/jbuilder_generator_with_namespace_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'test_helper'
require 'rails/generators/test_case'
require 'generators/rails/jbuilder_generator'

class JbuilderGeneratorWithNamespaceTest < Rails::Generators::TestCase
tests Rails::Generators::JbuilderGenerator
arguments %w(api/foo bar:integer baz:string)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination

test 'all views are generated' do
run_generator

%w(index show).each do |view|
assert_file "app/views/api/foos/#{view}.json.jbuilder"
end
assert_file "app/views/api/foos/_api_foo.json.jbuilder"
end

test 'the files are correctly structured to work' do
run_generator

assert_file 'app/views/api/foos/index.json.jbuilder' do |content|
assert_match %r{json.array! @api_foos, partial: 'api/foos/api_foo', as: :api_foo}, content
end

assert_file 'app/views/api/foos/show.json.jbuilder' do |content|
assert_match %r{json.partial! \"api/foos/api_foo\", api_foo: @api_foo}, content
end

assert_file 'app/views/api/foos/_api_foo.json.jbuilder' do |content|
assert_match %r{json\.extract! api_foo, :id, :bar, :baz, :created_at, :updated_at}, content
assert_match %r{json\.url api_foo_url\(api_foo, format: :json\)}, content
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove those blank lines?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the lines after 34 on the test?


end
end