Skip to content

Commit 7869130

Browse files
authored
Merge pull request #3 from allenwq/master
Use type instead of sql_type
2 parents c39aa4f + 732ed15 commit 7869130

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

lib/simple_form/bootstrap/form_builders/date_time.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
module SimpleForm::Bootstrap::FormBuilders::DateTime
22
DATE_TIME_COLUMN_TYPES = [
3+
:datetime,
34
'datetime',
45
'timestamp',
56
'timestamp without time zone'
67
].freeze
78

89
DATE_COLUMN_TYPES = [
10+
:date,
911
'date'
1012
].freeze
1113

1214
def default_input_type(attribute_name, column, options, *args, &block)
1315
if (options.is_a?(Hash) ? options[:as] : @options[:as]).nil? && !column.nil?
14-
return :bootstrap_date_time if DATE_TIME_COLUMN_TYPES.include?(column.sql_type)
15-
return :bootstrap_date if DATE_COLUMN_TYPES.include?(column.sql_type)
16+
type = column.respond_to?(:type) ? column.type : column.sql_type
17+
return :bootstrap_date_time if DATE_TIME_COLUMN_TYPES.include?(type)
18+
return :bootstrap_date if DATE_COLUMN_TYPES.include?(type)
1619
end
1720

1821
super(attribute_name, column, options, *args, &block)

spec/simple_form/bootstrap/form_builders/date_time_spec.rb

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ def persisted?
1414
end
1515

1616
def column_for_attribute(*)
17-
Struct.new(:sql_type).new(@column_sql_type)
17+
if @column_sql_type.is_a?(Symbol)
18+
Struct.new(:type).new(@column_sql_type)
19+
else
20+
Struct.new(:sql_type).new(@column_sql_type)
21+
end
1822
end
1923

2024
def has_attribute?(*)
@@ -34,35 +38,39 @@ def test
3438
end
3539

3640
context 'when the database column is a datetime' do
37-
let(:object) { DateTimeModel.new('datetime') }
38-
it 'displays the text field' do
39-
expect(rendered).to have_tag('div.form-group.bootstrap_date_time') do
40-
with_tag('input.bootstrap_date_time', with: { value: object.test })
41+
['datetime', :datetime].each do |type|
42+
let(:object) { DateTimeModel.new(type) }
43+
it 'displays the text field' do
44+
expect(rendered).to have_tag('div.form-group.bootstrap_date_time') do
45+
with_tag('input.bootstrap_date_time', with: { value: object.test })
46+
end
4147
end
42-
end
4348

44-
it 'has a hidden datepicker control' do
45-
selector = 'div.form-group.bootstrap_date_time div.input-group'
46-
required_style = { style: 'display: none' }
49+
it 'has a hidden datepicker control' do
50+
selector = 'div.form-group.bootstrap_date_time div.input-group'
51+
required_style = { style: 'display: none' }
4752

48-
expect(rendered).to have_tag(selector, with: required_style) do
49-
with_tag('input.bootstrap_date_time', with: { type: 'hidden' })
53+
expect(rendered).to have_tag(selector, with: required_style) do
54+
with_tag('input.bootstrap_date_time', with: { type: 'hidden' })
55+
end
5056
end
5157
end
5258
end
5359

5460
context 'when the database column is a date' do
55-
let(:object) { DateTimeModel.new('date') }
56-
it 'displays the text field' do
57-
expect(rendered).to have_tag('div.form-group.bootstrap_date') do
58-
with_tag('input.bootstrap_date', with: { value: object.test })
61+
['date', :date].each do |type|
62+
let(:object) { DateTimeModel.new(type) }
63+
it 'displays the text field' do
64+
expect(rendered).to have_tag('div.form-group.bootstrap_date') do
65+
with_tag('input.bootstrap_date', with: { value: object.test })
66+
end
5967
end
6068
end
6169
end
6270

6371
context 'when the database column is not a datetime' do
6472
let(:object) { DateTimeModel.new('string') }
65-
it 'does not have a datepicker control' do
73+
it 'does not have a datepicker control' do
6674
expect(rendered).not_to have_tag('div.form-group.bootstrap_date_time')
6775
end
6876
end

0 commit comments

Comments
 (0)