Class: FormComponent

Inherits:
Object
  • Object
show all
Defined in:
app/components/form_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ FormComponent

Returns a new instance of FormComponent.



4
5
6
# File 'app/components/form_component.rb', line 4

def initialize(**kwargs)
  @form_options = kwargs
end

Instance Method Details

#render_in(context, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/form_component.rb', line 8

def render_in(context, &block)
  @view_context = context

  if @form_options[:url] || @form_options[:model]
    @_block = block
    if Rails.env.development?
      "<!-- BEGIN #{self.class.name} -->#{self}<!-- END #{self.class.name} -->".html_safe
    else
      to_s
    end
  else
    @content = context.capture(self, &block) if block
    @content = ERB::Util.html_escape(@content) unless @content.nil? || @content.html_safe?
    if Rails.env.development?
      "<!-- BEGIN #{self.class.name} -->#{self}<!-- END #{self.class.name} -->".html_safe
    else
      to_s
    end
  end
end

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/components/form_component.rb', line 29

def to_s
  if @form_options[:url] || @form_options[:model]
    opts = { class: "ui form", data: { controller: "fui-form" } }
    opts.merge!(@form_options)

    @view_context.form_with(**opts) { |f|
      previous = @view_context.instance_variable_get(:@_form_builder)
      @view_context.instance_variable_set(:@_form_builder, f)
      begin
        @_block ? @view_context.instance_exec(&@_block) : "".html_safe
      ensure
        @view_context.instance_variable_set(:@_form_builder, previous)
      end
    }
  else
    opts = { class: "ui form", data: { controller: "fui-form" } }
    opts.merge!(@form_options)

    tag.form(**opts) { @content }
  end
end