Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/wycats/merb-plugins into rs…
Browse files Browse the repository at this point in the history
…pec_and_test_unit
  • Loading branch information
benburkert committed Feb 13, 2008
2 parents 65d5cb5 + d9ba497 commit 3205959
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 20 deletions.
4 changes: 2 additions & 2 deletions merb_datamapper/lib/merb/session/data_mapper_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.included(base)
base.add_hook :after_dispatch do
Merb.logger.info("Finalize DataMapper session")
request.session.save if @_fingerprint != Marshal.dump(request.session.data).hash
set_cookie(_session_id_key, request.session.session_id, _session_expiry) if (@_new_cookie || request.session.needs_new_cookie)
set_cookie(_session_id_key, request.session.session_id, Time.now + _session_expiry) if (@_new_cookie || request.session.needs_new_cookie)
end
end

Expand All @@ -28,7 +28,7 @@ def session_store_type
class DataMapperSession < DataMapper::Base

set_table_name "sessions"
property :session_id, :string, :length => 255, :lazy => false, :key => true, :serial => true
property :session_id, :string, :length => 255, :lazy => false, :key => true
property :data, :text, :lazy => false

attr_accessor :needs_new_cookie
Expand Down
17 changes: 13 additions & 4 deletions merb_helpers/lib/merb_helpers/form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,10 @@ def radio_group_control(col, options = [], attrs = {})
val = @_obj.send(col)
ret = ""
options.each do |opt|
value, label = opt.is_a?(Hash) ? [opt[:value], opt[:label]] : [opt, opt]
hash = {:name => "#{@_object_name}[#{col}]", :value => value, :label => label}
hash.merge!(:selected => "selected") if val.to_s == value.to_s
value, label = opt.is_a?(Hash) ? [opt.delete(:value), opt.delete(:label)] : [opt, opt]
hash = {:name => "#{@_object_name}[#{col}]", :value => value, :label => label, :id => "#{control_id(col)}_#{value}" }
hash.merge!(opt) if opt.is_a?(Hash)
hash.merge!(:checked => "checked") if val.to_s == value.to_s
ret << radio_field(hash)
end
ret
Expand All @@ -319,6 +320,7 @@ def radio_field(attrs = {})
def text_area_control(col, attrs = {})
attrs ||= {}
errorify_field(attrs, col)
attrs.merge!(:id => control_id(col))
text_area_field(control_value(col), attrs.merge(:name => control_name(col)))
end

Expand Down Expand Up @@ -568,7 +570,14 @@ def optional_label(attrs = {})
if label
title = label.is_a?(Hash) ? label.delete(:title) : label
named = attrs[:id].blank? ? {} : {:for => attrs[:id]}
label(title, '', label.is_a?(Hash) ? label.merge(named) : named) + yield
align = label.delete(:align) if label.is_a?(Hash)
align ||= ['radio', 'checkbox'].include?(attrs[:type].to_s) ? :right : :left
label_tag = label(title, '', label.is_a?(Hash) ? label.merge(named) : named)
if align && align.to_sym == :right
yield + label_tag
else
label_tag + yield
end
else
yield
end
Expand Down
98 changes: 84 additions & 14 deletions merb_helpers/spec/merb_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end

it "should provide an additional label tag if the :label option is passed in" do
result = checkbox_field(:label => "LABEL" )
result.should match(/<label>LABEL<\/label><input/)
result.should match(/<input.*><label>LABEL<\/label>/)
res = result.scan(/<[^>]*>/)
res[2].should_not match_tag(:input, :label => "LABEL")
res[0].should_not match_tag(:input, :label => "LABEL")
end
end

Expand Down Expand Up @@ -337,9 +337,9 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end
form_for :obj do
_buffer << checkbox_control(:foo, :label => "LABEL")
end
_buffer.should match( /<label.*>LABEL<\/label><input/ )
_buffer.should match( /<input.*><label.*>LABEL<\/label>/ )
res = _buffer.scan(/<[^>]*>/)
res[2].should_not match_tag(:input, :label => "LABEL")
res[0].should_not match_tag(:input, :label => "LABEL")
end

it "should not errorify the field for a new object" do
Expand Down Expand Up @@ -425,14 +425,17 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end

describe "radio button (basic)" do
it "should should return a basic radio button based on the values passed in" do
radio_field(:name => "foo", :value => "bar").should match_tag(:input, :type => "radio", :name => "foo", :value => "bar")
radio_field(:name => "foo", :value => "bar", :id => "baz").should match_tag(:input, :type => "radio", :name => "foo", :value => "bar", :id => "baz")
end

it "should provide an additional label tag if the :label option is passed in" do
result = radio_field(:name => "foo", :value => "bar", :label => "LABEL")
result.should match(/<label.*>LABEL<\/label><input/)
# result.should match(/<label.*>LABEL<\/label><input/)
# res = result.scan(/<[^>]*>/)
# res[2].should_not match_tag(:input, :label => "LABEL")
result.should match(/<input.*><label.*>LABEL<\/label>/)
res = result.scan(/<[^>]*>/)
res[2].should_not match_tag(:input, :label => "LABEL")
res[0].should_not match_tag(:input, :label => "LABEL")
end

end
Expand All @@ -443,19 +446,52 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end
it "should return a group of radio buttons" do
form_for :obj do
radio = radio_group_control(:foo, [:foowee, :baree]).scan(/<[^>]*>/)
radio[2].should match_tag(:input, :type => "radio", :name => "fake_model[foo]", :value => "foowee", :selected => "selected")
radio[5].should match_tag(:input, :type => "radio", :name => "fake_model[foo]", :value => "baree")
radio[3].should not_match_tag(:selected => "selected")
radio[0].should match_tag(:input, :type => "radio", :name => "fake_model[foo]", :value => "foowee", :checked => "checked")
radio[3].should match_tag(:input, :type => "radio", :name => "fake_model[foo]", :value => "baree")
radio[4].should not_match_tag(:checked => "checked")
end
end

it "should provide an additional label tag if the :label option is passed in" do
it "should provide an additional label tag for each option in array-based options" do
form_for :obj do
radio = radio_group_control(:foo, [:foowee, :baree])
radio.scan( /<label.*?>(foowee|baree)<\/label><input/ ).size.should == 2
radio.scan( /<input.*?><label.*?>(foowee|baree)<\/label>/ ).size.should == 2
radio = radio.scan(/<[^>]*>/)
radio[0].should_not match_tag(:input, :label => "LABEL")
radio[3].should_not match_tag(:input, :label => "LABEL")
end
end

it "should accept array of hashes as options" do
form_for :obj do
radio = radio_group_control(:foo, [{:value => 5, :label => "Five"}, {:value => 'bar', :label => 'Bar', :id => 'bar_id'}])
radio.scan( /<input.*?><label.*?>(Five|Bar)<\/label>/ ).size.should == 2
radio = radio.scan(/<[^>]*>/)
radio[1].should_not match_tag(:input, :label => "LABEL")
radio[4].should_not match_tag(:input, :label => "LABEL")
radio.size.should == 6
radio[0].should match_tag(:input, :value => 5)
radio[1].should match_tag(:label)
radio[2].should match_tag('/label')
radio[3].should match_tag(:input, :value => 'bar', :id => 'bar_id')
radio[4].should match_tag(:label, :for => 'bar_id')
radio[5].should match_tag('/label')
end
end

it "should provide autogenerated id for inputs" do
form_for :obj do
[ radio_group_control(:foo, [:bar]), radio_group_control(:foo, [{:value => 'bar', :label => 'Bar'}]) ].each do |radio|
radio = radio.scan(/<[^>]*>/)
radio[0].should match_tag(:input, :id => 'fake_model_foo_bar')
radio[1].should match_tag(:label, :for => 'fake_model_foo_bar')
end
end
end

it "should override autogenerated id for inputs with hash-given id" do
form_for :obj do
radio = radio_group_control(:foo, [{:value => 'bar', :label => 'Bar', :id => 'bar_id'}]).scan(/<[^>]*>/)
radio[0].should match_tag(:input, :id => 'bar_id')
radio[1].should match_tag(:label, :for => 'bar_id')
end
end
end
Expand All @@ -481,6 +517,16 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end
end
end

describe "text area (data bound)" do
it_should_behave_like "FakeBufferConsumer"

it "should provide :id attribute" do
form_for :obj do
text_area_control( :foo ).should match_tag(:textarea, :id => 'fake_model_foo')
end
end
end

describe "select (data bound)" do

it_should_behave_like "FakeBufferConsumer"
Expand Down Expand Up @@ -693,3 +739,27 @@ def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end
delete_button(:obj).should == delete_button(:obj, @obj)
end
end

describe "optional_label" do
it "should generate label tag if given attrs includes :label" do
(optional_label({}) {''}).should == ''
(optional_label({}) {'foo'}).should == 'foo'
(optional_label({:label => 'foo'}) {''}).should == '<label>foo</label>'
(optional_label({:label => {:title => 'foo', :class => 'bar'}}) {''}).should == '<label class="bar">foo</label>'
end

it "should provide :for attribute for label tag if given attrs include :id" do
(optional_label({:label => 'foo', :id => 'bar'}) {''}).should == '<label for="bar">foo</label>'
end

it "should generate label before yielded content be default" do
(optional_label({:label => 'foo'}) {'bar'}).should == '<label>foo</label>bar'
end

it "should generate label after yielded content if given label hash includes :align => :right" do
(optional_label({:label => {:title => 'foo', :align => :right}}) {'bar'}).should == 'bar<label>foo</label>'
(optional_label({:label => {:title => 'foo', :align => 'right'}}) {'bar'}).should == 'bar<label>foo</label>'
(optional_label({:label => {:title => 'foo', :align => :left}}) {'bar'}).should == '<label>foo</label>bar'
(optional_label({:label => {:title => 'foo', :align => 'foo'}}) {'bar'}).should == '<label>foo</label>bar'
end
end

0 comments on commit 3205959

Please sign in to comment.