From 14b6b2e5fd8cf849508dd816d0239bbc1adeaa0c Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 29 Jun 2024 18:03:06 +0200 Subject: [PATCH] Add weekday input. Closes https://github.com/heartcombo/simple_form/issues/1770 --- CHANGELOG.md | 1 + README.md | 1 + lib/simple_form/inputs.rb | 1 + lib/simple_form/inputs/weekday_input.rb | 14 ++++++++++++++ test/inputs/weekday_input_test.rb | 19 +++++++++++++++++++ 5 files changed, 36 insertions(+) create mode 100644 lib/simple_form/inputs/weekday_input.rb create mode 100644 test/inputs/weekday_input_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7d57da9..ec90eaba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased * Remove redundant `aria-required` attribute for required fields. [@aduth](https://github.com/aduth) +* Add `weekday` input. [@nashby](https://github.com/nashby) ## 5.3.1 diff --git a/README.md b/README.md index 1f516d9f1..97dc60d07 100644 --- a/README.md +++ b/README.md @@ -621,6 +621,7 @@ Mapping | Generated HTML Element | Database Column Type `datetime` | `datetime select` | `datetime/timestamp` `date` | `date select` | `date` `time` | `time select` | `time` +`weekday` | `select` (weekdays as options) | - `select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations `radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations `check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations diff --git a/lib/simple_form/inputs.rb b/lib/simple_form/inputs.rb index 2485d3914..2476debf6 100644 --- a/lib/simple_form/inputs.rb +++ b/lib/simple_form/inputs.rb @@ -22,5 +22,6 @@ module Inputs autoload :RichTextAreaInput autoload :StringInput autoload :TextInput + autoload :WeekdayInput end end diff --git a/lib/simple_form/inputs/weekday_input.rb b/lib/simple_form/inputs/weekday_input.rb new file mode 100644 index 000000000..131ae2ccf --- /dev/null +++ b/lib/simple_form/inputs/weekday_input.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +module SimpleForm + module Inputs + class WeekdayInput < CollectionSelectInput + enable :placeholder + + def input(wrapper_options = nil) + merged_input_options = merge_wrapper_options(input_html_options, wrapper_options) + + @builder.weekday_select(attribute_name, input_options, merged_input_options) + end + end + end +end diff --git a/test/inputs/weekday_input_test.rb b/test/inputs/weekday_input_test.rb new file mode 100644 index 000000000..b8d80370c --- /dev/null +++ b/test/inputs/weekday_input_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +# encoding: UTF-8 +require 'test_helper' + +class WeekdayInputTest < ActionView::TestCase + test 'input generates a weekday select' do + if ActionView::VERSION::MAJOR >= 7 + with_input_for @user, :born_at, :weekday + assert_select 'select.weekday#user_born_at' + end + end + + test 'input generates a weekday select that accepts placeholder' do + if ActionView::VERSION::MAJOR >= 7 + with_input_for @user, :born_at, :weekday, placeholder: 'Put in a weekday' + assert_select 'select.weekday[placeholder="Put in a weekday"]' + end + end +end