Class: Primer::Forms::Base

Inherits:
Object
  • Object
show all
Extended by:
ActsAsComponent
Defined in:
app/lib/primer/forms/base.rb

Overview

:nodoc:

Direct Known Subclasses

ApplicationForm, ToggleSwitchForm

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsComponent

compile!, extended, renders_templates

Class Attribute Details

.__vcf_builderObject (readonly)

Returns the value of attribute __vcf_builder.



12
13
14
# File 'app/lib/primer/forms/base.rb', line 12

def __vcf_builder
  @__vcf_builder
end

.__vcf_form_blockObject (readonly)

Returns the value of attribute __vcf_form_block.



12
13
14
# File 'app/lib/primer/forms/base.rb', line 12

def __vcf_form_block
  @__vcf_form_block
end

.has_after_contentObject (readonly) Also known as: after_content?

Returns the value of attribute has_after_content.



12
13
14
# File 'app/lib/primer/forms/base.rb', line 12

def has_after_content
  @has_after_content
end

Class Method Details

.caption_template?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/lib/primer/forms/base.rb', line 41

def caption_template?(field_name)
  fields_with_caption_templates.include?(sanitize_field_name_for_template_path(field_name))
end

.fields_with_caption_templatesObject



45
46
47
# File 'app/lib/primer/forms/base.rb', line 45

def fields_with_caption_templates
  @fields_with_caption_templates ||= []
end

.form(&block) ⇒ Object



15
16
17
# File 'app/lib/primer/forms/base.rb', line 15

def form(&block)
  @__vcf_form_block = block
end

.inherited(base) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/lib/primer/forms/base.rb', line 31

def inherited(base)
  base.renders_template "after_content.html.erb" do
    base.instance_variable_set(:@has_after_content, true)
  end

  base.renders_templates "*_caption.html.erb" do |path|
    base.fields_with_caption_templates << File.basename(path).chomp("_caption.html.erb").to_sym
  end
end

.new(builder, **options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/lib/primer/forms/base.rb', line 19

def new(builder, **options)
  if builder && !builder.is_a?(Primer::Forms::Builder)
    raise ArgumentError, "please pass an instance of Primer::Forms::Builder when "\
      "constructing a form object (consider using the `primer_form_with` helper)"
  end

  allocate.tap do |form|
    form.instance_variable_set(:@builder, builder)
    form.send(:initialize, **options)
  end
end

.sanitize_field_name_for_template_path(field_name) ⇒ Object



49
50
51
# File 'app/lib/primer/forms/base.rb', line 49

def sanitize_field_name_for_template_path(field_name)
  field_name.to_s.delete_suffix("?").to_sym
end

Instance Method Details

#after_content?(*args) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/lib/primer/forms/base.rb', line 94

def after_content?(*args)
  self.class.after_content?(*args)
end

#before_renderObject



81
82
83
84
85
86
87
88
# File 'app/lib/primer/forms/base.rb', line 81

def before_render
  each_input_in(self) do |input|
    if input.input? && input.invalid? && input.focusable?
      input.autofocus!
      break
    end
  end
end

#caption_template?(*args) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/lib/primer/forms/base.rb', line 90

def caption_template?(*args)
  self.class.caption_template?(*args)
end

#each_input_in(root_input, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'app/lib/primer/forms/base.rb', line 69

def each_input_in(root_input, &block)
  return enum_for(__method__, root_input) unless block

  root_input.inputs.each do |input|
    if input.respond_to?(:inputs)
      each_input_in(input, &block)
    else
      yield input
    end
  end
end

#inputsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/lib/primer/forms/base.rb', line 54

def inputs
  @inputs ||= form_object.inputs.map do |input|
    next input unless input.input?

    # wrap inputs in a group (unless they are already groups)
    if input.type == :group
      input
    else
      Primer::Forms::Dsl::InputGroup.new(builder: @builder, form: self) do |group|
        group.send(:add_input, input)
      end
    end
  end
end

#perform_render(&_block) ⇒ Object



102
103
104
105
106
107
108
109
# File 'app/lib/primer/forms/base.rb', line 102

def perform_render(&_block)
  return "" unless render?

  Base.compile!
  self.class.compile!

  render_base_form
end

#render?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/lib/primer/forms/base.rb', line 111

def render?
  true
end

#render_caption_template(field_name) ⇒ Object



98
99
100
# File 'app/lib/primer/forms/base.rb', line 98

def render_caption_template(field_name)
  send(:"render_#{self.class.sanitize_field_name_for_template_path(field_name)}_caption")
end