diff --git a/lib/generators/rails/templates/index.json.jbuilder b/lib/generators/rails/templates/index.json.jbuilder index 955b095..beb0142 100644 --- a/lib/generators/rails/templates/index.json.jbuilder +++ b/lib/generators/rails/templates/index.json.jbuilder @@ -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 %> \ No newline at end of file diff --git a/lib/generators/rails/templates/show.json.jbuilder b/lib/generators/rails/templates/show.json.jbuilder index 4723e6b..8126420 100644 --- a/lib/generators/rails/templates/show.json.jbuilder +++ b/lib/generators/rails/templates/show.json.jbuilder @@ -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 %> \ No newline at end of file diff --git a/test/jbuilder_generator_with_namespace_test.rb b/test/jbuilder_generator_with_namespace_test.rb new file mode 100644 index 0000000..7e21a38 --- /dev/null +++ b/test/jbuilder_generator_with_namespace_test.rb @@ -0,0 +1,36 @@ +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 + end +end