actionview-component 1.9.0 → 1.10.0
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/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -44
- data/lib/action_view/component/render_monkey_patch.rb +11 -6
- data/lib/action_view/component/version.rb +1 -1
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: c20b9ae371123eb58c62aa3c093baafdf7d5745bf8311ff2fb975c70282e6dfd
         | 
| 4 | 
            +
              data.tar.gz: caaad9f6e645602efe2faea055ff08dd1f548efe08b503a3048d83eebc824997
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 52aec02cba11b482df4c4e4f160ad7602dadca664eb75a4fad579fb5802c9f4efb6a7e07f1082ab634e99c9ed8efad82861daf1fdc9c5108cdf63e3f2b0f80d1
         | 
| 7 | 
            +
              data.tar.gz: 4d5fb555f12740dfa62a64b4c9b095879e4ea182a3930f9a725482525c6d5bbeae08d9914dff847e8521a7107f31b13b919f5b9d5cc69d83cbaf53c08422503c
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -144,7 +144,7 @@ end | |
| 144 144 | 
             
            We can render it in a view as:
         | 
| 145 145 |  | 
| 146 146 | 
             
            ```erb
         | 
| 147 | 
            -
            <%= render(TestComponent | 
| 147 | 
            +
            <%= render(TestComponent.new(title: "my title")) do %>
         | 
| 148 148 | 
             
              Hello, World!
         | 
| 149 149 | 
             
            <% end %>
         | 
| 150 150 | 
             
            ```
         | 
| @@ -155,42 +155,12 @@ Which returns: | |
| 155 155 | 
             
            <span title="my title">Hello, World!</span>
         | 
| 156 156 | 
             
            ```
         | 
| 157 157 |  | 
| 158 | 
            -
            ##### Supported `render` syntaxes
         | 
| 159 | 
            -
             | 
| 160 | 
            -
            Components can be rendered via:
         | 
| 161 | 
            -
             | 
| 162 | 
            -
            `render(TestComponent, foo: :bar)`
         | 
| 163 | 
            -
             | 
| 164 | 
            -
            `render(component: TestComponent, locals: { foo: :bar })`
         | 
| 165 | 
            -
             | 
| 166 | 
            -
            **Rendering components through models**
         | 
| 167 | 
            -
             | 
| 168 | 
            -
            Passing model instances will cause `render` to look for its respective component class.
         | 
| 169 | 
            -
             | 
| 170 | 
            -
            The component is instantiated with the rendered model instance.
         | 
| 171 | 
            -
             | 
| 172 | 
            -
            Example for a `Post` model:
         | 
| 173 | 
            -
             | 
| 174 | 
            -
            `render(@post)`
         | 
| 175 | 
            -
             | 
| 176 | 
            -
            ```ruby
         | 
| 177 | 
            -
            class PostComponent < ActionView::Component::Base
         | 
| 178 | 
            -
              def initialize(post)
         | 
| 179 | 
            -
                @post = post
         | 
| 180 | 
            -
              end
         | 
| 181 | 
            -
            end
         | 
| 182 | 
            -
            ```
         | 
| 183 | 
            -
             | 
| 184 | 
            -
            The following syntax has been deprecated and will be removed in v2.0.0:
         | 
| 185 | 
            -
             | 
| 186 | 
            -
            `render(TestComponent.new(foo: :bar))`
         | 
| 187 | 
            -
             | 
| 188 158 | 
             
            #### Error case
         | 
| 189 159 |  | 
| 190 160 | 
             
            If the component is rendered with a blank title:
         | 
| 191 161 |  | 
| 192 162 | 
             
            ```erb
         | 
| 193 | 
            -
            <%= render(TestComponent | 
| 163 | 
            +
            <%= render(TestComponent.new(title: "")) do %>
         | 
| 194 164 | 
             
              Hello, World!
         | 
| 195 165 | 
             
            <% end %>
         | 
| 196 166 | 
             
            ```
         | 
| @@ -227,7 +197,7 @@ end | |
| 227 197 | 
             
            We can render it in a view as:
         | 
| 228 198 |  | 
| 229 199 | 
             
            ```erb
         | 
| 230 | 
            -
            <%= render(ModalComponent | 
| 200 | 
            +
            <%= render(ModalComponent.new(user: {name: 'Jane'})) do |component| %>
         | 
| 231 201 | 
             
              <% component.with(:header) do %>
         | 
| 232 202 | 
             
                  Hello <%= user[:name] %>
         | 
| 233 203 | 
             
                <% end %>
         | 
| @@ -265,7 +235,7 @@ end | |
| 265 235 | 
             
            ```
         | 
| 266 236 |  | 
| 267 237 | 
             
            ```erb
         | 
| 268 | 
            -
            <%= render(ModalComponent | 
| 238 | 
            +
            <%= render(ModalComponent.new(header: "Hi!")) do |component| %>
         | 
| 269 239 | 
             
              <% help_enabled? && component.with(:header) do %>
         | 
| 270 240 | 
             
                <span class="help_icon"><%= component.header %></span>
         | 
| 271 241 | 
             
              <% end %>
         | 
| @@ -292,7 +262,7 @@ end | |
| 292 262 |  | 
| 293 263 | 
             
            `app/views/render_arg.html.erb`:
         | 
| 294 264 | 
             
            ```erb
         | 
| 295 | 
            -
            <%= render(ModalComponent | 
| 265 | 
            +
            <%= render(ModalComponent.new(header: "Hi!")) do |component| %>
         | 
| 296 266 | 
             
              <% component.with(:body) do %>
         | 
| 297 267 | 
             
                <p>Have a great day.</p>
         | 
| 298 268 | 
             
              <% end %>
         | 
| @@ -338,7 +308,7 @@ end | |
| 338 308 |  | 
| 339 309 | 
             
            `app/views/render_arg.html.erb`:
         | 
| 340 310 | 
             
            ```erb
         | 
| 341 | 
            -
            <%= render(ModalComponent | 
| 311 | 
            +
            <%= render(ModalComponent.new(header: "Hi!")) do |component| %>
         | 
| 342 312 | 
             
              <% component.with(:body) do %>
         | 
| 343 313 | 
             
                <p>Have a great day.</p>
         | 
| 344 314 | 
             
              <% end %>
         | 
| @@ -347,7 +317,7 @@ end | |
| 347 317 |  | 
| 348 318 | 
             
            `app/views/with_block.html.erb`:
         | 
| 349 319 | 
             
            ```erb
         | 
| 350 | 
            -
            <%= render(ModalComponent) do |component| %>
         | 
| 320 | 
            +
            <%= render(ModalComponent.new) do |component| %>
         | 
| 351 321 | 
             
              <% component.with(:header) do %>
         | 
| 352 322 | 
             
                <span class="help_icon">Hello</span>
         | 
| 353 323 | 
             
              <% end %>
         | 
| @@ -359,7 +329,7 @@ end | |
| 359 329 |  | 
| 360 330 | 
             
            `app/views/no_header.html.erb`:
         | 
| 361 331 | 
             
            ```erb
         | 
| 362 | 
            -
            <%= render(ModalComponent) do |component| %>
         | 
| 332 | 
            +
            <%= render(ModalComponent.new) do |component| %>
         | 
| 363 333 | 
             
              <% component.with(:body) do %>
         | 
| 364 334 | 
             
                <p>Have a great day.</p>
         | 
| 365 335 | 
             
              <% end %>
         | 
| @@ -386,7 +356,7 @@ or the view that renders the component: | |
| 386 356 | 
             
            ```erb
         | 
| 387 357 | 
             
            <!-- app/views/_banners.html.erb -->
         | 
| 388 358 | 
             
            <% if current_user.requires_confirmation? %>
         | 
| 389 | 
            -
              <%= render(ConfirmEmailComponent | 
| 359 | 
            +
              <%= render(ConfirmEmailComponent.new(user: current_user)) %>
         | 
| 390 360 | 
             
            <% end %>
         | 
| 391 361 | 
             
            ```
         | 
| 392 362 |  | 
| @@ -414,7 +384,7 @@ end | |
| 414 384 |  | 
| 415 385 | 
             
            ```erb
         | 
| 416 386 | 
             
            <!-- app/views/_banners.html.erb -->
         | 
| 417 | 
            -
            <%= render(ConfirmEmailComponent | 
| 387 | 
            +
            <%= render(ConfirmEmailComponent.new(user: current_user)) %>
         | 
| 418 388 | 
             
            ```
         | 
| 419 389 |  | 
| 420 390 | 
             
            ### Testing
         | 
| @@ -428,7 +398,7 @@ class MyComponentTest < ActionView::Component::TestCase | |
| 428 398 | 
             
              test "render component" do
         | 
| 429 399 | 
             
                assert_equal(
         | 
| 430 400 | 
             
                  %(<span title="my title">Hello, World!</span>),
         | 
| 431 | 
            -
                  render_inline(TestComponent | 
| 401 | 
            +
                  render_inline(TestComponent.new(title: "my title")) { "Hello, World!" }.to_html
         | 
| 432 402 | 
             
                )
         | 
| 433 403 | 
             
              end
         | 
| 434 404 | 
             
            end
         | 
| @@ -445,7 +415,7 @@ test "render component for tablet" do | |
| 445 415 | 
             
              with_variant :tablet do
         | 
| 446 416 | 
             
                assert_equal(
         | 
| 447 417 | 
             
                  %(<span title="my title">Hello, tablets!</span>),
         | 
| 448 | 
            -
                  render_inline(TestComponent | 
| 418 | 
            +
                  render_inline(TestComponent.new(title: "my title")) { "Hello, tablets!" }.css("span").to_html
         | 
| 449 419 | 
             
                )
         | 
| 450 420 | 
             
              end
         | 
| 451 421 | 
             
            end
         | 
| @@ -462,11 +432,11 @@ You can define as many examples as you want: | |
| 462 432 |  | 
| 463 433 | 
             
            class TestComponentPreview < ActionView::Component::Preview
         | 
| 464 434 | 
             
              def with_default_title
         | 
| 465 | 
            -
                render(TestComponent | 
| 435 | 
            +
                render(TestComponent.new(title: "Test component default"))
         | 
| 466 436 | 
             
              end
         | 
| 467 437 |  | 
| 468 438 | 
             
              def with_long_title
         | 
| 469 | 
            -
                render(TestComponent | 
| 439 | 
            +
                render(TestComponent.new(title: "This is a really long title to see how the component renders this"))
         | 
| 470 440 | 
             
              end
         | 
| 471 441 | 
             
            end
         | 
| 472 442 | 
             
            ```
         | 
| @@ -1,24 +1,29 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 3 | 
             
            # Monkey patch ActionView::Base#render to support ActionView::Component
         | 
| 4 | 
            -
            #
         | 
| 5 | 
            -
            # A version of this monkey patch was upstreamed in https://github.com/rails/rails/pull/36388
         | 
| 6 | 
            -
            # We'll need to upstream an updated version of this eventually.
         | 
| 7 4 | 
             
            module ActionView
         | 
| 8 5 | 
             
              module Component
         | 
| 9 6 | 
             
                module RenderMonkeyPatch # :nodoc:
         | 
| 10 7 | 
             
                  def render(options = {}, args = {}, &block)
         | 
| 11 8 | 
             
                    if options.respond_to?(:render_in)
         | 
| 9 | 
            +
                      options.render_in(self, &block)
         | 
| 10 | 
            +
                    elsif options.is_a?(Class) && options < ActionView::Component::Base
         | 
| 12 11 | 
             
                      ActiveSupport::Deprecation.warn(
         | 
| 13 | 
            -
                        " | 
| 12 | 
            +
                        "`render MyComponent, foo: :bar` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
         | 
| 14 13 | 
             
                      )
         | 
| 15 14 |  | 
| 16 | 
            -
                      options.render_in(self, &block)
         | 
| 17 | 
            -
                    elsif options.is_a?(Class) && options < ActionView::Component::Base
         | 
| 18 15 | 
             
                      options.new(args).render_in(self, &block)
         | 
| 19 16 | 
             
                    elsif options.is_a?(Hash) && options.has_key?(:component)
         | 
| 17 | 
            +
                      ActiveSupport::Deprecation.warn(
         | 
| 18 | 
            +
                        "`render component: MyComponent, locals: { foo: :bar }` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
         | 
| 19 | 
            +
                      )
         | 
| 20 | 
            +
             | 
| 20 21 | 
             
                      options[:component].new(options[:locals]).render_in(self, &block)
         | 
| 21 22 | 
             
                    elsif options.respond_to?(:to_component_class) && !options.to_component_class.nil?
         | 
| 23 | 
            +
                      ActiveSupport::Deprecation.warn(
         | 
| 24 | 
            +
                        "rendering objects that respond_to `to_component_class` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
         | 
| 25 | 
            +
                      )
         | 
| 26 | 
            +
             | 
| 22 27 | 
             
                      options.to_component_class.new(options).render_in(self, &block)
         | 
| 23 28 | 
             
                    else
         | 
| 24 29 | 
             
                      super
         |