Created
June 15, 2023 16:53
-
-
Save kaspth/56c4eb549b873083f17d710d3c7e8256 to your computer and use it in GitHub Desktop.
Trying to play around ideas with different FormBuilders for Rails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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