Class: Yattho::Forms::Base

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

Overview

:nodoc:

Direct Known Subclasses

ApplicationForm, ToggleSwitchForm

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/yattho/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/yattho/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/yattho/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/yattho/forms/base.rb', line 46

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

.fields_with_caption_templatesObject



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

def fields_with_caption_templates
  @fields_with_caption_templates ||= []
end

.form(&block) ⇒ Object



15
16
17
# File 'lib/yattho/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/yattho/forms/base.rb', line 31

def inherited(base)
  form_path = Utils.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/yattho/forms/base.rb', line 19

def new(builder, **options)
  if builder && !builder.is_a?(Yattho::Forms::Builder)
    raise ArgumentError, "please pass an instance of Yattho::Forms::Builder when " \
                         "constructing a form object (consider using the `yattho_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



54
55
56
# File 'lib/yattho/forms/base.rb', line 54

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)


99
100
101
# File 'lib/yattho/forms/base.rb', line 99

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

#before_renderObject



86
87
88
89
90
91
92
93
# File 'lib/yattho/forms/base.rb', line 86

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)


95
96
97
# File 'lib/yattho/forms/base.rb', line 95

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

#each_input_in(root_input, &block) ⇒ Object



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

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/yattho/forms/base.rb', line 59

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
      Yattho::Forms::Dsl::InputGroup.new(builder: @builder, form: self) do |group|
        group.send(:add_input, input)
      end
    end
  end
end

#perform_render(&_block) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/yattho/forms/base.rb', line 107

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

  Base.compile!
  self.class.compile!

  render_base_form
end

#render?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/yattho/forms/base.rb', line 116

def render?
  true
end

#render_caption_template(field_name) ⇒ Object



103
104
105
# File 'lib/yattho/forms/base.rb', line 103

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