jb 0.5.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 +2 -0
- data/Gemfile +14 -2
- data/README.md +29 -25
- data/Rakefile +1 -0
- data/bin/benchmark.sh +1 -1
- data/gemfiles/benchmark.gemfile +10 -0
- data/jb.gemspec +5 -4
- data/lib/generators/rails/jb_generator.rb +1 -0
- data/lib/generators/rails/scaffold_controller_generator.rb +2 -1
- data/lib/jb/action_view_legacy_monkeys.rb +54 -0
- data/lib/jb/action_view_monkeys.rb +51 -35
- data/lib/jb/handler.rb +3 -2
- data/lib/jb/railtie.rb +21 -1
- data/lib/jb/version.rb +2 -1
- data/lib/jb.rb +1 -0
- metadata +34 -19
- data/.travis.yml +0 -5
    
        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
    
    | @@ -1,10 +1,22 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 2 3 | 
             
            source 'https://rubygems.org'
         | 
| 3 4 |  | 
| 4 5 | 
             
            # Specify your gem's dependencies in jb.gemspec
         | 
| 5 6 | 
             
            gemspec
         | 
| 6 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 | 
            +
             | 
| 7 21 | 
             
            # For benchmarking
         | 
| 8 | 
            -
            gem 'jbuilder'
         | 
| 9 | 
            -
            gem 'benchmark-ips'
         | 
| 10 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
         | 
| @@ -176,12 +183,10 @@ It's also possible to render collections of partials: | |
| 176 183 |  | 
| 177 184 | 
             
            ```ruby
         | 
| 178 185 | 
             
            render partial: 'posts/post', collection: @posts, as: :post
         | 
| 179 | 
            -
             | 
| 180 | 
            -
            # or
         | 
| 181 | 
            -
             | 
| 182 | 
            -
            render @post.comments
         | 
| 183 186 | 
             
            ```
         | 
| 184 187 |  | 
| 188 | 
            +
            > NOTE: Don't use `render @post.comments` because if the collection is empty, `render` will return `nil` instead of an empty array.
         | 
| 189 | 
            +
             | 
| 185 190 | 
             
            You can pass any objects into partial templates with or without `:locals` option.
         | 
| 186 191 |  | 
| 187 192 | 
             
            ```ruby
         | 
| @@ -194,7 +199,7 @@ render 'sub_template', user: user | |
| 194 199 |  | 
| 195 200 | 
             
            You can of course include Ruby `nil` as a Hash value if you want. That would become `null` in the JSON.
         | 
| 196 201 |  | 
| 197 | 
            -
             | 
| 202 | 
            +
            You can use `Hash#compact`/`!` method to prevent including `null` values in the output:
         | 
| 198 203 |  | 
| 199 204 | 
             
            ```ruby
         | 
| 200 205 | 
             
            {foo: nil, bar: 'bar'}.compact
         | 
| @@ -212,8 +217,7 @@ end | |
| 212 217 |  | 
| 213 218 |  | 
| 214 219 | 
             
            ## The Generator
         | 
| 215 | 
            -
            Jb extends the default Rails scaffold generator and adds some  | 
| 216 | 
            -
            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.
         | 
| 217 221 |  | 
| 218 222 | 
             
            ```ruby
         | 
| 219 223 | 
             
            Rails.application.config.generators.jb false
         | 
    
        data/Rakefile
    CHANGED
    
    
    
        data/bin/benchmark.sh
    CHANGED
    
    
    
        data/jb.gemspec
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            # coding: utf-8
         | 
| 2 2 | 
             
            # frozen_string_literal: true
         | 
| 3 | 
            +
             | 
| 3 4 | 
             
            lib = File.expand_path('../lib', __FILE__)
         | 
| 4 5 | 
             
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 5 6 | 
             
            require 'jb/version'
         | 
| @@ -20,11 +21,11 @@ Gem::Specification.new do |spec| | |
| 20 21 | 
             
              spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         | 
| 21 22 | 
             
              spec.require_paths = ["lib"]
         | 
| 22 23 |  | 
| 23 | 
            -
              spec. | 
| 24 | 
            -
             | 
| 25 | 
            -
              spec.add_development_dependency "bundler", "~> 1.12"
         | 
| 24 | 
            +
              spec.add_development_dependency 'bundler'
         | 
| 26 25 | 
             
              spec.add_development_dependency 'rake'
         | 
| 27 26 | 
             
              spec.add_development_dependency 'test-unit-rails'
         | 
| 28 27 | 
             
              spec.add_development_dependency 'rails'
         | 
| 29 | 
            -
              spec.add_development_dependency ' | 
| 28 | 
            +
              spec.add_development_dependency 'action_args'
         | 
| 29 | 
            +
              spec.add_development_dependency 'debug'
         | 
| 30 | 
            +
              spec.add_development_dependency 'selenium-webdriver'
         | 
| 30 31 | 
             
            end
         | 
| @@ -1,11 +1,12 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 2 3 | 
             
            require 'rails/generators'
         | 
| 3 4 | 
             
            require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
         | 
| 4 5 |  | 
| 5 6 | 
             
            module Rails
         | 
| 6 7 | 
             
              module Generators
         | 
| 7 8 | 
             
                class ScaffoldControllerGenerator
         | 
| 8 | 
            -
                  hook_for :jb, default: true
         | 
| 9 | 
            +
                  hook_for :jb, type: :boolean, default: true
         | 
| 9 10 | 
             
                end
         | 
| 10 11 | 
             
              end
         | 
| 11 12 | 
             
            end
         | 
| @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
            # Monkey-patches for Action View 4 and 5
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module Jb
         | 
| 5 | 
            +
              module PartialRenderer
         | 
| 6 | 
            +
                module JbTemplateDetector
         | 
| 7 | 
            +
                  # A monkey-patch to inject StrongArray module to Jb partial renderer
         | 
| 8 | 
            +
                  private def find_partial(*)
         | 
| 9 | 
            +
                    template = super
         | 
| 10 | 
            +
                    extend RenderCollectionExtension if template && (template.handler == Jb::Handler)
         | 
| 11 | 
            +
                    template
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                # A horrible monkey-patch to prevent rendered collection from being converted to String
         | 
| 16 | 
            +
                module StrongArray
         | 
| 17 | 
            +
                  def join(*)
         | 
| 18 | 
            +
                    self
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def html_safe
         | 
| 22 | 
            +
                    self
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                # A monkey-patch strengthening rendered collection
         | 
| 27 | 
            +
                module RenderCollectionExtension
         | 
| 28 | 
            +
                  private def collection_with_template
         | 
| 29 | 
            +
                    super.extend StrongArray
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  private def collection_without_template
         | 
| 33 | 
            +
                    super.extend StrongArray
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  private def render_collection
         | 
| 37 | 
            +
                    return [] if @collection.blank?
         | 
| 38 | 
            +
                    super
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              # A monkey-patch that converts non-partial result to a JSON String
         | 
| 44 | 
            +
              module TemplateRenderer
         | 
| 45 | 
            +
                module JSONizer
         | 
| 46 | 
            +
                  def render_template(template, *)
         | 
| 47 | 
            +
                    template.respond_to?(:handler) && (template.handler == Jb::Handler) ? super.to_json : super
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
            end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            ::ActionView::PartialRenderer.prepend ::Jb::PartialRenderer::JbTemplateDetector
         | 
| 54 | 
            +
            ::ActionView::TemplateRenderer.prepend ::Jb::TemplateRenderer::JSONizer
         | 
| @@ -1,54 +1,70 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 | 
            -
             | 
| 2 | 
            +
            # Monkey-patches for Action View 6+
         | 
| 3 3 |  | 
| 4 4 | 
             
            module Jb
         | 
| 5 | 
            -
               | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
                   | 
| 9 | 
            -
                     | 
| 10 | 
            -
                     | 
| 11 | 
            -
                     | 
| 5 | 
            +
              # A monkey-patch that converts non-partial result to a JSON String
         | 
| 6 | 
            +
              module TemplateRenderer
         | 
| 7 | 
            +
                module JSONizer
         | 
| 8 | 
            +
                  def render_template(_view, template, *)
         | 
| 9 | 
            +
                    rendered_template = super
         | 
| 10 | 
            +
                    rendered_template.instance_variable_set :@body, rendered_template.body.to_json if template.respond_to?(:handler) && (template.handler == Jb::Handler)
         | 
| 11 | 
            +
                    rendered_template
         | 
| 12 12 | 
             
                  end
         | 
| 13 13 | 
             
                end
         | 
| 14 | 
            +
              end
         | 
| 14 15 |  | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
                  end
         | 
| 20 | 
            -
             | 
| 21 | 
            -
                  def html_safe
         | 
| 22 | 
            -
                    self
         | 
| 23 | 
            -
                  end
         | 
| 16 | 
            +
              # A wrapper class for template result that makes `to_s` method do nothing
         | 
| 17 | 
            +
              class TemplateResult < SimpleDelegator
         | 
| 18 | 
            +
                def to_s
         | 
| 19 | 
            +
                  __getobj__
         | 
| 24 20 | 
             
                end
         | 
| 21 | 
            +
              end
         | 
| 25 22 |  | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
                   | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
                  end
         | 
| 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
         | 
| 35 31 |  | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 32 | 
            +
              # Rails 6.1+: A monkey-patch for jb template collection result's `body` not to return a String but an Array
         | 
| 33 | 
            +
              module CollectionRendererExtension
         | 
| 34 | 
            +
                private def render_collection(_collection, _view, _path, template, _layout, _block)
         | 
| 35 | 
            +
                  obj = super
         | 
| 36 | 
            +
                  if template.respond_to?(:handler) && (template.handler == Jb::Handler)
         | 
| 37 | 
            +
                    if ActionView::AbstractRenderer::RenderedCollection::EmptyCollection === obj
         | 
| 38 | 
            +
                      def obj.body; []; end
         | 
| 39 | 
            +
                    else
         | 
| 40 | 
            +
                      def obj.body; @rendered_templates.map(&:body); end
         | 
| 41 | 
            +
                    end
         | 
| 39 42 | 
             
                  end
         | 
| 43 | 
            +
                  obj
         | 
| 40 44 | 
             
                end
         | 
| 41 45 | 
             
              end
         | 
| 42 46 |  | 
| 43 | 
            -
              # A monkey-patch  | 
| 44 | 
            -
              module  | 
| 45 | 
            -
                 | 
| 46 | 
            -
                   | 
| 47 | 
            -
             | 
| 47 | 
            +
              # Rails 6.0: A monkey-patch for jb template collection result's `body` not to return a String but an Array
         | 
| 48 | 
            +
              module PartialRendererExtension
         | 
| 49 | 
            +
                private def render_collection(_view, template)
         | 
| 50 | 
            +
                  obj = super
         | 
| 51 | 
            +
                  if template.respond_to?(:handler) && (template.handler == Jb::Handler)
         | 
| 52 | 
            +
                    if ActionView::AbstractRenderer::RenderedCollection::EmptyCollection === obj
         | 
| 53 | 
            +
                      def obj.body; []; end
         | 
| 54 | 
            +
                    else
         | 
| 55 | 
            +
                      def obj.body; @rendered_templates.map(&:body); end
         | 
| 56 | 
            +
                    end
         | 
| 48 57 | 
             
                  end
         | 
| 58 | 
            +
                  obj
         | 
| 49 59 | 
             
                end
         | 
| 50 60 | 
             
              end
         | 
| 51 61 | 
             
            end
         | 
| 52 62 |  | 
| 53 | 
            -
            ::ActionView::PartialRenderer.prepend ::Jb::PartialRenderer::JbTemplateDetector
         | 
| 54 63 | 
             
            ::ActionView::TemplateRenderer.prepend ::Jb::TemplateRenderer::JSONizer
         | 
| 64 | 
            +
            ::ActionView::Base.prepend ::Jb::TemlateResultCaster if (ActionView::VERSION::MAJOR >= 7) && (ActionView::VERSION::MINOR >= 1)
         | 
| 65 | 
            +
            begin
         | 
| 66 | 
            +
              # ActionView::CollectionRenderer is a newly added class since 6.1
         | 
| 67 | 
            +
              ::ActionView::CollectionRenderer.prepend ::Jb::CollectionRendererExtension
         | 
| 68 | 
            +
            rescue NameError
         | 
| 69 | 
            +
              ::ActionView::PartialRenderer.prepend ::Jb::PartialRendererExtension
         | 
| 70 | 
            +
            end
         | 
    
        data/lib/jb/handler.rb
    CHANGED
    
    | @@ -1,11 +1,12 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 2 3 | 
             
            module Jb
         | 
| 3 4 | 
             
              class Handler
         | 
| 4 5 | 
             
                class_attribute :default_format
         | 
| 5 6 | 
             
                self.default_format = :json
         | 
| 6 7 |  | 
| 7 | 
            -
                def self.call(template)
         | 
| 8 | 
            -
                  template.source
         | 
| 8 | 
            +
                def self.call(template, source=nil)
         | 
| 9 | 
            +
                  source || template.source
         | 
| 9 10 | 
             
                end
         | 
| 10 11 |  | 
| 11 12 | 
             
                def self.handles_encoding?
         | 
    
        data/lib/jb/railtie.rb
    CHANGED
    
    | @@ -1,14 +1,34 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 2 3 | 
             
            module Jb
         | 
| 3 4 | 
             
              class Railtie < ::Rails::Railtie
         | 
| 4 5 | 
             
                initializer 'jb' do
         | 
| 5 6 | 
             
                  ActiveSupport.on_load :action_view do
         | 
| 6 | 
            -
                     | 
| 7 | 
            +
                    if Rails::VERSION::MAJOR >= 6
         | 
| 8 | 
            +
                      require 'jb/action_view_monkeys'
         | 
| 9 | 
            +
                    else
         | 
| 10 | 
            +
                      require 'jb/action_view_legacy_monkeys'
         | 
| 11 | 
            +
                    end
         | 
| 7 12 | 
             
                    require 'jb/handler'
         | 
| 8 13 | 
             
                    ::ActionView::Template.register_template_handler :jb, Jb::Handler
         | 
| 9 14 | 
             
                  end
         | 
| 10 15 | 
             
                end
         | 
| 11 16 |  | 
| 17 | 
            +
                if Rails::VERSION::MAJOR >= 5
         | 
| 18 | 
            +
                  module ::ActionController
         | 
| 19 | 
            +
                    module ApiRendering
         | 
| 20 | 
            +
                      include ActionView::Rendering
         | 
| 21 | 
            +
                    end
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                  ActiveSupport.on_load :action_controller do
         | 
| 25 | 
            +
                    if self == ActionController::API
         | 
| 26 | 
            +
                      include ActionController::Helpers
         | 
| 27 | 
            +
                      include ActionController::ImplicitRender
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 12 32 | 
             
                generators do |app|
         | 
| 13 33 | 
             
                  Rails::Generators.configure! app.config.generators
         | 
| 14 34 | 
             
                  Rails::Generators.hidden_namespaces.uniq!
         | 
    
        data/lib/jb/version.rb
    CHANGED
    
    
    
        data/lib/jb.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.2
         | 
| 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-11-04 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,21 +25,21 @@ 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 | 
            +
                - - ">="
         | 
| 32 32 | 
             
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            -
                    version: ' | 
| 33 | 
            +
                    version: '0'
         | 
| 34 34 | 
             
              type: :development
         | 
| 35 35 | 
             
              prerelease: false
         | 
| 36 36 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 37 | 
             
                requirements:
         | 
| 38 | 
            -
                - - " | 
| 38 | 
            +
                - - ">="
         | 
| 39 39 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            -
                    version: ' | 
| 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,21 @@ dependencies: | |
| 67 67 | 
             
                  - !ruby/object:Gem::Version
         | 
| 68 68 | 
             
                    version: '0'
         | 
| 69 69 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            -
              name:  | 
| 70 | 
            +
              name: action_args
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: debug
         | 
| 71 85 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 86 | 
             
                requirements:
         | 
| 73 87 | 
             
                - - ">="
         | 
| @@ -81,7 +95,7 @@ dependencies: | |
| 81 95 | 
             
                  - !ruby/object:Gem::Version
         | 
| 82 96 | 
             
                    version: '0'
         | 
| 83 97 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            -
              name:  | 
| 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
         | 
| @@ -110,12 +124,14 @@ files: | |
| 110 124 | 
             
            - bin/benchmark.sh
         | 
| 111 125 | 
             
            - bin/console
         | 
| 112 126 | 
             
            - bin/setup
         | 
| 127 | 
            +
            - gemfiles/benchmark.gemfile
         | 
| 113 128 | 
             
            - jb.gemspec
         | 
| 114 129 | 
             
            - lib/generators/rails/jb_generator.rb
         | 
| 115 130 | 
             
            - lib/generators/rails/scaffold_controller_generator.rb
         | 
| 116 131 | 
             
            - lib/generators/rails/templates/index.json.jb
         | 
| 117 132 | 
             
            - lib/generators/rails/templates/show.json.jb
         | 
| 118 133 | 
             
            - lib/jb.rb
         | 
| 134 | 
            +
            - lib/jb/action_view_legacy_monkeys.rb
         | 
| 119 135 | 
             
            - lib/jb/action_view_monkeys.rb
         | 
| 120 136 | 
             
            - lib/jb/handler.rb
         | 
| 121 137 | 
             
            - lib/jb/railtie.rb
         | 
| @@ -124,7 +140,7 @@ homepage: https://github.com/amatsuda/jb | |
| 124 140 | 
             
            licenses:
         | 
| 125 141 | 
             
            - MIT
         | 
| 126 142 | 
             
            metadata: {}
         | 
| 127 | 
            -
            post_install_message: | 
| 143 | 
            +
            post_install_message:
         | 
| 128 144 | 
             
            rdoc_options: []
         | 
| 129 145 | 
             
            require_paths:
         | 
| 130 146 | 
             
            - lib
         | 
| @@ -139,9 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 139 155 | 
             
                - !ruby/object:Gem::Version
         | 
| 140 156 | 
             
                  version: '0'
         | 
| 141 157 | 
             
            requirements: []
         | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
            signing_key: 
         | 
| 158 | 
            +
            rubygems_version: 3.5.0.dev
         | 
| 159 | 
            +
            signing_key:
         | 
| 145 160 | 
             
            specification_version: 4
         | 
| 146 161 | 
             
            summary: Faster and simpler Jbuilder alternative
         | 
| 147 162 | 
             
            test_files: []
         |