Class: CafeCar::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Resolver
Defined in:
lib/cafe_car/form_builder.rb

Instance Method Summary collapse

Methods included from Resolver

#const, #const!

Constructor Details

#initializeFormBuilder

Returns a new instance of FormBuilder.



7
8
9
10
# File 'lib/cafe_car/form_builder.rb', line 7

def initialize(...)
  super
  @fields = {}
end

Instance Method Details

#association(method, collection: nil, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cafe_car/form_builder.rb', line 16

def association(method, collection: nil, **options)
  info = info(method)

  return show(info.input_key) if info.polymorphic? and object.persisted?
  return hidden(*info.polymorphic_methods) if info.polymorphic?

  collection              ||= info.collection
  # options[:prompt]      ||= info.prompt
  options[:include_blank] ||= info.prompt

  input(info.input_key, collection, :id, -> { @template.present(_1).title }, as: :collection_select, **options)
end

#error(method, text = error_text(method)) ⇒ Object



73
74
75
# File 'lib/cafe_car/form_builder.rb', line 73

def error(method, text = error_text(method), **)
  @template.tag.span(text, **) if text.present?
end

#error_text(method) ⇒ Object



69
70
71
# File 'lib/cafe_car/form_builder.rb', line 69

def error_text(method)
  errors(method).to_sentence.presence
end

#errors(method) ⇒ Object



63
64
65
66
67
# File 'lib/cafe_car/form_builder.rb', line 63

def errors(method)
  errors     = object.try(:errors)
  associated = info(method).reflection&.then { errors[_1.name] } || []
  errors[method] | associated
end

#field(method) ⇒ Object



34
35
36
# File 'lib/cafe_car/form_builder.rb', line 34

def field(method, **, &)
  @fields[method] ||= const(:FieldBuilder).new(method:, form: self, template: @template, **, &)
end

#fields_for(method, object = object_for(method)) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/cafe_car/form_builder.rb', line 77

def fields_for(method, object = object_for(method), **, &)
  method = method.to_s.chomp("_attributes").to_sym
  if block_given?
    super
  else
    super(method, **) do |f|
      f.remaining_fields
    end
  end
end

#hidden(*methods) ⇒ Object



29
30
31
32
# File 'lib/cafe_car/form_builder.rb', line 29

def hidden(*methods, **, &)
  methods.map  { input(_1, as: :hidden_field, **, &) }
         .then { @template.safe_join(_1) }
end

#hint(method, text = info(method).hint) ⇒ Object



59
60
61
# File 'lib/cafe_car/form_builder.rb', line 59

def hint(method, text = info(method).hint, **)
  @template.tag.small(text, **) if text.present?
end

#info(method) ⇒ Object



47
# File 'lib/cafe_car/form_builder.rb', line 47

def info(method) = model.info.field(method)

#input(method, *args, as: nil, **options) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/cafe_car/form_builder.rb', line 49

def input(method, *args, as: nil, **options)
  info = info(method)
  as ||= info.input

  options[:placeholder]  = info.placeholder  unless options.key?(:placeholder)
  options[:autocomplete] = info.autocomplete unless options.key?(:autocomplete)

  public_send(as, method, *args, **options)
end

#label(method, text = info(method).label, required: info(method).required?) ⇒ Object



38
39
40
# File 'lib/cafe_car/form_builder.rb', line 38

def label(method, text = info(method).label, required: info(method).required?, **, &)
  super(method, @template.safe_join([ text, required ? "*" : "" ]), required:, **, &)
end

#modelObject



12
# File 'lib/cafe_car/form_builder.rb', line 12

def model     = @object.is_a?(Class) ? @object : @object.class

#object_for(method) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/cafe_car/form_builder.rb', line 88

def object_for(method)
  method = method.to_s.chomp("_attributes").to_sym
  if info(method).reflection.collection?
    object.try(method)
  else
    object.try(method) || object.try("build_#{method}")
  end
end

#policyObject



13
# File 'lib/cafe_car/form_builder.rb', line 13

def policy    = @template.policy(@object)

#remaining_attributesObject



97
# File 'lib/cafe_car/form_builder.rb', line 97

def remaining_attributes = policy.editable_attributes - @fields.keys

#remaining_fields(&block) ⇒ Object



99
100
101
102
103
# File 'lib/cafe_car/form_builder.rb', line 99

def remaining_fields(**, &block)
  block  ||= proc { field(_1, **) }
  fields   = remaining_attributes.map(&block)
  @template.safe_join(fields)
end

#showObject



14
# File 'lib/cafe_car/form_builder.rb', line 14

def show(...) = ui.Input { @template.present(@object).show(...) }

#submit(value = nil, **options) ⇒ Object



42
43
44
45
# File 'lib/cafe_car/form_builder.rb', line 42

def submit(value = nil, **options)
  options[:class] ||= ui.class(:button, :primary)
  super(value, options)
end