Skip to content

Instantly share code, notes, and snippets.

@kaspth
Created June 15, 2023 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaspth/56c4eb549b873083f17d710d3c7e8256 to your computer and use it in GitHub Desktop.
Save kaspth/56c4eb549b873083f17d710d3c7e8256 to your computer and use it in GitHub Desktop.
Trying to play around ideas with different FormBuilders for Rails.
class FormBuilder
def text_field(method, **options)
tag.input type: :text, **options_for(__method__, method, **options)
end
def options_for(type, method, index: @index, namespace: @options[:namespace], multiple: false, **options)
Options.new(self, type, method, index:, namespace:, multiple:, **options)
end
class Options
def initialize(form, type, method, index:, namespace:, multiple:, **options)
@options = options.dup
@options[:id] ||= form.field_id(method, index:, namespace:)
@options[:name] ||= form.field_name(method, index:, multiple:)
@options[:value] ||= form.field_value(method, type)
@options[:size] = @options[:maxlength] unless @options.key?(:size)
@options
end
delegate :to_hash, :to_h, to: :@options
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment