Skip to content

Commit

Permalink
Merge pull request #1782 from heartcombo/country-timezone-split
Browse files Browse the repository at this point in the history
Split country and timezone inputs to fix deprecated way of passing priority countries to country input.
  • Loading branch information
nashby authored Dec 29, 2022
2 parents 31fe255 + b472e19 commit 7790db2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 37 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
6 changes: 3 additions & 3 deletions gemfiles/Gemfile-rails-main
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ source 'https://rubygems.org'
gemspec path: '..'

gem 'country_select'
gem 'railties', github: 'rails/rails'
gem 'activemodel', github: 'rails/rails'
gem 'actionpack', github: 'rails/rails'
gem 'railties', github: 'rails/rails', branch: 'main'
gem 'activemodel', github: 'rails/rails', branch: 'main'
gem 'actionpack', github: 'rails/rails', branch: 'main'
gem 'rake'
gem 'tzinfo'
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
4 changes: 2 additions & 2 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ class WrapperTest < ActionView::TestCase
with_form_for @user, :name
assert_no_select "section.custom_wrapper div.another_wrapper label"
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
output_buffer.replace ""
output_buffer.to_s.replace ""

with_form_for @user, :name, wrapper: :another
assert_select "section.custom_wrapper div.another_wrapper label"
assert_select "section.custom_wrapper div.another_wrapper input.string"
output_buffer.replace ""
output_buffer.to_s.replace ""
end

with_form_for @user, :name, wrapper: custom_wrapper
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 7790db2

Please sign in to comment.