Sha256: 058f4e51cdf4767d752432cd920960fa74bd597c392da1b7f4ef960bb0e25215

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Hanami
  module Extensions
    module View
      # Provides slice-specific configuration and behavior for any view part class defined within a
      # slice's module namespace.
      #
      # @api private
      # @since 2.1.0
      class SliceConfiguredPart < Module
        attr_reader :slice

        # @api private
        # @since 2.1.0
        def initialize(slice)
          super()
          @slice = slice
        end

        # @api private
        # @since 2.1.0
        def extended(klass)
          define_new
        end

        # @api private
        # @since 2.1.0
        def inspect
          "#<#{self.class.name}[#{slice.name}]>"
        end

        private

        # Defines a `.new` method on the part class that provides a default `rendering:` argument of
        # a rendering coming from a view configured for the slice. This means that any part can be
        # initialized standalone (with a `value:` only) and still have access to all the integrated
        # view facilities from the slice, such as helpers. This is helpful when unit testing parts.
        #
        # @example
        #   module MyApp::Views::Parts
        #     class Post < MyApp::View::Part
        #       def title_tag
        #         helpers.h1(value.title)
        #       end
        #     end
        #   end
        #
        #   # Useful when unit testing parts
        #   part = MyApp::Views::Parts::Post.new(value: hello_world_post)
        #   part.title_tag # => "<h1>Hello world</h1>"
        #
        # @api private
        # @since 2.1.0
        def define_new
          slice = self.slice

          define_method(:new) do |**args|
            return super(**args) if args.key?(:rendering)

            slice_rendering = Class.new(Hanami::View)
              .configure_for_slice(slice)
              .new
              .rendering

            super(rendering: slice_rendering, **args)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-2.1.0.rc1 lib/hanami/extensions/view/slice_configured_part.rb