Skip to content
Browse files

Support :label_position option in both of the labelers, can be set to…

… :before or :after to override the default
  • Loading branch information...
1 parent bd952f2 commit fb6acfc7361dd983a2c030217a06db797949138e @jeremyevans committed May 2, 2012
Showing with 38 additions and 3 deletions.
  1. +2 −0 CHANGELOG
  2. +20 −3 lib/forme.rb
  3. +16 −0 spec/forme_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Support :label_position option in both of the labelers, can be set to :before or :after to override the default (jeremyevans)
+
* Add Rails integration (jeremyevans)
* Make explicit labeler put label after checkboxes and radio buttons instead of before (jeremyevans)
View
23 lib/forme.rb
@@ -940,12 +940,22 @@ class Labeler
# the label occurs before the tag.
def call(tag, input)
label = input.opts[:label]
+ label_position = input.opts[:label_position]
if [:radio, :checkbox].include?(input.type)
if input.type == :checkbox && tag.is_a?(Array) && tag.length == 2 && tag.first.attr[:type].to_s == 'hidden'
- return [tag.first , input.tag(:label, input.opts[:label_attr]||{}, [tag.last, ' ', label])]
+ t = if label_position == :before
+ [label, ' ', tag.last]
+ else
+ [tag.last, ' ', label]
+ end
+ return [tag.first , input.tag(:label, input.opts[:label_attr]||{}, t)]
+ elsif label_position == :before
+ t = [label, ' ', tag]
else
t = [tag, ' ', label]
end
+ elsif label_position == :after
+ t = [tag, ' ', label]
else
t = [label, ": ", tag]
end
@@ -968,9 +978,16 @@ class Labeler::Explicit
# associated with an input.
def call(tag, input)
if [:radio, :checkbox].include?(input.type)
- [tag, input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]])]
+ t = [tag, input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]])]
+ p = :before
+ else
+ t = [input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]]), tag]
+ p = :after
+ end
+ if input.opts[:label_position] == p
+ t.reverse
else
- [input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]]), tag]
+ t
end
end
end
View
16 spec/forme_spec.rb
@@ -200,6 +200,14 @@ def sel(opts, s)
@f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a').to_s.should == '<input name="a" type="hidden" value="0"/><label><input name="a" type="checkbox" value="foo"/> Foo</label>'
end
+ specify "should handle implicit labels with :label_position=>:after" do
+ @f.input(:text, :label=>'Foo', :value=>'foo', :label_position=>:after).to_s.should == '<label><input type="text" value="foo"/> Foo</label>'
+ end
+
+ specify "should handle implicit labels with checkboxes with :label_position=>:before" do
+ @f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a', :label_position=>:before).to_s.should == '<input name="a" type="hidden" value="0"/><label>Foo <input name="a" type="checkbox" value="foo"/></label>'
+ end
+
specify "should automatically note the input has errors if :error option is used" do
@f.input(:text, :error=>'Bad Stuff!', :value=>'foo').to_s.should == '<input class="error" type="text" value="foo"/><span class="error_message">Bad Stuff!</span>'
end
@@ -305,10 +313,18 @@ def sel(opts, s)
@f.input(:textarea, :labeler=>:explicit, :label=>'bar', :id=>:foo).to_s.should == '<label for="foo">bar</label><textarea id="foo"></textarea>'
end
+ specify "inputs handle explicit labels with :label_position=>:after" do
+ @f.input(:textarea, :labeler=>:explicit, :label=>'bar', :id=>:foo, :label_position=>:after).to_s.should == '<textarea id="foo"></textarea><label for="foo">bar</label>'
+ end
+
specify "should handle explicit labels with checkboxes" do
@f.input(:checkbox, :labeler=>:explicit, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar').to_s.should == '<input id="bar_hidden" name="a" type="hidden" value="0"/><input id="bar" name="a" type="checkbox" value="foo"/><label for="bar">Foo</label>'
end
+ specify "should handle explicit labels with checkboxes with :label_position=>:before" do
+ @f.input(:checkbox, :labeler=>:explicit, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar', :label_position=>:before).to_s.should == '<label for="bar">Foo</label><input id="bar_hidden" name="a" type="hidden" value="0"/><input id="bar" name="a" type="checkbox" value="foo"/>'
+ end
+
specify "inputs should accept a :error_handler option to use a custom error_handler" do
@f.input(:textarea, :error_handler=>proc{|t, i| [t, "!!! #{i.opts[:error]}"]}, :error=>'bar', :id=>:foo).to_s.should == '<textarea class="error" id="foo"></textarea>!!! bar'
end

0 comments on commit fb6acfc

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