Skip to content

Commit

Permalink
Split country and timezone inputs to fix deprecated way of passing pr…
Browse files Browse the repository at this point in the history
…iority countries to country input.

It still uses SimpleForm's PriorityInput for backward compatibility.
  • Loading branch information
nashby committed Sep 23, 2022
1 parent 31fe255 commit f38c6a1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Update Bootstrap install generator version 5. [@mhw](https://github.com/mhw)
* Accept proc as `group_method` for grouped collection select
* Honor `include_hidden` option on inline boolean inputs [@yboulkaid](https://github.com/yboulkaid)
* Fix deprecation error when using country_select input.

## 5.1.0

Expand Down
14 changes: 3 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ GEM
tzinfo (~> 2.0)
builder (3.2.4)
concurrent-ruby (1.1.9)
countries (4.2.2)
i18n_data (~> 0.15.0)
countries (5.1.0)
sixarm_ruby_unaccent (~> 1.1)
country_select (6.1.1)
countries (~> 4.2)
sort_alphabetical (~> 1.1)
country_select (8.0.0)
countries (~> 5.0)
crass (1.0.6)
erubi (1.10.0)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
i18n_data (0.15.0)
simple_po_parser (~> 1.1)
loofah (2.14.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -68,14 +64,10 @@ GEM
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.0.6)
simple_po_parser (1.1.5)
sixarm_ruby_unaccent (1.2.0)
sort_alphabetical (1.1.0)
unicode_utils (>= 1.2.2)
thor (1.2.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode_utils (1.4.0)
zeitwerk (2.5.4)

PLATFORMS
Expand Down
18 changes: 16 additions & 2 deletions lib/simple_form/inputs/priority_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class PriorityInput < CollectionSelectInput
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

@builder.send(:"#{input_type}_select", attribute_name, input_priority,
input_options, merged_input_options)
send(:"#{input_type}_input", merged_input_options)
end

def input_priority
Expand All @@ -15,6 +14,21 @@ def input_priority

protected

def country_input(merged_input_options)
@builder.send(:country_select,
attribute_name,
input_options.merge(priority_countries: input_priority),
merged_input_options)
end

def time_zone_input(merged_input_options)
@builder.send(:time_zone_select,
attribute_name,
input_priority,
input_options,
merged_input_options)
end

def skip_include_blank?
super || input_priority.present?
end
Expand Down
36 changes: 36 additions & 0 deletions test/inputs/country_input_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true
# encoding: UTF-8
require 'test_helper'

class CountryInputTest < ActionView::TestCase
test 'input generates a country select field' do
with_input_for @user, :country, :country
assert_select 'select#user_country'
assert_select 'select option[value=BR]', 'Brazil'
assert_no_select 'select option[value=""][disabled=disabled]'
end

test 'input generates a country select with SimpleForm default' do
swap SimpleForm, country_priority: [ 'Brazil' ] do
with_input_for @user, :country, :country
assert_select 'select option[value="BR"] + option[value="---------------"][disabled=disabled]'
end
end

test 'input generates a country select using options priority' do
with_input_for @user, :country, :country, priority: [ 'Ukraine' ]
assert_select 'select option[value="UA"] + option[value="---------------"][disabled=disabled]'
end

test 'input does generate select element with required html attribute' do
with_input_for @user, :country, :country
assert_select 'select.required'
assert_select 'select[required]'
end

test 'input does generate select element with aria-required html attribute' do
with_input_for @user, :country, :country
assert_select 'select.required'
assert_select 'select[aria-required]'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@
# encoding: UTF-8
require 'test_helper'

class PriorityInputTest < ActionView::TestCase
test 'input generates a country select field' do
with_input_for @user, :country, :country
assert_select 'select#user_country'
assert_select 'select option[value=BR]', 'Brazil'
assert_no_select 'select option[value=""][disabled=disabled]'
end

test 'input generates a country select with SimpleForm default' do
swap SimpleForm, country_priority: [ 'Brazil' ] do
with_input_for @user, :country, :country
assert_select 'select option[value="---------------"][disabled=disabled]'
end
end

class TimeZoneInputTest < ActionView::TestCase
test 'input generates a time zone select field' do
with_input_for @user, :time_zone, :time_zone
assert_select 'select#user_time_zone'
Expand All @@ -36,14 +22,14 @@ class PriorityInputTest < ActionView::TestCase
assert_no_select 'select option[value=""]', /^$/
end

test 'priority input does generate select element with required html attribute' do
with_input_for @user, :country, :country
test 'input does generate select element with required html attribute' do
with_input_for @user, :time_zone, :time_zone
assert_select 'select.required'
assert_select 'select[required]'
end

test 'priority input does generate select element with aria-required html attribute' do
with_input_for @user, :country, :country
test 'input does generate select element with aria-required html attribute' do
with_input_for @user, :time_zone, :time_zone
assert_select 'select.required'
assert_select 'select[aria-required]'
end
Expand Down

0 comments on commit f38c6a1

Please sign in to comment.