Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/simple_form/bootstrap/form_builders/date_time.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module SimpleForm::Bootstrap::FormBuilders::DateTime
DATE_TIME_COLUMN_TYPES = [
:datetime,
'datetime',
'timestamp',
'timestamp without time zone'
].freeze

DATE_COLUMN_TYPES = [
:date,
'date'
].freeze

def default_input_type(attribute_name, column, options, *args, &block)
if (options.is_a?(Hash) ? options[:as] : @options[:as]).nil? && !column.nil?
return :bootstrap_date_time if DATE_TIME_COLUMN_TYPES.include?(column.sql_type)
return :bootstrap_date if DATE_COLUMN_TYPES.include?(column.sql_type)
type = column.respond_to?(:type) ? column.type : column.sql_type
return :bootstrap_date_time if DATE_TIME_COLUMN_TYPES.include?(type)
return :bootstrap_date if DATE_COLUMN_TYPES.include?(type)
end

super(attribute_name, column, options, *args, &block)
Expand Down
40 changes: 24 additions & 16 deletions spec/simple_form/bootstrap/form_builders/date_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def persisted?
end

def column_for_attribute(*)
Struct.new(:sql_type).new(@column_sql_type)
if @column_sql_type.is_a?(Symbol)
Struct.new(:type).new(@column_sql_type)
else
Struct.new(:sql_type).new(@column_sql_type)
end
end

def has_attribute?(*)
Expand All @@ -34,35 +38,39 @@ def test
end

context 'when the database column is a datetime' do
let(:object) { DateTimeModel.new('datetime') }
it 'displays the text field' do
expect(rendered).to have_tag('div.form-group.bootstrap_date_time') do
with_tag('input.bootstrap_date_time', with: { value: object.test })
['datetime', :datetime].each do |type|
let(:object) { DateTimeModel.new(type) }
it 'displays the text field' do
expect(rendered).to have_tag('div.form-group.bootstrap_date_time') do
with_tag('input.bootstrap_date_time', with: { value: object.test })
end
end
end

it 'has a hidden datepicker control' do
selector = 'div.form-group.bootstrap_date_time div.input-group'
required_style = { style: 'display: none' }
it 'has a hidden datepicker control' do
selector = 'div.form-group.bootstrap_date_time div.input-group'
required_style = { style: 'display: none' }

expect(rendered).to have_tag(selector, with: required_style) do
with_tag('input.bootstrap_date_time', with: { type: 'hidden' })
expect(rendered).to have_tag(selector, with: required_style) do
with_tag('input.bootstrap_date_time', with: { type: 'hidden' })
end
end
end
end

context 'when the database column is a date' do
let(:object) { DateTimeModel.new('date') }
it 'displays the text field' do
expect(rendered).to have_tag('div.form-group.bootstrap_date') do
with_tag('input.bootstrap_date', with: { value: object.test })
['date', :date].each do |type|
let(:object) { DateTimeModel.new(type) }
it 'displays the text field' do
expect(rendered).to have_tag('div.form-group.bootstrap_date') do
with_tag('input.bootstrap_date', with: { value: object.test })
end
end
end
end

context 'when the database column is not a datetime' do
let(:object) { DateTimeModel.new('string') }
it 'does not have a datepicker control' do
it 'does not have a datepicker control' do
expect(rendered).not_to have_tag('div.form-group.bootstrap_date_time')
end
end
Expand Down