Class: Shimmer::Form::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/shimmer/form/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.input_registryObject



7
8
9
# File 'lib/shimmer/form/builder.rb', line 7

def input_registry
  @input_registry ||= {}
end

.register(klass) ⇒ Object



11
12
13
# File 'lib/shimmer/form/builder.rb', line 11

def register(klass)
  input_registry[klass.type] = klass
end

Instance Method Details

#input(method, as: guess_type(method), wrapper_options: {}, description: nil, label_method: nil, id_method: :id, name_method: nil, label: nil, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shimmer/form/builder.rb', line 16

def input(method, as: guess_type(method), wrapper_options: {}, description: nil, label_method: nil, id_method: :id, name_method: nil, label: nil, **options)
  as ||= guess_type(method)
  options[:class] ||= "input__input"
  collection = options.delete :collection
  collection_based = !collection.nil? || as == :select
  collection ||= guess_collection(method) if collection_based
  name_method ||= guess_name_method(method) if collection_based
  id_method ||= :id if collection_based
  classes = []
  options[:required] ||= true if options[:required].nil? && required_attributes.include?(method)
  options[:data] ||= {}
  options[:data][:controller] = options.delete(:controller) if options[:controller]
  extra = []
  input_class = self.class.input_registry[as]
  raise "Unknown type #{as}" unless input_class
  input = input_class.new(builder: self, method: method, options: options, id_method: id_method, collection: collection, name_method: name_method)
  input.prepare
  wrapper_options.reverse_merge! input.wrapper_options
  label_method ||= wrapper_options.delete(:label_method)
  wrap method: method, content: input.render, classes: classes + ["input--#{as}"], label: label, extra: extra, description: description, options: wrapper_options, label_method: label_method
end