File: content_areas.rb

package info (click to toggle)
ruby-view-component 2.74.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 3,156 kB
  • sloc: ruby: 6,731; sh: 163; javascript: 10; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require "active_support/concern"

require "view_component/slot"

# DEPRECATED - ContentAreas is deprecated and will be removed in v3.0.0
module ViewComponent
  module ContentAreas
    extend ActiveSupport::Concern

    # Assign the provided content to the content area accessor
    #
    # @private
    def with(area, content = nil, &block)
      unless content_areas.include?(area)
        raise ArgumentError.new(
          "Unknown content_area '#{area}' for #{self} - expected one of '#{content_areas}'.\n\n" \
          "To fix this issue, add `with_content_area :#{area}` to #{self} or reference " \
          "a valid content area."
        )
      end

      if block
        content = view_context.capture(&block)
      end

      instance_variable_set("@#{area}".to_sym, content)
      nil
    end

    class_methods do
      def with_content_areas(*areas)
        ViewComponent::Deprecation.warn(
          "`with_content_areas` is deprecated and will be removed in ViewComponent v3.0.0.\n\n" \
          "Use slots (https://viewcomponent.org/guide/slots.html) instead."
        )

        if areas.include?(:content)
          raise ArgumentError.new(
            "#{self} defines a content area called :content, which is a reserved name. \n\n" \
            "To fix this issue, use another name, such as `:body`."
          )
        end

        areas.each do |area|
          define_method area.to_sym do
            content unless content_evaluated? # ensure content is loaded so content_areas will be defined
            instance_variable_get(:"@#{area}") if instance_variable_defined?(:"@#{area}")
          end
        end

        self.content_areas = areas
      end
    end
  end
end