[openstreetmap/openstreetmap-website] Add dir=auto to text inputs (PR #3430)

Andy Allan notifications at github.com
Wed Feb 16 17:59:09 UTC 2022


> I haven't researched how easy rails makes it to add your own attributes to the field helpers.

I've been looking into this today, and I haven't found any particularly neat way of doing this, other than monkey-patching the TextField initializer to add the option, e.g.

```ruby
# Apply dir=auto to all text fields
# Also includes fields subclassed from TextField, like PasswordField, EmailField etc
module ActionView
  module Helpers
    module Tags
      class TextField
        def initialize(object_name, method_name, template_object, options = {})
          super(object_name, method_name, template_object, options.reverse_merge({ :dir => :auto }))
        end
      end
    end
  end
end
```
Reading the source code for [TextField](https://github.com/rails/rails/blob/7-0-stable/actionview/lib/action_view/helpers/tags/text_field.rb) and [Base](https://github.com/rails/rails/blob/7-0-stable/actionview/lib/action_view/helpers/tags/base.rb) doesn't give me any hints that there are configurable defaults for the options hashes, only that they are expected to be passed in by the caller.

I guess a non-monkey-patching approach would be to use our own FormBuilder that subclasses bootstrap_form's [FormBuilder](https://github.com/bootstrap-ruby/bootstrap_form/blob/main/lib/bootstrap_form/form_builder.rb), and add the default html attributes at that stage. I haven't explored that approach yet to see if there's any gotchas there.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/3430#issuecomment-1041941272
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/3430/c1041941272 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20220216/7366e273/attachment.htm>


More information about the rails-dev mailing list