jb 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +63 -0
- data/.gitignore +1 -0
- data/Gemfile +13 -0
- data/README.md +27 -22
- data/jb.gemspec +2 -1
- data/lib/jb/action_view_legacy_monkeys.rb +1 -1
- data/lib/jb/action_view_monkeys.rb +18 -1
- data/lib/jb/version.rb +1 -1
- metadata +19 -11
- data/.travis.yml +0 -33
- 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
- data/gemfiles/rails_edge.gemfile +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8532f278227b3fc1282898874bf192c8ccf72d8fd3c1d40f1fb4d2751c1894f
|
4
|
+
data.tar.gz: 395b638d4565433fa519951b12a906f32448d7c4a35a531f099679e0ed643366
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a1e9e60aee2347cc897ec6ec10061faf4b008b840dc6c95de373ca3d3ec1544853487c0be54b2a2777713dbff5b15a4fc35af04d25d106f6ba908db896af09c
|
7
|
+
data.tar.gz: c696aacf1ed03636f984980ab60a5a5b53eab6d4c54ad0562392daae1e32dc82464a59073e57c6061414634e6c49a8250787cda15c90be90dce7395e91a699f4
|
@@ -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.1', '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/.gitignore
CHANGED
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
@@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency 'test-unit-rails'
|
27
27
|
spec.add_development_dependency 'rails'
|
28
28
|
spec.add_development_dependency 'action_args'
|
29
|
-
spec.add_development_dependency '
|
29
|
+
spec.add_development_dependency 'debug'
|
30
|
+
spec.add_development_dependency 'selenium-webdriver'
|
30
31
|
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
|
@@ -13,6 +13,22 @@ module Jb
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
# A wrapper class for template result that makes `to_s` method do nothing
|
17
|
+
class TemplateResult < SimpleDelegator
|
18
|
+
def to_s
|
19
|
+
__getobj__
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Rails 7.1+: A monkey-patch not to stringify rendered object from JB templates
|
24
|
+
module TemlateResultCaster
|
25
|
+
def _run(method, template, *, **)
|
26
|
+
val = super
|
27
|
+
val = Jb::TemplateResult.new val if template.respond_to?(:handler) && (template.handler == Jb::Handler)
|
28
|
+
val
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
16
32
|
# Rails 6.1+: A monkey-patch for jb template collection result's `body` not to return a String but an Array
|
17
33
|
module CollectionRendererExtension
|
18
34
|
private def render_collection(_collection, _view, _path, template, _layout, _block)
|
@@ -45,6 +61,7 @@ module Jb
|
|
45
61
|
end
|
46
62
|
|
47
63
|
::ActionView::TemplateRenderer.prepend ::Jb::TemplateRenderer::JSONizer
|
64
|
+
::ActionView::Base.prepend ::Jb::TemlateResultCaster if (ActionView::VERSION::MAJOR >= 7) && (ActionView::VERSION::MINOR >= 1)
|
48
65
|
begin
|
49
66
|
# ActionView::CollectionRenderer is a newly added class since 6.1
|
50
67
|
::ActionView::CollectionRenderer.prepend ::Jb::CollectionRendererExtension
|
data/lib/jb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,7 +81,21 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: debug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: selenium-webdriver
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -101,8 +115,8 @@ executables: []
|
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
103
117
|
files:
|
118
|
+
- ".github/workflows/main.yml"
|
104
119
|
- ".gitignore"
|
105
|
-
- ".travis.yml"
|
106
120
|
- Gemfile
|
107
121
|
- LICENSE.txt
|
108
122
|
- README.md
|
@@ -111,12 +125,6 @@ files:
|
|
111
125
|
- bin/console
|
112
126
|
- bin/setup
|
113
127
|
- gemfiles/benchmark.gemfile
|
114
|
-
- gemfiles/rails_42.gemfile
|
115
|
-
- gemfiles/rails_50.gemfile
|
116
|
-
- gemfiles/rails_51.gemfile
|
117
|
-
- gemfiles/rails_52.gemfile
|
118
|
-
- gemfiles/rails_60.gemfile
|
119
|
-
- gemfiles/rails_edge.gemfile
|
120
128
|
- jb.gemspec
|
121
129
|
- lib/generators/rails/jb_generator.rb
|
122
130
|
- lib/generators/rails/scaffold_controller_generator.rb
|
@@ -147,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
155
|
- !ruby/object:Gem::Version
|
148
156
|
version: '0'
|
149
157
|
requirements: []
|
150
|
-
rubygems_version: 3.
|
158
|
+
rubygems_version: 3.5.0.dev
|
151
159
|
signing_key:
|
152
160
|
specification_version: 4
|
153
161
|
summary: Faster and simpler Jbuilder alternative
|
data/.travis.yml
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
|
3
|
-
rvm:
|
4
|
-
- 2.7.2
|
5
|
-
- 2.6.6
|
6
|
-
- 2.5.8
|
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.2
|
18
|
-
gemfile: gemfiles/rails_52.gemfile
|
19
|
-
- rvm: 2.7.2
|
20
|
-
gemfile: gemfiles/rails_51.gemfile
|
21
|
-
- rvm: 2.7.2
|
22
|
-
gemfile: gemfiles/rails_50.gemfile
|
23
|
-
- rvm: 2.7.2
|
24
|
-
gemfile: gemfiles/rails_42.gemfile
|
25
|
-
|
26
|
-
include:
|
27
|
-
- rvm: 2.7.2
|
28
|
-
gemfile: gemfiles/rails_edge.gemfile
|
29
|
-
- rvm: ruby-head
|
30
|
-
gemfile: gemfiles/rails_edge.gemfile
|
31
|
-
|
32
|
-
allow_failures:
|
33
|
-
- rvm: ruby-head
|
data/gemfiles/rails_42.gemfile
DELETED
data/gemfiles/rails_50.gemfile
DELETED
data/gemfiles/rails_51.gemfile
DELETED
data/gemfiles/rails_52.gemfile
DELETED
data/gemfiles/rails_60.gemfile
DELETED
data/gemfiles/rails_edge.gemfile
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
gemspec :path => '../'
|
6
|
-
|
7
|
-
git_source(:github) do |repo_name|
|
8
|
-
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
|
9
|
-
"https://github.com/#{repo_name}.git"
|
10
|
-
end
|
11
|
-
|
12
|
-
github 'rails/rails' do
|
13
|
-
gem 'rails'
|
14
|
-
end
|
15
|
-
|
16
|
-
gem 'selenium-webdriver'
|