Permalink
Browse files

Merge pull request #770 from justinfrench/numeric_inputs

Refactor numeric inputs. Closes #758, #728
  • Loading branch information...
2 parents e8da720 + ed4c24b commit ab27e75e559774bcd9a381b9a927fb1da66354a0 @yabawock yabawock committed Jan 3, 2012
View
2 lib/formtastic/inputs/base.rb
@@ -43,7 +43,9 @@ def removed_option!(old_option_name)
autoload :Html
autoload :Labelling
autoload :Naming
+ autoload :Numeric
autoload :Options
+ autoload :Placeholder
autoload :Stringish
autoload :Timeish
autoload :Validations
View
50 lib/formtastic/inputs/base/numeric.rb
@@ -0,0 +1,50 @@
+module Formtastic
+ module Inputs
+ module Base
+ module Numeric
+ def input_html_options
+ defaults = super
+
+ # override rails default size - not valid on numeric inputs
+ #@todo document/spec
+ defaults[:size] = nil
+
+ if in_option
+ defaults[:min] = in_option.to_a.min
+ defaults[:max] = in_option.to_a.max
+ else
+ defaults[:min] ||= min_option
+ defaults[:max] ||= max_option
+ end
+ defaults[:step] ||= step_option
+ defaults
+ end
+
+ def step_option
+ return options[:step] if options.key?(:step)
+ validation_step
+ end
+
+ def min_option
+ return options[:min] if options.key?(:min)
+ validation_min
+ end
+
+ def max_option
+ return options[:max] if options.key?(:max)
+ validation_max
+ end
+
+ def in_option
+ options[:in]
+ end
+
+ def wrapper_html_options
+ new_class = [super[:class], "numeric", "stringish"].compact.join(" ")
+ super.merge(:class => new_class)
+ end
+
+ end
+ end
+ end
+end
View
17 lib/formtastic/inputs/base/placeholder.rb
@@ -0,0 +1,17 @@
+module Formtastic
+ module Inputs
+ module Base
+ module Placeholder
+
+ def input_html_options
+ {:placeholder => placeholder_text}.merge(super)
+ end
+
+ def placeholder_text
+ localized_string(method, options[:placeholder], :placeholder)
+ end
+
+ end
+ end
+ end
+end
View
9 lib/formtastic/inputs/base/stringish.rb
@@ -2,7 +2,7 @@ module Formtastic
module Inputs
module Base
module Stringish
-
+
# @abstract Override this method in your input class to describe how the input should render itself.
def to_html
input_wrapping do
@@ -15,15 +15,10 @@ def to_html
def input_html_options
{
:maxlength => options[:input_html].try(:[], :maxlength) || limit,
- :size => builder.default_text_field_size,
- :placeholder => placeholder_text
+ :size => builder.default_text_field_size
}.merge(super)
end
- def placeholder_text
- localized_string(method, options[:placeholder], :placeholder)
- end
-
def wrapper_html_options
new_class = [super[:class], "stringish"].compact.join(" ")
super.merge(:class => new_class)
View
11 lib/formtastic/inputs/base/validations.rb
@@ -108,7 +108,7 @@ def validation_step
validation.kind == :numericality
end
if validation
- validation.options[:step]
+ validation.options[:step] || (1 if validation_integer_only?)
else
nil
end
@@ -183,14 +183,7 @@ def autofocus?
end
def column_limit
- if column? && column.respond_to?(:limit)
- case column.type
- when :integer
- (2 ** (column.limit * 8)).to_s.length + 1
- else
- column.limit
- end
- end
+ column.limit if column? && column.respond_to?(:limit)
end
def limit
View
1 lib/formtastic/inputs/email_input.rb
@@ -28,6 +28,7 @@ module Inputs
class EmailInput
include Base
include Base::Stringish
+ include Base::Placeholder
def to_html
input_wrapping do
View
40 lib/formtastic/inputs/number_input.rb
@@ -69,50 +69,20 @@ module Inputs
# @see http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of Rails' Numericality validation documentation
class NumberInput
include Base
- include Base::Stringish
+ include Base::Numeric
+ include Base::Placeholder
def to_html
input_wrapping do
label_html <<
builder.number_field(method, input_html_options)
end
end
-
- def input_html_options
- defaults = super
-
- if in_option
- defaults[:min] = in_option.to_a.min
- defaults[:max] = in_option.to_a.max
- else
- defaults[:min] ||= min_option
- defaults[:max] ||= max_option
- end
- defaults[:step] ||= step_option
- defaults
- end
-
+
def step_option
- return options[:step] if options.key?(:step)
- return validation_step if validation_step
- return 1 if validation_integer_only?
- "any"
- end
-
- def min_option
- return options[:min] if options.key?(:min)
- validation_min
+ super || "any"
end
-
- def max_option
- return options[:max] if options.key?(:max)
- validation_max
- end
-
- def in_option
- options[:in]
- end
-
+
end
end
end
View
1 lib/formtastic/inputs/password_input.rb
@@ -28,6 +28,7 @@ module Inputs
class PasswordInput
include Base
include Base::Stringish
+ include Base::Placeholder
def to_html
input_wrapping do
View
1 lib/formtastic/inputs/phone_input.rb
@@ -28,6 +28,7 @@ module Inputs
class PhoneInput
include Base
include Base::Stringish
+ include Base::Placeholder
def to_html
input_wrapping do
View
38 lib/formtastic/inputs/range_input.rb
@@ -67,51 +67,27 @@ module Inputs
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documentation of all possible options.
# @see http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of Rails' Numericality validation documentation
#
- # @todo Is it still correct for this to be Stringish?
class RangeInput
include Base
- include Base::Stringish
+ include Base::Numeric
def to_html
input_wrapping do
label_html <<
builder.range_field(method, input_html_options)
end
end
-
- def input_html_options
- defaults = super
-
- if in_option
- defaults[:min] = in_option.to_a.min
- defaults[:max] = in_option.to_a.max
- else
- defaults[:min] ||= min_option
- defaults[:max] ||= max_option
- end
- defaults[:step] ||= step_option
- defaults
- end
-
- def step_option
- return options[:step] if options.key?(:step)
- return validation_step if validation_step
- return 1 if validation_integer_only?
- 1
- end
-
+
def min_option
- return options[:min] if options.key?(:min)
- validation_min || 1
+ super || 1
end
def max_option
- return options[:max] if options.key?(:max)
- validation_max || 100
+ super || 100
end
-
- def in_option
- options[:in]
+
+ def step_option
+ super || 1
end
end
View
1 lib/formtastic/inputs/search_input.rb
@@ -27,6 +27,7 @@ module Inputs
class SearchInput
include Base
include Base::Stringish
+ include Base::Placeholder
def to_html
input_wrapping do
View
2 lib/formtastic/inputs/string_input.rb
@@ -29,6 +29,8 @@ module Inputs
class StringInput
include Base
include Base::Stringish
+ include Base::Placeholder
+
end
end
end
View
8 lib/formtastic/inputs/text_input.rb
@@ -27,12 +27,12 @@ module Inputs
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documentation of all possible options.
class TextInput
include Base
+ include Base::Placeholder
def input_html_options
{
:cols => builder.default_text_area_width,
- :rows => builder.default_text_area_height,
- :placeholder => placeholder_text
+ :rows => builder.default_text_area_height
}.merge(super)
end
@@ -42,10 +42,6 @@ def to_html
builder.text_area(method, input_html_options)
end
end
-
- def placeholder_text
- localized_string(method, options[:placeholder], :placeholder)
- end
end
end
View
1 lib/formtastic/inputs/url_input.rb
@@ -28,6 +28,7 @@ module Inputs
class UrlInput
include Base
include Base::Stringish
+ include Base::Placeholder
def to_html
input_wrapping do
View
10 spec/inputs/number_input_spec.rb
@@ -26,17 +26,19 @@
it_should_have_input_wrapper_with_class(:number)
it_should_have_input_wrapper_with_class(:input)
+ it_should_have_input_wrapper_with_class(:numeric)
it_should_have_input_wrapper_with_class(:stringish)
it_should_have_input_wrapper_with_id("post_title_input")
it_should_have_label_with_text(/Title/)
it_should_have_label_for("post_title")
it_should_have_input_with_id("post_title")
it_should_have_input_with_type(:number)
it_should_have_input_with_name("post[title]")
- it_should_use_default_text_field_size_when_not_nil(:string)
- it_should_not_use_default_text_field_size_when_nil(:string)
- it_should_apply_custom_input_attributes_when_input_html_provided(:string)
- it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
+ # @todo this is not testing what it should be testing!
+ # it_should_use_default_text_field_size_when_not_nil(:string)
+ # it_should_not_use_default_text_field_size_when_nil(:string)
+ # it_should_apply_custom_input_attributes_when_input_html_provided(:string)
+ # it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
it_should_apply_error_logic_for_input_type(:number)
end
View
3 spec/inputs/range_input_spec.rb
@@ -19,7 +19,8 @@
it_should_have_input_wrapper_with_class(:range)
it_should_have_input_wrapper_with_class(:input)
- it_should_have_input_wrapper_with_class(:stringish) # might be removed
+ it_should_have_input_wrapper_with_class(:numeric)
+ it_should_have_input_wrapper_with_class(:stringish)
it_should_have_input_wrapper_with_id("author_age_input")
it_should_have_label_with_text(/Age/)
it_should_have_label_for("author_age")

0 comments on commit ab27e75

Please sign in to comment.