Skip to content
Browse files

Added support for the :selected option for datetime/date/time input t…

…ypes. Specs and RDoc notes added as well.
  • Loading branch information...
1 parent 48a5c37 commit e1993e2669d13e4e080b63495469d6098be891c0 @grimen grimen committed with justinfrench Nov 28, 2009
View
28 lib/formtastic.rb
@@ -830,7 +830,15 @@ def radio_input(method, options)
# items (li), one for each fragment for the date (year, month, day). Each li contains a label
# (eg "Year") and a select box. See date_or_datetime_input for a more detailed output example.
#
+ # You can pre-select a specific option value by passing in the :selected option.
+ #
+ # Examples:
+ #
+ # f.input :created_at, :as => :date, :selected => 1.day.ago
+ # f.input :created_at, :as => :date, :selected => nil # override any defaults: select none
+ #
# Some of Rails' options for select_date are supported, but not everything yet.
+ #
def date_input(method, options)
options = set_include_blank(options)
date_or_datetime_input(method, options.merge(:discard_hour => true))
@@ -841,7 +849,15 @@ def date_input(method, options)
# contains a label (eg "Year") and a select box. See date_or_datetime_input for a more
# detailed output example.
#
+ # You can pre-select a specific option value by passing in the :selected option.
+ #
+ # Examples:
+ #
+ # f.input :created_at, :as => :datetime, :selected => 1.day.ago
+ # f.input :created_at, :as => :datetime, :selected => nil # override any defaults: select none
+ #
# Some of Rails' options for select_date are supported, but not everything yet.
+ #
def datetime_input(method, options)
options = set_include_blank(options)
date_or_datetime_input(method, options)
@@ -851,7 +867,15 @@ def datetime_input(method, options)
# items (li), one for each fragment for the time (hour, minute, second). Each li contains a label
# (eg "Hour") and a select box. See date_or_datetime_input for a more detailed output example.
#
+ # You can pre-select a specific option value by passing in the :selected option.
+ #
+ # Examples:
+ #
+ # f.input :created_at, :as => :time, :selected => 1.hour.ago
+ # f.input :created_at, :as => :time, :selected => nil # override any defaults: select none
+ #
# Some of Rails' options for select_time are supported, but not everything yet.
+ #
def time_input(method, options)
options = set_include_blank(options)
date_or_datetime_input(method, options.merge(:discard_year => true, :discard_month => true, :discard_day => true))
@@ -901,10 +925,10 @@ def date_or_datetime_input(method, options)
list_items_capture = ""
hidden_fields_capture = ""
- default_time = Time.now
+ default_time = ::Time.now
# Gets the datetime object. It can be a Fixnum, Date or Time, or nil.
- datetime = (@object ? @object.send(method) : default_time) || default_time
+ datetime = options[:selected] || (@object ? @object.send(method) : default_time) || default_time
html_options = options.delete(:input_html) || {}
input_ids = []
View
53 spec/custom_macros.rb
@@ -247,8 +247,8 @@ def it_should_select_existing_datetime_else_current(*datetime_parts)
output_buffer.should have_tag("form li select#post_publish_at_2i option[@selected='selected'][@value='#{@new_post.publish_at.month}']") if datetime_parts.include?(:month)
output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@new_post.publish_at.day}']") if datetime_parts.include?(:day)
output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@new_post.publish_at.strftime("%H")}']") if datetime_parts.include?(:hour)
- output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@new_post.publish_at.min}']") if datetime_parts.include?(:minute)
- # output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@new_post.publish_at.sec}']") if datetime_parts.include?(:second)
+ output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@new_post.publish_at.strftime("%M")}']") if datetime_parts.include?(:minute)
+ #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@new_post.publish_at.sec}']") if datetime_parts.include?(:second)
end
end
@@ -270,14 +270,61 @@ def it_should_select_existing_datetime_else_current(*datetime_parts)
output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@current_time.day}']") if datetime_parts.include?(:day)
output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@current_time.strftime("%H")}']") if datetime_parts.include?(:hour)
output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@current_time.strftime("%M")}']") if datetime_parts.include?(:minute)
- #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@current_time.sec}']") if datetime_parts.include?(:second)
+ #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@custom_default_time.sec}']") if datetime_parts.include?(:second)
end
# TODO: Scenario when current time is not a possible choice (because of specified date/time ranges)?
end
end
end
+ def it_should_select_explicit_default_value_if_set(*datetime_parts)
+ describe 'when :selected is set' do
+ before do
+ @output_buffer = ''
+ end
+
+ # Note: Not possible to override default selected value for time_zone input
+ # without overriding Rails core helper. This Rails helper works "a bit different". =/
+ #
+ describe "no selected items" do
+ before do
+ @default_time = 2.days.ago
+ @new_post.stub!(:publish_at).and_return(@default_time)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :time_zone, :selected => nil))
+ end
+ end
+
+ it 'should not have any selected item(s)' do
+ output_buffer.should_not have_tag("form li select#post_publish_at_1i option[@selected='selected']")
+ end
+ end
+
+ describe "single selected item" do
+ before do
+ @custom_default_time = 5.days.ago
+ @new_post.stub!(:publish_at).and_return(2.days.ago)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime, :selected => @custom_default_time))
+ end
+ end
+
+ it "should select the specified value" do
+ output_buffer.should have_tag("form li select#post_publish_at_1i option[@selected='selected'][@value='#{@custom_default_time.year}']") if datetime_parts.include?(:year)
+ output_buffer.should have_tag("form li select#post_publish_at_2i option[@selected='selected'][@value='#{@custom_default_time.month}']") if datetime_parts.include?(:month)
+ output_buffer.should have_tag("form li select#post_publish_at_3i option[@selected='selected'][@value='#{@custom_default_time.day}']") if datetime_parts.include?(:day)
+ output_buffer.should have_tag("form li select#post_publish_at_4i option[@selected='selected'][@value='#{@custom_default_time.strftime("%H")}']") if datetime_parts.include?(:hour)
+ output_buffer.should have_tag("form li select#post_publish_at_5i option[@selected='selected'][@value='#{@custom_default_time.strftime("%M")}']") if datetime_parts.include?(:minute)
+ #output_buffer.should have_tag("form li select#post_publish_at_6i option[@selected='selected'][@value='#{@custom_default_time.sec}']") if datetime_parts.include?(:second)
+ end
+ end
+
+ end
+ end
+
def it_should_use_the_collection_when_provided(as, countable)
describe 'when the :collection option is provided' do
View
1 spec/inputs/date_input_spec.rb
@@ -40,5 +40,6 @@
end
it_should_select_existing_datetime_else_current(:year, :month, :day)
+ it_should_select_explicit_default_value_if_set(:year, :month, :day)
end
View
3 spec/inputs/datetime_input_spec.rb
@@ -59,7 +59,8 @@
end
end
- it_should_select_existing_datetime_else_current(:year, :month, :day, :hour, :minute)
+ it_should_select_existing_datetime_else_current(:year, :month, :day, :hour, :minute, :second)
+ it_should_select_explicit_default_value_if_set(:year, :month, :day, :hour, :minute, :second)
describe 'when :discard_input => true is set' do
it 'should use default attribute value when it is not nil' do
View
1 spec/inputs/time_input_spec.rb
@@ -39,5 +39,6 @@
end
it_should_select_existing_datetime_else_current(:hour, :minute, :second)
+ it_should_select_explicit_default_value_if_set(:hour, :minute, :second)
end

0 comments on commit e1993e2

Please sign in to comment.
Something went wrong with that request. Please try again.