Permalink
Browse files

Fixed #634, where TimeInput was not rendering hidden y/m/d inputs by …

…default.
  • Loading branch information...
1 parent b66cb77 commit b043bd63ad91657be7dd02a79cdf86e854b8a946 @justinfrench committed Aug 16, 2011
Showing with 61 additions and 10 deletions.
  1. +5 −0 lib/formtastic/inputs/base/timeish.rb
  2. +25 −3 lib/formtastic/inputs/time_input.rb
  3. +31 −7 spec/inputs/time_input_spec.rb
@@ -90,6 +90,7 @@ module Timeish
def to_html
input_wrapping do
fragments_wrapping do
+ hidden_fragments <<
fragments_label <<
template.content_tag(:ol,
fragments.map do |fragment|
@@ -206,6 +207,10 @@ def fragments_inner_wrapping(&block)
)
end
+ def hidden_fragments
+ "".html_safe
+ end
+
end
end
end
@@ -1,16 +1,38 @@
module Formtastic
module Inputs
# Outputs a series of select boxes for the fragments that make up a time (hour, minute, second).
+ # Unless `:ignore_date` is true, it will render hidden inputs for the year, month and day as
+ # well, defaulting to `Time.current` if the form object doesn't have a value, much like Rails'
+ # own `time_select`.
#
# @see Formtastic::Inputs::Timeish Timeish module for documetation of date, time and datetime input options.
class TimeInput
include Base
include Base::Timeish
- # we don't want year / month / day fragments in a time select
- def date_fragments
- []
+ # we don't want year / month / day fragments if :ignore_date => true
+ def fragments
+ time_fragments
end
+
+ def value_or_default_value
+ value ? value : Time.current
+ end
+
+ def fragment_value(fragment)
+ value_or_default_value.send(fragment)
+ end
+
+ def hidden_fragments
+ if !options[:ignore_date]
+ date_fragments.map do |fragment|
+ template.hidden_field_tag("#{object_name}[#{fragment_name(fragment)}]", fragment_value(fragment), :id => fragment_id(fragment), :disabled => input_html_options[:disabled] )
+ end.join.html_safe
+ else
+ super
+ end
+ end
+
end
end
end
@@ -16,22 +16,46 @@
output_buffer.replace ''
end
- describe "with :ignore_date" do
+ describe "with :ignore_date => true" do
before do
concat(semantic_form_for(@new_post) do |builder|
concat(builder.input(:publish_at, :as => :time, :ignore_date => true))
end)
end
- it 'should not have an input for day, month and year' do
- output_buffer.should_not have_tag('#post_publish_at_1i')
- output_buffer.should_not have_tag('#post_publish_at_2i')
- output_buffer.should_not have_tag('#post_publish_at_3i')
+ it 'should not have hidden inputs for day, month and year' do
+ output_buffer.should_not have_tag('input#post_publish_at_1i')
+ output_buffer.should_not have_tag('input#post_publish_at_2i')
+ output_buffer.should_not have_tag('input#post_publish_at_3i')
end
it 'should have an input for hour and minute' do
- output_buffer.should have_tag('#post_publish_at_4i')
- output_buffer.should have_tag('#post_publish_at_5i')
+ output_buffer.should have_tag('select#post_publish_at_4i')
+ output_buffer.should have_tag('select#post_publish_at_5i')
+ end
+
+ end
+
+ describe "with :ignore_date => false" do
+ before do
+ @new_post.stub(:publish_at).and_return(Time.parse('2010-11-07'))
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :time, :ignore_date => false))
+ end)
+ end
+
+ it 'should have a hidden input for day, month and year' do
+ output_buffer.should have_tag('input#post_publish_at_1i')
+ output_buffer.should have_tag('input#post_publish_at_2i')
+ output_buffer.should have_tag('input#post_publish_at_3i')
+ output_buffer.should have_tag('input#post_publish_at_1i[@value="2010"]')
+ output_buffer.should have_tag('input#post_publish_at_2i[@value="11"]')
+ output_buffer.should have_tag('input#post_publish_at_3i[@value="7"]')
+ end
+
+ it 'should have an select for hour and minute' do
+ output_buffer.should have_tag('select#post_publish_at_4i')
+ output_buffer.should have_tag('select#post_publish_at_5i')
end
end

0 comments on commit b043bd6

Please sign in to comment.