Class: Primer::Forms::Base

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

Overview

:nodoc:

Class Attribute Summary collapse

Attributes included from ActsAsComponent

#template_root_path

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 '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 '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 '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)


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

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

.fields_with_caption_templatesObject



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

def fields_with_caption_templates
  @fields_with_caption_templates ||= []
end

.form(&block) ⇒ Object



15
16
17
# File '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
40
41
42
43
44
# File 'lib/primer/forms/base.rb', line 31

def inherited(base)
  form_path = const_source_location(base.name)
  return unless form_path

  base.template_root_path = File.join(File.dirname(form_path), base.name.demodulize.underscore)

  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 '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

Instance Method Details

#after_content?(*args) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/primer/forms/base.rb', line 114

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

#before_renderObject



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

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)


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

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

#each_input_in(root_input, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/primer/forms/base.rb', line 89

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/primer/forms/base.rb', line 74

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



122
123
124
125
126
127
# File 'lib/primer/forms/base.rb', line 122

def perform_render(&_block)
  Base.compile!
  self.class.compile!

  render_base_form
end

#render_caption_template(name) ⇒ Object



118
119
120
# File 'lib/primer/forms/base.rb', line 118

def render_caption_template(name)
  send(:"render_#{name}_caption")
end