Permalink
Browse files

Recognize :size and :maxlength options as attributes for text inputs …

…in the default formatter
  • Loading branch information...
1 parent 8b9451a commit d8093e432b1cba5e43877e8f3319dbd8d572c424 @jeremyevans committed May 1, 2012
Showing with 24 additions and 6 deletions.
  1. +4 −0 CHANGELOG
  2. +11 −6 lib/forme.rb
  3. +9 −0 spec/forme_spec.rb
View
@@ -1,5 +1,9 @@
=== HEAD
+* Recognize :size and :maxlength options as attributes for text inputs in the default formatter (jeremyevans)
+
+* Recognize :style option as attributes in the default formatter (jeremyevans)
+
* Join attribute values specified as arrays with spaces instead of the empty string (jeremyevans)
* Make Sinatra ERB integration work with partials (jeremyevans)
View
@@ -591,7 +591,7 @@ class Formatter
# attributes hash, so they don't need to be specified in the :attr
# option. However, they can be specified in both places, and if so,
# the :attr option version takes precedence.
- ATTRIBUTE_OPTIONS = [:name, :id, :placeholder, :value]
+ ATTRIBUTE_OPTIONS = [:name, :id, :placeholder, :value, :style]
# Create a new instance and call it
def self.call(input)
@@ -725,6 +725,7 @@ def format_datetime
# with the type attribute set to input.
def _format_input(type)
@attr[:type] = type
+ copy_options_to_attributes([:size, :maxlength])
tag(:input)
end
@@ -802,15 +803,19 @@ def format_textarea
end
end
- # Normalize the options used for all input types. Handles:
- # :required :: Sets the +required+ attribute on the resulting tag if true.
- # :disabled :: Sets the +disabled+ attribute on the resulting tag if true.
- def normalize_options
- ATTRIBUTE_OPTIONS.each do |k|
+ def copy_options_to_attributes(attributes)
+ attributes.each do |k|
if @opts.has_key?(k) && !@attr.has_key?(k)
@attr[k] = @opts[k]
end
end
+ end
+
+ # Normalize the options used for all input types. Handles:
+ # :required :: Sets the +required+ attribute on the resulting tag if true.
+ # :disabled :: Sets the +disabled+ attribute on the resulting tag if true.
+ def normalize_options
+ copy_options_to_attributes(ATTRIBUTE_OPTIONS)
Forme.attr_classes(@attr, @opts[:class]) if @opts.has_key?(:class)
Forme.attr_classes(@attr, 'error') if @opts[:error]
View
@@ -42,6 +42,10 @@ def sel(opts, s)
@f.input(:text, :placeholder=>"foo").to_s.should == '<input placeholder="foo" type="text"/>'
end
+ specify "should use :style option as attribute" do
+ @f.input(:text, :style=>"foo").to_s.should == '<input style="foo" type="text"/>'
+ end
+
specify "should allow arbitrary attributes using the :attr option" do
@f.input(:text, :attr=>{:bar=>"foo"}).to_s.should == '<input bar="foo" type="text"/>'
end
@@ -62,6 +66,11 @@ def sel(opts, s)
@f.input(:text, :data=>{:bar=>"baz"}, :attr=>{:"data-bar"=>"foo"}).to_s.should == '<input data-bar="foo" type="text"/>'
end
+ specify "should use :size and :maxlength options as attributes for text inputs" do
+ @f.input(:text, :size=>5, :maxlength=>10).to_s.should == '<input maxlength="10" size="5" type="text"/>'
+ @f.input(:textarea, :size=>5, :maxlength=>10).to_s.should == '<textarea></textarea>'
+ end
+
specify "should create hidden input with value 0 for each checkbox with a name" do
@f.input(:checkbox, :name=>"foo").to_s.should == '<input name="foo" type="hidden" value="0"/><input name="foo" type="checkbox"/>'
end

0 comments on commit d8093e4

Please sign in to comment.