Class: CafeCar::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- CafeCar::FormBuilder
- Includes:
- Resolver
- Defined in:
- lib/cafe_car/form_builder.rb
Instance Method Summary collapse
- #association(method, collection: nil, **options) ⇒ Object
- #error(method, text = error_text(method)) ⇒ Object
- #error_text(method) ⇒ Object
- #errors(method) ⇒ Object
- #field(method) ⇒ Object
- #fields_for(method, object = object_for(method)) ⇒ Object
- #hidden(*methods) ⇒ Object
- #hint(method, text = info(method).hint) ⇒ Object
- #info(method) ⇒ Object
-
#initialize ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #input(method, *args, as: nil, **options) ⇒ Object
- #label(method, text = info(method).label, required: info(method).required?) ⇒ Object
- #model ⇒ Object
- #object_for(method) ⇒ Object
- #policy ⇒ Object
- #remaining_attributes ⇒ Object
- #remaining_fields(&block) ⇒ Object
- #show ⇒ Object
- #submit(value = nil, **options) ⇒ Object
Methods included from Resolver
Constructor Details
#initialize ⇒ FormBuilder
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, **) 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 [:include_blank] ||= info.prompt input(info.input_key, collection, :id, -> { @template.present(_1).title }, as: :collection_select, **) 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, **) info = info(method) as ||= info.input [:placeholder] = info.placeholder unless .key?(:placeholder) [:autocomplete] = info.autocomplete unless .key?(:autocomplete) public_send(as, method, *args, **) 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 |
#model ⇒ Object
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 |
#policy ⇒ Object
13 |
# File 'lib/cafe_car/form_builder.rb', line 13 def policy = @template.policy(@object) |
#remaining_attributes ⇒ Object
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 |
#show ⇒ Object
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, **) [:class] ||= ui.class(:button, :primary) super(value, ) end |