Script 'mail_helper' called by obssrc
Hello community,
here is the log from the commit of package rubygem-jbuilder for
openSUSE:Factory checked in at 2021-02-20 22:12:05
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Comparing /work/SRC/openSUSE:Factory/rubygem-jbuilder (Old)
and /work/SRC/openSUSE:Factory/.rubygem-jbuilder.new.28504 (New)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "rubygem-jbuilder"
Sat Feb 20 22:12:05 2021 rev:24 rq:869948 version:2.11.2
Changes:
--------
--- /work/SRC/openSUSE:Factory/rubygem-jbuilder/rubygem-jbuilder.changes
2020-10-05 19:31:44.696852394 +0200
+++
/work/SRC/openSUSE:Factory/.rubygem-jbuilder.new.28504/rubygem-jbuilder.changes
2021-02-20 22:12:12.687034050 +0100
@@ -1,0 +2,7 @@
+Sat Feb 6 11:30:12 UTC 2021 - Manuel Schnitzer <[email protected]>
+
+- updated to version 2.11.2
+
+ * Improve key formatting for nested hashes and disable by default
+
+-------------------------------------------------------------------
Old:
----
jbuilder-2.10.1.gem
New:
----
jbuilder-2.11.2.gem
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Other differences:
------------------
++++++ rubygem-jbuilder.spec ++++++
--- /var/tmp/diff_new_pack.fuHi1J/_old 2021-02-20 22:12:13.099034487 +0100
+++ /var/tmp/diff_new_pack.fuHi1J/_new 2021-02-20 22:12:13.099034487 +0100
@@ -1,7 +1,7 @@
#
# spec file for package rubygem-jbuilder
#
-# Copyright (c) 2020 SUSE LLC
+# Copyright (c) 2021 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
@@ -24,7 +24,7 @@
#
Name: rubygem-jbuilder
-Version: 2.10.1
+Version: 2.11.2
Release: 0
%define mod_name jbuilder
%define mod_full_name %{mod_name}-%{version}
++++++ jbuilder-2.10.1.gem -> jbuilder-2.11.2.gem ++++++
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/CHANGELOG.md new/CHANGELOG.md
--- old/CHANGELOG.md 2020-09-12 16:41:21.000000000 +0200
+++ new/CHANGELOG.md 2021-01-27 13:37:14.000000000 +0100
@@ -1,12 +1,35 @@
# Changelog
+2.11.2
+------
+
+* [Improve key formatting for nested hashes and disable by
default](https://github.com/rails/jbuilder/pull/497)
+
+2.11.1
+------
+
+* Use symbols instead of strings for before_action filters [DHH]
+* Slim down comments in generated scaffold code [DHH]
+
+2.11.0
+------
+
+* [Allow jbuilder instance to be passed to
#merge!](https://github.com/rails/jbuilder/pull/485)
+* [Fix for key_format! when using nested
hashes](https://github.com/rails/jbuilder/pull/486)
+* [Include rich_text, attachment, and attachments fields in json
partial](https://github.com/rails/jbuilder/pull/459)
+
+2.10.2
+------
+
+* Update scaffold generator to use double quotes, 422 form error responds, and
modern string-of-arrays syntax [DHH]
+
2.10.1
-----------
+------
* Fix keyword arguments warning on Ruby 2.7
2.10.0
-----------
+------
* Requires Rails 5+ and Ruby 2.2+
* Nested hashes are deep-merged
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/CONTRIBUTING.md new/CONTRIBUTING.md
--- old/CONTRIBUTING.md 2020-09-12 16:41:21.000000000 +0200
+++ new/CONTRIBUTING.md 2021-01-27 13:37:14.000000000 +0100
@@ -13,7 +13,7 @@
#### Fork the Project
-Fork the [project on Github](https://github.com/rails/jbuilder) and check out
your copy.
+Fork the [project on GitHub](https://github.com/rails/jbuilder) and check out
your copy.
```
git clone https://github.com/contributor/jbuilder.git
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/README.md new/README.md
--- old/README.md 2020-09-12 16:41:21.000000000 +0200
+++ new/README.md 2021-01-27 13:37:14.000000000 +0100
@@ -274,6 +274,25 @@
Jbuilder.key_format camelize: :lower
```
+By default, key format is not applied to keys of hashes that are
+passed to methods like `set!`, `array!` or `merge!`. You can opt into
+deeply transforming these as well:
+
+``` ruby
+json.key_format! camelize: :lower
+json.deep_format_keys!
+json.settings([{some_value: "abc"}])
+
+# => { "settings": [{ "someValue": "abc" }]}
+```
+
+You can set this globally with the class method `deep_format_keys` (from
inside your
+environment.rb for example):
+
+``` ruby
+Jbuilder.deep_format_keys true
+```
+
## Contributing to Jbuilder
Jbuilder is the work of many contributors. You're encouraged to submit pull
requests, propose
Binary files old/checksums.yaml.gz and new/checksums.yaml.gz differ
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/jbuilder.gemspec new/jbuilder.gemspec
--- old/jbuilder.gemspec 2020-09-12 16:41:21.000000000 +0200
+++ new/jbuilder.gemspec 2021-01-27 13:37:14.000000000 +0100
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'jbuilder'
- s.version = '2.10.1'
+ s.version = '2.11.2'
s.authors = 'David Heinemeier Hansson'
s.email = '[email protected]'
s.summary = 'Create JSON structures via a Builder-style DSL'
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/lib/generators/rails/jbuilder_generator.rb
new/lib/generators/rails/jbuilder_generator.rb
--- old/lib/generators/rails/jbuilder_generator.rb 2020-09-12
16:41:21.000000000 +0200
+++ new/lib/generators/rails/jbuilder_generator.rb 2021-01-27
13:37:14.000000000 +0100
@@ -50,6 +50,10 @@
attributes.map { |a| ":#{a}"} * ', '
end
+
+ def virtual_attributes
+ attributes.select {|name| name.respond_to?(:virtual?) &&
name.virtual? }
+ end
end
end
end
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/lib/generators/rails/templates/api_controller.rb
new/lib/generators/rails/templates/api_controller.rb
--- old/lib/generators/rails/templates/api_controller.rb 2020-09-12
16:41:21.000000000 +0200
+++ new/lib/generators/rails/templates/api_controller.rb 2021-01-27
13:37:14.000000000 +0100
@@ -4,7 +4,7 @@
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
- before_action :set_<%= singular_table_name %>, only: [:show, :update,
:destroy]
+ before_action :set_<%= singular_table_name %>, only: %i[ show update destroy
]
# GET <%= route_url %>
# GET <%= route_url %>.json
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/lib/generators/rails/templates/controller.rb
new/lib/generators/rails/templates/controller.rb
--- old/lib/generators/rails/templates/controller.rb 2020-09-12
16:41:21.000000000 +0200
+++ new/lib/generators/rails/templates/controller.rb 2021-01-27
13:37:14.000000000 +0100
@@ -4,16 +4,14 @@
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
- before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update,
:destroy]
+ before_action :set_<%= singular_table_name %>, only: %i[ show edit update
destroy ]
- # GET <%= route_url %>
- # GET <%= route_url %>.json
+ # GET <%= route_url %> or <%= route_url %>.json
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
end
- # GET <%= route_url %>/1
- # GET <%= route_url %>/1.json
+ # GET <%= route_url %>/1 or <%= route_url %>/1.json
def show
end
@@ -26,42 +24,39 @@
def edit
end
- # POST <%= route_url %>
- # POST <%= route_url %>.json
+ # POST <%= route_url %> or <%= route_url %>.json
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name,
"#{singular_table_name}_params") %>
respond_to do |format|
if @<%= orm_instance.save %>
- format.html { redirect_to @<%= singular_table_name %>, notice: <%=
"'#{human_name} was successfully created.'" %> }
+ format.html { redirect_to @<%= singular_table_name %>, notice: <%=
%("#{human_name} was successfully created.") %> }
format.json { render :show, status: :created, location: <%=
"@#{singular_table_name}" %> }
else
- format.html { render :new }
+ format.html { render :new, status: :unprocessable_entity }
format.json { render json: <%= "@#{orm_instance.errors}" %>, status:
:unprocessable_entity }
end
end
end
- # PATCH/PUT <%= route_url %>/1
- # PATCH/PUT <%= route_url %>/1.json
+ # PATCH/PUT <%= route_url %>/1 or <%= route_url %>/1.json
def update
respond_to do |format|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
- format.html { redirect_to @<%= singular_table_name %>, notice: <%=
"'#{human_name} was successfully updated.'" %> }
+ format.html { redirect_to @<%= singular_table_name %>, notice: <%=
%("#{human_name} was successfully updated.") %> }
format.json { render :show, status: :ok, location: <%=
"@#{singular_table_name}" %> }
else
- format.html { render :edit }
+ format.html { render :edit, status: :unprocessable_entity }
format.json { render json: <%= "@#{orm_instance.errors}" %>, status:
:unprocessable_entity }
end
end
end
- # DELETE <%= route_url %>/1
- # DELETE <%= route_url %>/1.json
+ # DELETE <%= route_url %>/1 or <%= route_url %>/1.json
def destroy
@<%= orm_instance.destroy %>
respond_to do |format|
- format.html { redirect_to <%= index_helper %>_url, notice: <%=
"'#{human_name} was successfully destroyed.'" %> }
+ format.html { redirect_to <%= index_helper %>_url, notice: <%=
%("#{human_name} was successfully destroyed.") %> }
format.json { head :no_content }
end
end
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/lib/generators/rails/templates/partial.json.jbuilder
new/lib/generators/rails/templates/partial.json.jbuilder
--- old/lib/generators/rails/templates/partial.json.jbuilder 2020-09-12
16:41:21.000000000 +0200
+++ new/lib/generators/rails/templates/partial.json.jbuilder 2021-01-27
13:37:14.000000000 +0100
@@ -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 -%>
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/lib/jbuilder.rb new/lib/jbuilder.rb
--- old/lib/jbuilder.rb 2020-09-12 16:41:21.000000000 +0200
+++ new/lib/jbuilder.rb 2021-01-27 13:37:14.000000000 +0100
@@ -9,12 +9,14 @@
class Jbuilder
@@key_formatter = nil
@@ignore_nil = false
+ @@deep_format_keys = false
def initialize(options = {})
@attributes = {}
@key_formatter = options.fetch(:key_formatter){ @@key_formatter ?
@@key_formatter.clone : nil}
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)
+ @deep_format_keys = options.fetch(:deep_format_keys, @@deep_format_keys)
yield self if ::Kernel.block_given?
end
@@ -43,11 +45,11 @@
# json.age 32
# json.person another_jbuilder
# { "age": 32, "person": { ... }
- value.attributes!
+ _format_keys(value.attributes!)
else
# json.age 32
# { "age": 32 }
- value
+ _format_keys(value)
end
elsif _is_collection?(value)
# json.comments @post.comments, :content, :created_at
@@ -131,6 +133,31 @@
@@ignore_nil = value
end
+ # Deeply apply key format to nested hashes and arrays passed to
+ # methods like set!, merge! or array!.
+ #
+ # Example:
+ #
+ # json.key_format! camelize: :lower
+ # json.settings({some_value: "abc"})
+ #
+ # { "settings": { "some_value": "abc" }}
+ #
+ # json.key_format! camelize: :lower
+ # json.deep_format_keys!
+ # json.settings({some_value: "abc"})
+ #
+ # { "settings": { "someValue": "abc" }}
+ #
+ def deep_format_keys!(value = true)
+ @deep_format_keys = value
+ end
+
+ # Same as instance method deep_format_keys! except sets the default.
+ def self.deep_format_keys(value = true)
+ @@deep_format_keys = value
+ end
+
# Turns the current element into an array and yields a builder to add a hash.
#
# Example:
@@ -190,10 +217,10 @@
elsif attributes.any?
_map_collection(collection) { |element| extract! element, *attributes }
else
- collection.to_a
+ _format_keys(collection.to_a)
end
- merge! array
+ @attributes = _merge_values(@attributes, array)
end
# Extracts the mentioned attributes or hash elements from the passed object
and turns them into attributes of the JSON.
@@ -241,9 +268,10 @@
@attributes
end
- # Merges hash or array into current builder.
- def merge!(hash_or_array)
- @attributes = _merge_values(@attributes, hash_or_array)
+ # Merges hash, array, or Jbuilder instance into current builder.
+ def merge!(object)
+ hash_or_array = ::Jbuilder === object ? object.attributes! : object
+ @attributes = _merge_values(@attributes, _format_keys(hash_or_array))
end
# Encodes the current builder as JSON.
@@ -254,11 +282,11 @@
private
def _extract_hash_values(object, attributes)
- attributes.each{ |key| _set_value key, object.fetch(key) }
+ attributes.each{ |key| _set_value key, _format_keys(object.fetch(key)) }
end
def _extract_method_values(object, attributes)
- attributes.each{ |key| _set_value key, object.public_send(key) }
+ attributes.each{ |key| _set_value key,
_format_keys(object.public_send(key)) }
end
def _merge_block(key)
@@ -286,6 +314,18 @@
@key_formatter ? @key_formatter.format(key) : key.to_s
end
+ def _format_keys(hash_or_array)
+ return hash_or_array unless @deep_format_keys
+
+ if ::Array === hash_or_array
+ hash_or_array.map { |value| _format_keys(value) }
+ elsif ::Hash === hash_or_array
+ ::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }]
+ else
+ hash_or_array
+ end
+ end
+
def _set_value(key, value)
raise NullError.build(key) if @attributes.nil?
raise ArrayError.build(key) if ::Array === @attributes
@@ -301,12 +341,12 @@
end
def _scope
- parent_attributes, parent_formatter = @attributes, @key_formatter
+ parent_attributes, parent_formatter, parent_deep_format_keys =
@attributes, @key_formatter, @deep_format_keys
@attributes = BLANK
yield
@attributes
ensure
- @attributes, @key_formatter = parent_attributes, parent_formatter
+ @attributes, @key_formatter, @deep_format_keys = parent_attributes,
parent_formatter, parent_deep_format_keys
end
def _is_collection?(object)
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/metadata new/metadata
--- old/metadata 2020-09-12 16:41:21.000000000 +0200
+++ new/metadata 2021-01-27 13:37:14.000000000 +0100
@@ -1,14 +1,14 @@
--- !ruby/object:Gem::Specification
name: jbuilder
version: !ruby/object:Gem::Version
- version: 2.10.1
+ version: 2.11.2
platform: ruby
authors:
- David Heinemeier Hansson
-autorequire:
+autorequire:
bindir: bin
cert_chain: []
-date: 2020-09-12 00:00:00.000000000 Z
+date: 2021-01-27 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: activesupport
@@ -24,7 +24,7 @@
- - ">="
- !ruby/object:Gem::Version
version: 5.0.0
-description:
+description:
email: [email protected]
executables: []
extensions: []
@@ -71,7 +71,7 @@
licenses:
- MIT
metadata: {}
-post_install_message:
+post_install_message:
rdoc_options: []
require_paths:
- lib
@@ -87,7 +87,7 @@
version: '0'
requirements: []
rubygems_version: 3.1.2
-signing_key:
+signing_key:
specification_version: 4
summary: Create JSON structures via a Builder-style DSL
test_files:
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/test/jbuilder_generator_test.rb
new/test/jbuilder_generator_test.rb
--- old/test/jbuilder_generator_test.rb 2020-09-12 16:41:21.000000000 +0200
+++ new/test/jbuilder_generator_test.rb 2021-01-27 13:37:14.000000000 +0100
@@ -43,4 +43,16 @@
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
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/test/jbuilder_test.rb new/test/jbuilder_test.rb
--- old/test/jbuilder_test.rb 2020-09-12 16:41:21.000000000 +0200
+++ new/test/jbuilder_test.rb 2021-01-27 13:37:14.000000000 +0100
@@ -196,6 +196,18 @@
assert_equal 'Pavel', result['author']['name']
end
+ test 'support merge! method with Jbuilder instance' do
+ obj = jbuild do |json|
+ json.foo 'bar'
+ end
+
+ result = jbuild do |json|
+ json.merge! obj
+ end
+
+ assert_equal 'bar', result['foo']
+ end
+
test 'blocks are additive via extract syntax' do
person = Person.new('Pavel', 27)
@@ -554,6 +566,36 @@
assert_equal 'one', result['level1']
end
+ test 'key_format! can be changed in child elements' do
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+
+ json.level_one do
+ json.key_format! :upcase
+ json.value 'two'
+ end
+ end
+
+ assert_equal ['levelOne'], result.keys
+ assert_equal ['VALUE'], result['levelOne'].keys
+ end
+
+ test 'key_format! can be changed in array!' do
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+
+ json.level_one do
+ json.array! [{value: 'two'}] do |object|
+ json.key_format! :upcase
+ json.value object[:value]
+ end
+ end
+ end
+
+ assert_equal ['levelOne'], result.keys
+ assert_equal ['VALUE'], result['levelOne'][0].keys
+ end
+
test 'key_format! with no parameter' do
result = jbuild do |json|
json.key_format! :upcase
@@ -581,6 +623,161 @@
assert_equal ['oats and friends'], result.keys
end
+ test 'key_format! is not applied deeply by default' do
+ names = { first_name: 'camel', last_name: 'case' }
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.set! :all_names, names
+ end
+
+ assert_equal %i[first_name last_name], result['allNames'].keys
+ end
+
+ test 'applying key_format! deeply can be enabled per scope' do
+ names = { first_name: 'camel', last_name: 'case' }
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.scope do
+ json.deep_format_keys!
+ json.set! :all_names, names
+ end
+ json.set! :all_names, names
+ end
+
+ assert_equal %w[firstName lastName], result['scope']['allNames'].keys
+ assert_equal %i[first_name last_name], result['allNames'].keys
+ end
+
+ test 'applying key_format! deeply can be disabled per scope' do
+ names = { first_name: 'camel', last_name: 'case' }
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.set! :all_names, names
+ json.scope do
+ json.deep_format_keys! false
+ json.set! :all_names, names
+ end
+ end
+
+ assert_equal %w[firstName lastName], result['allNames'].keys
+ assert_equal %i[first_name last_name], result['scope']['allNames'].keys
+ end
+
+ test 'applying key_format! deeply can be enabled globally' do
+ names = { first_name: 'camel', last_name: 'case' }
+
+ Jbuilder.deep_format_keys true
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.set! :all_names, names
+ end
+
+ assert_equal %w[firstName lastName], result['allNames'].keys
+ Jbuilder.send(:class_variable_set, '@@deep_format_keys', false)
+ end
+
+ test 'deep key_format! with merge!' do
+ hash = { camel_style: 'for JS' }
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.merge! hash
+ end
+
+ assert_equal ['camelStyle'], result.keys
+ end
+
+ test 'deep key_format! with merge! deep' do
+ hash = { camel_style: { sub_attr: 'for JS' } }
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.merge! hash
+ end
+
+ assert_equal ['subAttr'], result['camelStyle'].keys
+ end
+
+ test 'deep key_format! with set! array of hashes' do
+ names = [{ first_name: 'camel', last_name: 'case' }]
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.set! :names, names
+ end
+
+ assert_equal %w[firstName lastName], result['names'][0].keys
+ end
+
+ test 'deep key_format! with set! extracting hash from object' do
+ comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case'
})
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.set! :comment, comment, :author
+ end
+
+ assert_equal %w[firstName lastName], result['comment']['author'].keys
+ end
+
+ test 'deep key_format! with array! of hashes' do
+ names = [{ first_name: 'camel', last_name: 'case' }]
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.array! names
+ end
+
+ assert_equal %w[firstName lastName], result[0].keys
+ end
+
+ test 'deep key_format! with merge! array of hashes' do
+ names = [{ first_name: 'camel', last_name: 'case' }]
+ new_names = [{ first_name: 'snake', last_name: 'case' }]
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.array! names
+ json.merge! new_names
+ end
+
+ assert_equal %w[firstName lastName], result[1].keys
+ end
+
+ test 'deep key_format! is applied to hash extracted from object' do
+ comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case'
})
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.extract! comment, :author
+ end
+
+ assert_equal %w[firstName lastName], result['author'].keys
+ end
+
+ test 'deep key_format! is applied to hash extracted from hash' do
+ comment = {author: { first_name: 'camel', last_name: 'case' }}
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.extract! comment, :author
+ end
+
+ assert_equal %w[firstName lastName], result['author'].keys
+ end
+
+ test 'deep key_format! is applied to hash extracted directly from array' do
+ comments = [Struct.new(:author).new({ first_name: 'camel', last_name:
'case' })]
+ result = jbuild do |json|
+ json.key_format! camelize: :lower
+ json.deep_format_keys!
+ json.array! comments, :author
+ end
+
+ assert_equal %w[firstName lastName], result[0]['author'].keys
+ end
+
test 'default key_format!' do
Jbuilder.key_format camelize: :lower
result = jbuild{ |json| json.camel_style 'for JS' }
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/test/scaffold_api_controller_generator_test.rb
new/test/scaffold_api_controller_generator_test.rb
--- old/test/scaffold_api_controller_generator_test.rb 2020-09-12
16:41:21.000000000 +0200
+++ new/test/scaffold_api_controller_generator_test.rb 2021-01-27
13:37:14.000000000 +0100
@@ -55,5 +55,16 @@
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
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/test/scaffold_controller_generator_test.rb
new/test/scaffold_controller_generator_test.rb
--- old/test/scaffold_controller_generator_test.rb 2020-09-12
16:41:21.000000000 +0200
+++ new/test/scaffold_controller_generator_test.rb 2021-01-27
13:37:14.000000000 +0100
@@ -31,22 +31,22 @@
assert_instance_method :create, content do |m|
assert_match %r{@post = Post\.new\(post_params\)}, m
assert_match %r{@post\.save}, m
- assert_match %r{format\.html \{ redirect_to @post, notice: 'Post was
successfully created\.' \}}, m
+ assert_match %r{format\.html \{ redirect_to @post, notice: "Post was
successfully created\." \}}, m
assert_match %r{format\.json \{ render :show, status: :created,
location: @post \}}, m
- assert_match %r{format\.html \{ render :new \}}, m
+ assert_match %r{format\.html \{ render :new, status:
:unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post\.errors, status:
:unprocessable_entity \}}, m
end
assert_instance_method :update, content do |m|
- assert_match %r{format\.html \{ redirect_to @post, notice: 'Post was
successfully updated\.' \}}, m
+ assert_match %r{format\.html \{ redirect_to @post, notice: "Post was
successfully updated\." \}}, m
assert_match %r{format\.json \{ render :show, status: :ok, location:
@post \}}, m
- assert_match %r{format\.html \{ render :edit \}}, m
+ assert_match %r{format\.html \{ render :edit, status:
:unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post.errors, status:
:unprocessable_entity \}}, m
end
assert_instance_method :destroy, content do |m|
assert_match %r{@post\.destroy}, m
- assert_match %r{format\.html \{ redirect_to posts_url, notice: 'Post
was successfully destroyed\.' \}}, m
+ assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post
was successfully destroyed\." \}}, m
assert_match %r{format\.json \{ head :no_content \}}, m
end
@@ -67,4 +67,14 @@
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