Skip to content

Commit

Permalink
Merge pull request #826 from justinfrench/rename_timeish_inputs
Browse files Browse the repository at this point in the history
Rename DateInput, TimeInput, DatetimeInput:
  • Loading branch information
justinfrench committed Mar 25, 2012
2 parents 7bfdb01 + a315547 commit e971195
Show file tree
Hide file tree
Showing 20 changed files with 294 additions and 206 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ HEAD
* deprecated :error_class option (use global configuration)
* deprecated :group_by and :group_label (use :collection option with a HTML string generated by Rails' grouped_options_for_select())
* deprecated :find_options
* renamed TimeInput (:as => :time) to TimeSelectInput (:as => :time_select), aliased and deprecated TimeInput
* renamed DatetimeInput (:as => :datetime) to DatetimeSelectInput (:as => :datetime_select), aliased and deprecated DatetimeInput
* renamed DateInput (:as => :date) to TimeSelectInput (:as => :date_select), aliased and deprecated DateInput

2.1.1

Expand Down
6 changes: 3 additions & 3 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ The Formtastic input types:
* @:time_zone@ - a select input. Default for column types: @:string@ with name matching @"time_zone"@.
* @:password@ - a password input. Default for column types: @:string@ with name matching @"password"@.
* @:text@ - a textarea. Default for column types: @:text@.
* @:date@ - a date select. Default for column types: @:date@.
* @:datetime@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@.
* @:time@ - a time select. Default for column types: @:time@.
* @:date_select@ - a date select. Default for column types: @:date@.
* @:datetime_select@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@.
* @:time_select@ - a time select. Default for column types: @:time@.
* @:boolean@ - a checkbox. Default for column types: @:boolean@.
* @:string@ - a text field. Default for column types: @:string@.
* @:number@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
Expand Down
14 changes: 9 additions & 5 deletions lib/formtastic/helpers/input_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ module InputHelper
# * `:boolean` (see {Inputs::BooleanInput})
# * `:check_boxes` (see {Inputs::CheckBoxesInput})
# * `:country` (see {Inputs::CountryInput})
# * `:datetime` (see {Inputs::DatetimeInput})
# * `:date` (see {Inputs::DateInput})
# * `:datetime_select` (see {Inputs::DatetimeSelectInput})
# * `:date_select` (see {Inputs::DateSelectInput})
# * `:email` (see {Inputs::EmailInput})
# * `:file` (see {Inputs::FileInput})
# * `:hidden` (see {Inputs::HiddenInput})
Expand All @@ -103,7 +103,7 @@ module InputHelper
# * `:string` (see {Inputs::StringInput})
# * `:text` (see {Inputs::TextInput})
# * `:time_zone` (see {Inputs::TimeZoneInput})
# * `:time` (see {Inputs::TimeInput})
# * `:time_select` (see {Inputs::TimeSelectInput})
# * `:url` (see {Inputs::UrlInput})
#
# Calling `:as => :string` (for example) will call `#to_html` on a new instance of
Expand Down Expand Up @@ -275,8 +275,12 @@ def default_input_type(method, options = {}) #:nodoc:
return :number
when :float, :decimal
return :number
when :timestamp
return :datetime
when :datetime, :timestamp
return :datetime_select
when :time
return :time_select
when :date
return :date_select
end

# Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database.
Expand Down
3 changes: 3 additions & 0 deletions lib/formtastic/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module Inputs
autoload :DateInput
autoload :DatePickerInput
autoload :DatetimePickerInput
autoload :DateSelectInput
autoload :DatetimeInput
autoload :DatetimeSelectInput
autoload :EmailInput
autoload :FileInput
autoload :HiddenInput
Expand All @@ -26,6 +28,7 @@ module Inputs
autoload :TextInput
autoload :TimeInput
autoload :TimePickerInput
autoload :TimeSelectInput
autoload :TimeZoneInput
autoload :Timeish
autoload :UrlInput
Expand Down
34 changes: 17 additions & 17 deletions lib/formtastic/inputs/base/timeish.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Formtastic
module Inputs
module Base
# Timeish inputs (`:date`, `:datetime`, `:time`) are similar to the Rails date and time
# Timeish inputs (`:date_select`, `:datetime_select`, `:time_select`) are similar to the Rails date and time
# helpers (`date_select`, `datetime_select`, `time_select`), rendering a series of `<select>`
# tags for each fragment (year, month, day, hour, minute, seconds). The fragments are then
# re-combined to a date by ActiveRecord through multi-parameter assignment.
Expand All @@ -21,12 +21,12 @@ module Base
# * floating the `<li>` fragments against each other as a single line
# * hiding the `<label>` of each fragment with `display:none`
#
# @example `:date` input with full form context and sample HTMl output
# @example `:date_select` input with full form context and sample HTMl output
#
# <%= semantic_form_for(@post) do |f| %>
# <%= f.inputs do %>
# ...
# <%= f.input :publish_at, :as => :date %>
# <%= f.input :publish_at, :as => :date_select %>
# <% end %>
# <% end %>
#
Expand Down Expand Up @@ -56,31 +56,31 @@ module Base
# </form>
#
#
# @example `:time` input
# <%= f.input :publish_at, :as => :time %>
# @example `:time_select` input
# <%= f.input :publish_at, :as => :time_select %>
#
# @example `:datetime` input
# <%= f.input :publish_at, :as => :datetime %>
# @example `:datetime_select` input
# <%= f.input :publish_at, :as => :datetime_select %>
#
# @example Change the labels for each fragment
# <%= f.input :publish_at, :as => :date, :labels => { :year => "Y", :month => "M", :day => "D" } %>
# <%= f.input :publish_at, :as => :date_select, :labels => { :year => "Y", :month => "M", :day => "D" } %>
#
# @example Skip a fragment (defaults to 1, skips all following fragments)
# <%= f.input :publish_at, :as => :datetime, :discard_minute => true %>
# <%= f.input :publish_at, :as => :datetime, :discard_hour => true %>
# <%= f.input :publish_at, :as => :datetime, :discard_day => true %>
# <%= f.input :publish_at, :as => :datetime, :discard_month => true %>
# <%= f.input :publish_at, :as => :datetime, :discard_year => true %>
# <%= f.input :publish_at, :as => :datetime_select, :discard_minute => true %>
# <%= f.input :publish_at, :as => :datetime_select, :discard_hour => true %>
# <%= f.input :publish_at, :as => :datetime_select, :discard_day => true %>
# <%= f.input :publish_at, :as => :datetime_select, :discard_month => true %>
# <%= f.input :publish_at, :as => :datetime_select, :discard_year => true %>
#
# @example Change the order
# <%= f.input :publish_at, :as => :date, :order => [:month, :day, :year] %>
# <%= f.input :publish_at, :as => :date_select, :order => [:month, :day, :year] %>
#
# @example Include seconds with times (excluded by default)
# <%= f.input :publish_at, :as => :time, :include_seconds => true %>
# <%= f.input :publish_at, :as => :time_select, :include_seconds => true %>
#
# @example Specify if there should be a blank option at the start of each select or not. Note that, unlike select inputs, :include_blank does not accept a string value.
# <%= f.input :publish_at, :as => :time, :include_blank => true %>
# <%= f.input :publish_at, :as => :time, :include_blank => false %>
# <%= f.input :publish_at, :as => :time_select, :include_blank => true %>
# <%= f.input :publish_at, :as => :time_select, :include_blank => false %>
#
# @todo Document i18n
# @todo Check what other Rails options are supported (`start_year`, `end_year`, `use_month_numbers`, `use_short_month`, `add_month_numbers`, `prompt`), write tests for them, and otherwise support them
Expand Down
34 changes: 5 additions & 29 deletions lib/formtastic/inputs/date_input.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
module Formtastic
module Inputs
# Outputs a series of select boxes for the fragments that make up a date (year, month, day).
#
# @see Formtastic::Inputs::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class DateInput
include Base
include Base::Timeish

# We don't want hour and minute fragments on a date input
def time_fragments
[]
class DateInput < DateSelectInput
def to_html
::ActiveSupport::Deprecation.warn("DateInput (:as => :date) has been renamed to DateSelectInput (:as => :date_select) and will be removed or changed in the next version of Formtastic, please update your forms.", caller(2))
super
end

def hidden_date_fragments
default_date_fragments - date_fragments
end

def hidden_fragments
hidden_date_fragments.map do |fragment|
template.hidden_field_tag(hidden_field_name(fragment), fragment_value(fragment), :id => fragment_id(fragment), :disabled => input_html_options[:disabled] )
end.join.html_safe
end

def fragment_value(fragment)
if fragment == :year
Time.now.year
else
'1'
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/formtastic/inputs/date_select_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Formtastic
module Inputs
# Outputs a series of select boxes for the fragments that make up a date (year, month, day).
#
# @see Formtastic::Inputs::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class DateSelectInput
include Base
include Base::Timeish

# We don't want hour and minute fragments on a date input
def time_fragments
[]
end

def hidden_date_fragments
default_date_fragments - date_fragments
end

def hidden_fragments
hidden_date_fragments.map do |fragment|
template.hidden_field_tag(hidden_field_name(fragment), fragment_value(fragment), :id => fragment_id(fragment), :disabled => input_html_options[:disabled] )
end.join.html_safe
end

def fragment_value(fragment)
if fragment == :year
Time.now.year
else
'1'
end
end
end
end
end
14 changes: 6 additions & 8 deletions lib/formtastic/inputs/datetime_input.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module Formtastic
module Inputs

# Outputs a series of select boxes for the fragments that make up a date and time (year, month, day, hour, minute, second).
#
# @see Formtastic::Inputs::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class DatetimeInput
include Base
include Base::Timeish
class DatetimeInput < DatetimeSelectInput
def to_html
::ActiveSupport::Deprecation.warn("DatetimeInput (:as => :datetime) has been renamed to DatetimeSelectInput (:as => :datetime_select) and will be removed or changed in the next version of Formtastic, please update your forms.", caller(2))
super
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/formtastic/inputs/datetime_select_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Formtastic
module Inputs

# Outputs a series of select boxes for the fragments that make up a date and time (year, month, day, hour, minute, second).
#
# @see Formtastic::Inputs::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class DatetimeSelectInput
include Base
include Base::Timeish
end
end
end
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/radio_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Inputs
#
# * a `:string` input (where you want to force the user to choose from a few specific strings rather than entering anything)
# * a `:boolean` checkbox input (where the user could choose yes or no, rather than checking a box)
# * a `:date`, `:time` or `:datetime` input (where the user could choose from a small set of pre-determined dates)
# * a `:date_select`, `:time_select` or `:datetime_select` input (where the user could choose from a small set of pre-determined dates)
# * a `:number` input (where the user could choose from a small set of pre-defined numbers)
# * a `:time_zone` input (where you want to provide your own small set of choices instead of relying on Rails)
# * a `:country` input (where you want to provide a small set of choices, no need for a plugin really)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/select_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Inputs
#
# * a `:string` input (where you want to force the user to choose from a few specific strings rather than entering anything)
# * a `:boolean` checkbox input (where the user could choose yes or no, rather than checking a box)
# * a `:date`, `:time` or `:datetime` input (where the user could choose from pre-selected dates)
# * a `:date_select`, `:time_select` or `:datetime_select` input (where the user could choose from pre-selected dates)
# * a `:number` input (where the user could choose from a set of pre-defined numbers)
# * a `:time_zone` input (where you want to provide your own set of choices instead of relying on Rails)
# * a `:country` input (no need for a plugin really)
Expand Down
32 changes: 4 additions & 28 deletions lib/formtastic/inputs/time_input.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
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::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class TimeInput
include Base
include Base::Timeish

# we don't want year / month / day fragments if :ignore_date => true
def fragments
time_fragments
class TimeInput < TimeSelectInput
def to_html
::ActiveSupport::Deprecation.warn("TimeInput (:as => :time) has been renamed to TimeSelectInput (:as => :time_select) and will be removed or changed in the next version of Formtastic, please update your forms.", caller(2))
super
end

def fragment_value(fragment)
value ? value.send(fragment) : ""
end

def hidden_fragments
if !options[:ignore_date]
date_fragments.map do |fragment|
template.hidden_field_tag(hidden_field_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
34 changes: 34 additions & 0 deletions lib/formtastic/inputs/time_select_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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::Base::Timeish Timeish module for documentation of date, time and datetime input options.
class TimeSelectInput
include Base
include Base::Timeish

# we don't want year / month / day fragments if :ignore_date => true
def fragments
time_fragments
end

def fragment_value(fragment)
value ? value.send(fragment) : ""
end

def hidden_fragments
if !options[:ignore_date]
date_fragments.map do |fragment|
template.hidden_field_tag(hidden_field_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 changes: 8 additions & 8 deletions spec/helpers/input_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,17 @@ def length_should_be_required(options)
default_input_type(:text).should == :text
end

it 'should default to :date for :date column types' do
default_input_type(:date).should == :date
it 'should default to :date_select for :date column types' do
default_input_type(:date).should == :date_select
end

it 'should default to :datetime for :datetime and :timestamp column types' do
default_input_type(:datetime).should == :datetime
default_input_type(:timestamp).should == :datetime
it 'should default to :datetime_select for :datetime and :timestamp column types' do
default_input_type(:datetime).should == :datetime_select
default_input_type(:timestamp).should == :datetime_select
end

it 'should default to :time for :time column types' do
default_input_type(:time).should == :time
it 'should default to :time_select for :time column types' do
default_input_type(:time).should == :time_select
end

it 'should default to :boolean for :boolean column types' do
Expand Down Expand Up @@ -492,7 +492,7 @@ def length_should_be_required(options)
end

it 'should call the corresponding input class with .to_html' do
[:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden, :string, :password, :number, :text, :file].each do |input_style|
[:select, :time_zone, :radio, :date_select, :datetime_select, :time_select, :boolean, :check_boxes, :hidden, :string, :password, :number, :text, :file].each do |input_style|
@new_post.stub!(:generic_column_name)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
semantic_form_for(@new_post) do |builder|
Expand Down

0 comments on commit e971195

Please sign in to comment.