jb 0.7.1 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +63 -0
- data/Gemfile +13 -0
- data/README.md +27 -22
- data/jb.gemspec +2 -3
- data/lib/jb/action_view_legacy_monkeys.rb +2 -2
- data/lib/jb/action_view_monkeys.rb +38 -4
- data/lib/jb/railtie.rb +0 -1
- data/lib/jb/version.rb +1 -1
- metadata +15 -20
- data/.travis.yml +0 -24
- data/gemfiles/rails_42.gemfile +0 -8
- data/gemfiles/rails_50.gemfile +0 -7
- data/gemfiles/rails_51.gemfile +0 -7
- data/gemfiles/rails_52.gemfile +0 -7
- data/gemfiles/rails_60.gemfile +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59c07bc2a0d9dd1b6e304a1f6c3037ccd2234cda1a5273ff8f0d12bb1e814270
|
4
|
+
data.tar.gz: 545f3b027f587ab30de9d2e93fad5b3ea399ad8a6695b9090d3e7b55085ab505
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9ef88b516a5bee2132af07f394a00f03bfda2928764b538189aa061d9af1c60c4942cb874d416e5d36265839bac1df8463cb5c8e795536000a903bbbecadc25
|
7
|
+
data.tar.gz: e0b272e833109cd995104fa033208eca904922a3ef66148dcbe583665917393efd99120800605ae9c35c3527f2530ee96762ee5bb991f650538f7dd0c896fab7
|
@@ -0,0 +1,63 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
pull_request:
|
6
|
+
schedule:
|
7
|
+
- cron: '26 13 * * *'
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
strategy:
|
12
|
+
matrix:
|
13
|
+
ruby_version: [ruby-head, '3.2', '3.1', '3.0', '2.7']
|
14
|
+
rails_version: [edge, '7.0', '6.1']
|
15
|
+
|
16
|
+
include:
|
17
|
+
- ruby_version: '3.0'
|
18
|
+
rails_version: '6.0'
|
19
|
+
|
20
|
+
- ruby_version: '2.7'
|
21
|
+
rails_version: '6.0'
|
22
|
+
|
23
|
+
- ruby_version: '2.6'
|
24
|
+
rails_version: '6.1'
|
25
|
+
- ruby_version: '2.6'
|
26
|
+
rails_version: '6.0'
|
27
|
+
- ruby_version: '2.6'
|
28
|
+
rails_version: '5.2'
|
29
|
+
- ruby_version: '2.6'
|
30
|
+
rails_version: '5.1'
|
31
|
+
- ruby_version: '2.6'
|
32
|
+
rails_version: '5.0'
|
33
|
+
- ruby_version: '2.6'
|
34
|
+
rails_version: '4.2'
|
35
|
+
bundler_version: '1'
|
36
|
+
|
37
|
+
- ruby_version: '2.5'
|
38
|
+
rails_version: '5.2'
|
39
|
+
- ruby_version: '2.5'
|
40
|
+
rails_version: '4.2'
|
41
|
+
bundler_version: '1'
|
42
|
+
|
43
|
+
- ruby_version: '2.4'
|
44
|
+
rails_version: '5.2'
|
45
|
+
- ruby_version: '2.4'
|
46
|
+
rails_version: '4.2'
|
47
|
+
bundler_version: '1'
|
48
|
+
|
49
|
+
env:
|
50
|
+
RAILS_VERSION: ${{ matrix.rails_version }}
|
51
|
+
|
52
|
+
runs-on: ubuntu-latest
|
53
|
+
|
54
|
+
steps:
|
55
|
+
- uses: actions/checkout@v4
|
56
|
+
- uses: ruby/setup-ruby@v1
|
57
|
+
with:
|
58
|
+
ruby-version: ${{ matrix.ruby_version }}
|
59
|
+
bundler: ${{ matrix.bundler_version }}
|
60
|
+
bundler-cache: true
|
61
|
+
continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.allow_failures == 'true') }}
|
62
|
+
- run: bundle exec rake
|
63
|
+
continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.allow_failures == 'true') }}
|
data/Gemfile
CHANGED
@@ -5,5 +5,18 @@ source 'https://rubygems.org'
|
|
5
5
|
# Specify your gem's dependencies in jb.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
+
if ENV['RAILS_VERSION'] == 'edge'
|
9
|
+
gem 'rails', git: 'https://github.com/rails/rails.git', branch: 'main'
|
10
|
+
elsif ENV['RAILS_VERSION']
|
11
|
+
gem 'rails', "~> #{ENV['RAILS_VERSION']}.0"
|
12
|
+
else
|
13
|
+
gem 'rails'
|
14
|
+
end
|
15
|
+
|
16
|
+
gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
|
17
|
+
gem 'loofah', RUBY_VERSION < '2.5' ? '< 2.21.0' : '>= 0'
|
18
|
+
gem 'selenium-webdriver'
|
19
|
+
gem 'net-smtp' if RUBY_VERSION >= '3.1'
|
20
|
+
|
8
21
|
# For benchmarking
|
9
22
|
gem 'action_args'
|
data/README.md
CHANGED
@@ -21,19 +21,34 @@ Put a template file named `*.jb` in your Rails app's `app/views/*` directory, an
|
|
21
21
|
|
22
22
|
## Features
|
23
23
|
|
24
|
-
* No
|
24
|
+
* No original builder syntax that you have to learn
|
25
25
|
* No `method_missing` calls
|
26
26
|
* `render_partial` with :collection option actually renders the collection (unlike Jbuilder)
|
27
27
|
|
28
28
|
|
29
29
|
## Syntax
|
30
30
|
|
31
|
-
A `.jb` template should contain Ruby code that returns any Ruby Object that
|
32
|
-
Then the return value will be `to_json`ed to a JSON String.
|
31
|
+
A `.jb` template should contain Ruby code that returns any Ruby Object that responds\_to `to_json` (generally Hash or Array). Then the return value will be `to_json`ed to a JSON String.
|
33
32
|
|
34
33
|
|
35
34
|
## Examples
|
36
35
|
|
36
|
+
Let's start with a very simple one. Just write a Ruby Hash as a template:
|
37
|
+
|
38
|
+
``` ruby
|
39
|
+
{language: 'Ruby', author: {name: 'Matz'}}
|
40
|
+
```
|
41
|
+
|
42
|
+
This renders the following JSON text:
|
43
|
+
|
44
|
+
``` javascript
|
45
|
+
{"language": "Ruby", "author": {"name": "Matz"}}
|
46
|
+
```
|
47
|
+
|
48
|
+
Note that modern Ruby Hash syntax pretty much looks alike JSON syntax. It's super-straight forward. Who needs a DSL to do this?
|
49
|
+
|
50
|
+
Next one is a little bit advanced usage. The template doesn't have to be a single literal but can be any code that returns a Hash object:
|
51
|
+
|
37
52
|
``` ruby
|
38
53
|
# app/views/messages/show.json.jb
|
39
54
|
|
@@ -97,18 +112,17 @@ This will build the following structure:
|
|
97
112
|
}
|
98
113
|
```
|
99
114
|
|
100
|
-
|
101
|
-
Note that modern Ruby Hash syntax pretty much looks alike JSON syntax.
|
102
|
-
It's super-straight forward. Who needs a DSL to do this?
|
115
|
+
If you want to define attribute and structure names dynamically, of course you still can do this with a Ruby Hash literal.
|
103
116
|
|
104
117
|
``` ruby
|
105
|
-
|
118
|
+
# model_name, column_name = :author, :name
|
119
|
+
|
120
|
+
{model_name => {column_name => 'Matz'}}
|
106
121
|
|
107
122
|
# => {"author": {"name": "Matz"}}
|
108
123
|
```
|
109
124
|
|
110
|
-
Top level arrays can be handled directly.
|
111
|
-
And you know, Ruby is such a powerful language for manipulating collections:
|
125
|
+
Top level arrays can be handled directly. Useful for index and other collection actions. And you know, Ruby is such a powerful language for manipulating collections:
|
112
126
|
|
113
127
|
``` ruby
|
114
128
|
# @comments = @post.comments
|
@@ -136,11 +150,7 @@ Jb has no special DSL method for extracting attributes from array directly, but
|
|
136
150
|
# => [{"id": 1, "name": "Matz"}, {"id": 2, "name": "Nobu"}]
|
137
151
|
```
|
138
152
|
|
139
|
-
You can use Jb directly as an Action View template language.
|
140
|
-
When required in Rails, you can create views ala show.json.jb.
|
141
|
-
You'll notice in the following example that the `.jb` template
|
142
|
-
doesn't have to be one big Ruby Hash literal as a whole
|
143
|
-
but it can be any Ruby code that finally returns a Hash instance.
|
153
|
+
You can use Jb directly as an Action View template language. When required in Rails, you can create views ala `show.json.jb`. You'll notice in the following example that the `.jb` template doesn't have to be one big Ruby Hash literal as a whole but it can be any Ruby code that finally returns a Hash instance.
|
144
154
|
|
145
155
|
``` ruby
|
146
156
|
# Any helpers available to views are available in the template
|
@@ -163,10 +173,7 @@ end
|
|
163
173
|
json
|
164
174
|
```
|
165
175
|
|
166
|
-
You can use partials as well. The following will render the file
|
167
|
-
`views/comments/_comments.json.jb`, and set a local variable
|
168
|
-
`comments` with all this message's comments, which you can use inside
|
169
|
-
the partial.
|
176
|
+
You can use partials as well. The following will render the file `views/comments/_comments.json.jb`, and set a local variable `comments` with all this message's comments, which you can use inside the partial.
|
170
177
|
|
171
178
|
```ruby
|
172
179
|
render 'comments/comments', comments: @message.comments
|
@@ -178,8 +185,7 @@ It's also possible to render collections of partials:
|
|
178
185
|
render partial: 'posts/post', collection: @posts, as: :post
|
179
186
|
```
|
180
187
|
|
181
|
-
> NOTE: Don't use `render @post.comments` because if the collection is empty,
|
182
|
-
`render` will return `nil` instead of an empty array.
|
188
|
+
> NOTE: Don't use `render @post.comments` because if the collection is empty, `render` will return `nil` instead of an empty array.
|
183
189
|
|
184
190
|
You can pass any objects into partial templates with or without `:locals` option.
|
185
191
|
|
@@ -211,8 +217,7 @@ end
|
|
211
217
|
|
212
218
|
|
213
219
|
## The Generator
|
214
|
-
Jb extends the default Rails scaffold generator and adds some
|
215
|
-
If you don't need them, please configure like so.
|
220
|
+
Jb extends the default Rails scaffold generator and adds some `.jb` templates. If you don't need them, please configure like so.
|
216
221
|
|
217
222
|
```ruby
|
218
223
|
Rails.application.config.generators.jb false
|
data/jb.gemspec
CHANGED
@@ -21,12 +21,11 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
-
spec.add_dependency 'multi_json'
|
25
|
-
|
26
24
|
spec.add_development_dependency 'bundler'
|
27
25
|
spec.add_development_dependency 'rake'
|
28
26
|
spec.add_development_dependency 'test-unit-rails'
|
29
27
|
spec.add_development_dependency 'rails'
|
30
28
|
spec.add_development_dependency 'action_args'
|
31
|
-
spec.add_development_dependency '
|
29
|
+
spec.add_development_dependency 'debug'
|
30
|
+
spec.add_development_dependency 'selenium-webdriver'
|
32
31
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
#
|
2
|
+
# Monkey-patches for Action View 4 and 5
|
3
3
|
|
4
4
|
module Jb
|
5
5
|
module PartialRenderer
|
@@ -44,7 +44,7 @@ module Jb
|
|
44
44
|
module TemplateRenderer
|
45
45
|
module JSONizer
|
46
46
|
def render_template(template, *)
|
47
|
-
template.respond_to?(:handler) && (template.handler == Jb::Handler) ?
|
47
|
+
template.respond_to?(:handler) && (template.handler == Jb::Handler) ? super.to_json : super
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
#
|
2
|
+
# Monkey-patches for Action View 6+
|
3
3
|
|
4
4
|
module Jb
|
5
5
|
# A monkey-patch that converts non-partial result to a JSON String
|
@@ -7,13 +7,41 @@ module Jb
|
|
7
7
|
module JSONizer
|
8
8
|
def render_template(_view, template, *)
|
9
9
|
rendered_template = super
|
10
|
-
rendered_template.instance_variable_set :@body,
|
10
|
+
rendered_template.instance_variable_set :@body, rendered_template.body.to_json if template.respond_to?(:handler) && (template.handler == Jb::Handler)
|
11
11
|
rendered_template
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
# A monkey-patch
|
16
|
+
# Rails 7.1+: A monkey-patch not to stringify rendered object from JB templates
|
17
|
+
module BaseToSCanceller
|
18
|
+
def _run(method, template, *, **)
|
19
|
+
val = super
|
20
|
+
if template.respond_to?(:handler) && (template.handler == Jb::Handler)
|
21
|
+
def val.to_s
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
val
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Rails 6.1+: A monkey-patch for jb template collection result's `body` not to return a String but an Array
|
30
|
+
module CollectionRendererExtension
|
31
|
+
private def render_collection(_collection, _view, _path, template, _layout, _block)
|
32
|
+
obj = super
|
33
|
+
if template.respond_to?(:handler) && (template.handler == Jb::Handler)
|
34
|
+
if ActionView::AbstractRenderer::RenderedCollection::EmptyCollection === obj
|
35
|
+
def obj.body; []; end
|
36
|
+
else
|
37
|
+
def obj.body; @rendered_templates.map(&:body); end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
obj
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Rails 6.0: A monkey-patch for jb template collection result's `body` not to return a String but an Array
|
17
45
|
module PartialRendererExtension
|
18
46
|
private def render_collection(_view, template)
|
19
47
|
obj = super
|
@@ -30,4 +58,10 @@ module Jb
|
|
30
58
|
end
|
31
59
|
|
32
60
|
::ActionView::TemplateRenderer.prepend ::Jb::TemplateRenderer::JSONizer
|
33
|
-
::ActionView::
|
61
|
+
::ActionView::Base.prepend ::Jb::BaseToSCanceller if (ActionView::VERSION::MAJOR >= 7) && (ActionView::VERSION::MINOR >= 1)
|
62
|
+
begin
|
63
|
+
# ActionView::CollectionRenderer is a newly added class since 6.1
|
64
|
+
::ActionView::CollectionRenderer.prepend ::Jb::CollectionRendererExtension
|
65
|
+
rescue NameError
|
66
|
+
::ActionView::PartialRenderer.prepend ::Jb::PartialRendererExtension
|
67
|
+
end
|
data/lib/jb/railtie.rb
CHANGED
data/lib/jb/version.rb
CHANGED
metadata
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
|
-
type: :
|
20
|
+
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: test-unit-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rails
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: action_args
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: debug
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: selenium-webdriver
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -115,8 +115,8 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- ".github/workflows/main.yml"
|
118
119
|
- ".gitignore"
|
119
|
-
- ".travis.yml"
|
120
120
|
- Gemfile
|
121
121
|
- LICENSE.txt
|
122
122
|
- README.md
|
@@ -125,11 +125,6 @@ files:
|
|
125
125
|
- bin/console
|
126
126
|
- bin/setup
|
127
127
|
- gemfiles/benchmark.gemfile
|
128
|
-
- gemfiles/rails_42.gemfile
|
129
|
-
- gemfiles/rails_50.gemfile
|
130
|
-
- gemfiles/rails_51.gemfile
|
131
|
-
- gemfiles/rails_52.gemfile
|
132
|
-
- gemfiles/rails_60.gemfile
|
133
128
|
- jb.gemspec
|
134
129
|
- lib/generators/rails/jb_generator.rb
|
135
130
|
- lib/generators/rails/scaffold_controller_generator.rb
|
@@ -145,7 +140,7 @@ homepage: https://github.com/amatsuda/jb
|
|
145
140
|
licenses:
|
146
141
|
- MIT
|
147
142
|
metadata: {}
|
148
|
-
post_install_message:
|
143
|
+
post_install_message:
|
149
144
|
rdoc_options: []
|
150
145
|
require_paths:
|
151
146
|
- lib
|
@@ -160,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
155
|
- !ruby/object:Gem::Version
|
161
156
|
version: '0'
|
162
157
|
requirements: []
|
163
|
-
rubygems_version: 3.0.
|
164
|
-
signing_key:
|
158
|
+
rubygems_version: 3.5.0.dev
|
159
|
+
signing_key:
|
165
160
|
specification_version: 4
|
166
161
|
summary: Faster and simpler Jbuilder alternative
|
167
162
|
test_files: []
|
data/.travis.yml
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
|
3
|
-
rvm:
|
4
|
-
- 2.7.0
|
5
|
-
- 2.6.5
|
6
|
-
- 2.5.7
|
7
|
-
|
8
|
-
gemfile:
|
9
|
-
- gemfiles/rails_60.gemfile
|
10
|
-
- gemfiles/rails_52.gemfile
|
11
|
-
- gemfiles/rails_51.gemfile
|
12
|
-
- gemfiles/rails_50.gemfile
|
13
|
-
- gemfiles/rails_42.gemfile
|
14
|
-
|
15
|
-
matrix:
|
16
|
-
exclude:
|
17
|
-
- rvm: 2.7.0
|
18
|
-
gemfile: gemfiles/rails_52.gemfile
|
19
|
-
- rvm: 2.7.0
|
20
|
-
gemfile: gemfiles/rails_51.gemfile
|
21
|
-
- rvm: 2.7.0
|
22
|
-
gemfile: gemfiles/rails_50.gemfile
|
23
|
-
- rvm: 2.7.0
|
24
|
-
gemfile: gemfiles/rails_42.gemfile
|
data/gemfiles/rails_42.gemfile
DELETED
data/gemfiles/rails_50.gemfile
DELETED
data/gemfiles/rails_51.gemfile
DELETED
data/gemfiles/rails_52.gemfile
DELETED