Class: Bootstrap5RailsExtensions::CardHelper::CardBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap5_rails_extensions/card_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ CardBuilder

Returns a new instance of CardBuilder.



41
42
43
44
45
46
47
48
49
# File 'app/helpers/bootstrap5_rails_extensions/card_helper.rb', line 41

def initialize(view_context)
  @view = view_context
  @header_html = nil
  @header_options = {}
  @body_fragments = []
  @body_options = {}
  @footer_html = nil
  @footer_options = {}
end

Instance Method Details

#body(content = nil, **options, &block) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/bootstrap5_rails_extensions/card_helper.rb', line 60

def body(content = nil, **options, &block)
  html = extract_content(content, &block)
  raise ArgumentError, "カード本文が空です" if blank_html?(html)

  @body_fragments << html
  css_class = extract_css_class!(options)
  if css_class.present?
    if @body_options[:class].present? && @body_options[:class] != css_class
      raise ArgumentError, "カード本文のクラス指定は一度だけ行ってください"
    end
    @body_options[:class] = css_class
  end
  self
end

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
# File 'app/helpers/bootstrap5_rails_extensions/card_helper.rb', line 75

def footer(content = nil, **options, &block)
  raise ArgumentError, "カードフッターは一度だけ定義できます" if @footer_html

  html = extract_content(content, &block)
  @footer_html = html unless blank_html?(html)
  css_class = extract_css_class!(options)
  @footer_options[:class] = css_class if css_class.present?
  self
end

#header(content = nil, **options, &block) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
# File 'app/helpers/bootstrap5_rails_extensions/card_helper.rb', line 51

def header(content = nil, **options, &block)
  raise ArgumentError, "カードヘッダーは一度だけ定義できます" if @header_html

  @header_html = extract_content(content, &block)
  css_class = extract_css_class!(options)
  @header_options[:class] = css_class if css_class.present?
  self
end

#renderObject

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
# File 'app/helpers/bootstrap5_rails_extensions/card_helper.rb', line 85

def render
  raise ArgumentError, "カード本文を定義してください" if @body_fragments.empty?

  fragments = []
  fragments << wrap_section(@header_html, default_header_class, @header_options[:class]) if @header_html
  fragments << wrap_section(@view.safe_join(@body_fragments), default_body_class, @body_options[:class])
  fragments << wrap_section(@footer_html, default_footer_class, @footer_options[:class]) if @footer_html
  @view.safe_join(fragments.compact)
end