merb-helpers-1.1.3/ 0000755 0001750 0001750 00000000000 11757204510 013407 5 ustar tfheen tfheen merb-helpers-1.1.3/spec/ 0000755 0001750 0001750 00000000000 11757204510 014341 5 ustar tfheen tfheen merb-helpers-1.1.3/spec/numeric_extlib_spec.rb 0000644 0001750 0001750 00000010636 11757204510 020717 0 ustar tfheen tfheen # -*- encoding: utf-8 -*-
require 'spec_helper'
describe "Numeric helpers" do
before :each do
@controller = NumericExtSpecs.new(Merb::Request.new({}))
end
describe "with_delimiter" do
before(:each) do
@number = 12345678
end
it "should use the default formatting for numbers" do
@number.with_delimiter.should == "12,345,678"
@number.with_delimiter.should == @number.with_delimiter(:us)
end
it "should support passing another format" do
@number.with_delimiter(:fr).should == "12 345 678"
end
it "should support passing overwriting options" do
@number.with_delimiter(:fr, :delimiter => ',').should == "12,345,678"
12345678.9.with_delimiter(:fr, :separator => ' et ').should == "12 345 678 et 9"
end
end
describe "with_precision" do
it "should use a default precision" do
111.2345.with_precision.should == "111.235"
end
it "should support other precision formats" do
111.2345.with_precision(:uk).should == "111.235"
end
it "should support overwriting precision options" do
111.2345.with_precision(:uk, :precision => 1).should == "111.2"
1234.567.with_precision(:us, :precision => 1, :separator => ',', :delimiter => '-').should == "1-234,6"
end
end
describe "to_concurrency" do
before(:each) do
@number = 1234567890.50
end
it "should use the US$ by default" do
@number.to_currency.should == "$1,234,567,890.50"
@number.to_currency.should == @number.to_currency(:us)
@number.to_currency.should == @number.to_currency(:default)
result = @controller.render :to_concurrency_default
result.should == "$1,234,567,890.50"
end
it "should use the precision settings of the format" do
1234567890.506.to_currency(:us).should == "$1,234,567,890.51"
end
it "should support other formats" do
@number.to_currency(:uk).should == "£1,234,567,890.50"
@number.to_currency(:fr).should == "1 234 567 890,50€"
end
it "should support overwriting options" do
1234567890.506.to_currency(:us, :precision => 1).should == "$1,234,567,890.5"
1234567890.516.to_currency(:us, :unit => "€").should == "€1,234,567,890.52"
1234567890.506.to_currency(:us, :precision => 3, :unit => "€").should == "€1,234,567,890.506"
1234567890.506.to_currency(:aus, :unit => "$AUD", :format => '%n %u').should == "1,234,567,890.51 $AUD"
end
end
describe "Numeric::Transformer formats" do
it "should be able to add a new format" do
Numeric::Transformer.default_format.should be_instance_of(Hash)
end
it "should be able to change the default format" do
original_default_format = Numeric::Transformer.default_format
original_default_format[:currency][:unit].should == "$"
Numeric::Transformer.change_default_format(:fr)
Numeric::Transformer.default_format.should_not == original_default_format
Numeric::Transformer.default_format[:currency][:unit].should == "€"
end
it "should be able to add a format" do
merb_format = {:merb =>
{ :number => {
:precision => 3,
:delimiter => ' ',
:separator => ','
},
:currency => {
:unit => 'Merbollars',
:format => '%n %u',
:precision => 2
}
}
}
Numeric::Transformer.add_format(merb_format)
Numeric::Transformer.change_default_format(:merb)
12345.to_currency.should == "12 345,00 Merbollars"
end
end
describe "other misc numeric helpers" do
describe "two_digits" do
it "should convert a numeric value in a 2 digit string (prepend with a zero if needed)" do
7.two_digits.should == "07"
result = @controller.render :two_digits
result.should == "07"
end
end
describe "minutes_to_hours" do
it "should convert a number of minutes into an hour representation" do
result = @controller.render :minutes_to_hours
result.should == "05:15"
end
end
end
end
merb-helpers-1.1.3/spec/spec.opts 0000644 0001750 0001750 00000000032 11757204510 016175 0 ustar tfheen tfheen --format specdoc
--colour
merb-helpers-1.1.3/spec/merb_helpers_text_spec.rb 0000644 0001750 0001750 00000004140 11757204510 021412 0 ustar tfheen tfheen require 'spec_helper'
describe "cycle" do
include Merb::Helpers::Text
it "do basic cycling" do
cycle("one", 2, "3").should == 'one'
cycle("one", 2, "3").should == '2'
cycle("one", 2, "3").should == '3'
cycle("one", 2, "3").should == 'one'
cycle("one", 2, "3").should == '2'
cycle("one", 2, "3").should == '3'
end
it "should reset with new values" do
cycle("even", "odd").should == 'even'
cycle("even", "odd").should == 'odd'
cycle("even", "odd").should == 'even'
cycle("even", "odd").should == 'odd'
cycle(1, 2, 3).should == '1'
cycle(1, 2, 3).should == '2'
cycle(1, 2, 3).should == '3'
cycle(1, 2, 3).should == '1'
end
it "should support named cycles" do
cycle(1, 2, 3, :name => "numbers").should == "1"
cycle("red", "blue", :name => "colors").should == "red"
cycle(1, 2, 3, :name => "numbers").should == "2"
cycle("red", "blue", :name => "colors").should == "blue"
cycle(1, 2, 3, :name => "numbers").should == "3"
cycle("red", "blue", :name => "colors").should == "red"
end
it "should use a named cycle called 'default' by default" do
cycle(1, 2, 3).should == "1"
cycle(1, 2, 3, :name => "default").should == "2"
cycle(1, 2, 3).should == "3"
end
it "should be able to be reset" do
cycle(1, 2, 3).should == "1"
cycle(1, 2, 3).should == "2"
reset_cycle
cycle(1, 2, 3).should == "1"
end
it "should be able to reset a named cycle" do
cycle(1, 2, 3, :name => "numbers").should == "1"
cycle("red", "blue", :name => "colors").should == "red"
reset_cycle("numbers")
cycle(1, 2, 3, :name => "numbers").should == "1"
cycle("red", "blue", :name => "colors").should == "blue"
cycle(1, 2, 3, :name => "numbers").should == "2"
cycle("red", "blue", :name => "colors").should == "red"
end
it "should work with things other than strings" do
class Red; def to_s; 'red'; end; end;
class Blue; def to_s; 'blue'; end; end;
red = Red.new
blue = Blue.new
cycle(red, blue).should == "red"
cycle(red, blue).should == "blue"
cycle(red, blue).should == "red"
end
end
merb-helpers-1.1.3/spec/time_dsl_spec.rb 0000644 0001750 0001750 00000002023 11757204510 017475 0 ustar tfheen tfheen require 'spec_helper'
describe "TimeDSL" do
it "Should do second/seconds" do
10.seconds.should == 10
1.second.should == 1
end
it "Should do minute/minutes" do
22.minutes.should == 22 * 60
1.minute.should == 60
end
it "Should do hour/hours" do
24.hours.should == 24 * 3600
1.hour.should == 3600
end
it "Should do day/days" do
7.days.should == 7 * 24 * 3600
1.day.should == 24 * 3600
end
it "Should do month/months" do
9.months.should == 9 * 30 * 24 * 3600
1.month.should == 30 * 24 * 3600
end
it "Should do year/years" do
3.years.should == 3 * 364.25 * 24 * 3600
1.year.should == 364.25 * 24 * 3600
end
it "Should do ago/until" do
5.seconds.ago.should be_close(Time.now - 5, 0.5)
8.minutes.until(3.minute.from_now).should be_close(3.minutes.from_now - 8 * 60, 0.5)
end
it "Should do from_now/since" do
3.seconds.from_now.should be_close(Time.now + 3, 0.5)
2.minutes.since(2.minutes.ago).should be_close(2.minutes.ago + 2 * 60, 0.5)
end
end
merb-helpers-1.1.3/spec/merb_helpers_date_time_spec.rb 0000644 0001750 0001750 00000021445 11757204510 022370 0 ustar tfheen tfheen require 'spec_helper'
describe "relative_date" do
include Merb::Helpers::DateAndTime
before :each do
Time.stub!(:now).and_return(Time.utc(2007, 6, 1, 11))
@controller = RelativeDateSpecs.new(Merb::Request.new({}))
end
it "Should show today" do
relative_date(Time.now.utc).should == "today"
result = @controller.render :relative_today
result.should == "today"
end
it "Should show yesterday" do
relative_date(1.day.ago.utc).should == 'yesterday'
result = @controller.render :relative_yesterday
result.should == "yesterday"
end
it "Should show tomorrow" do
relative_date(1.day.from_now.utc).should == 'tomorrow'
@controller.render(:relative_tomorrow).should == 'tomorrow'
end
it "Should show date with year" do
relative_date(Time.utc(2005, 11, 15)).should == 'Nov 15th, 2005'
@controller.render(:relative_date_with_year).should == 'Nov 15th, 2005'
end
it "Should show date" do
relative_date(Time.utc(2007, 11, 15)).should == 'Nov 15th'
@controller.render(:relative_date_without_year).should == 'Nov 15th'
end
end
describe "relative_date_span" do
include Merb::Helpers::DateAndTime
before :each do
Time.stub!(:now).and_return(Time.utc(2007, 6, 1, 11))
@controller = RelativeDateSpanSpecs.new(Merb::Request.new({}))
end
it "Should show date span on the same day" do
relative_date_span([Time.utc(2007, 11, 15), Time.utc(2007, 11, 15)]).should == 'Nov 15th'
@controller.render(:date_span_on_same_day).should == 'Nov 15th'
end
it "Should show date span on the same day on different year" do
relative_date_span([Time.utc(2006, 11, 15), Time.utc(2006, 11, 15)]).should == 'Nov 15th, 2006'
@controller.render(:date_span_on_same_day_on_different_year).should == 'Nov 15th, 2006'
end
it "Should show date span on the same month" do
relative_date_span([Time.utc(2007, 11, 15), Time.utc(2007, 11, 16)]).should == 'Nov 15th - 16th'
relative_date_span([Time.utc(2007, 11, 16), Time.utc(2007, 11, 15)]).should == 'Nov 15th - 16th'
end
it "Should show date span on the same month on different year" do
relative_date_span([Time.utc(2006, 11, 15), Time.utc(2006, 11, 16)]).should == 'Nov 15th - 16th, 2006'
relative_date_span([Time.utc(2006, 11, 16), Time.utc(2006, 11, 15)]).should == 'Nov 15th - 16th, 2006'
end
it "Should show date span on the different month" do
relative_date_span([Time.utc(2007, 11, 15), Time.utc(2007, 12, 16)]).should == 'Nov 15th - Dec 16th'
relative_date_span([Time.utc(2007, 12, 16), Time.utc(2007, 11, 15)]).should == 'Nov 15th - Dec 16th'
end
it "Should show date span on the different month on different year" do
relative_date_span([Time.utc(2006, 11, 15), Time.utc(2006, 12, 16)]).should == 'Nov 15th - Dec 16th, 2006'
relative_date_span([Time.utc(2006, 12, 16), Time.utc(2006, 11, 15)]).should == 'Nov 15th - Dec 16th, 2006'
end
it "Should show date span on the different year" do
relative_date_span([Time.utc(2006, 11, 15), Time.utc(2007, 12, 16)]).should == 'Nov 15th, 2006 - Dec 16th, 2007'
relative_date_span([Time.utc(2007, 12, 16), Time.utc(2006, 11, 15)]).should == 'Nov 15th, 2006 - Dec 16th, 2007'
end
end
describe "relative_time_span" do
include Merb::Helpers::DateAndTime
before :each do
Time.stub!(:now).and_return(Time.utc(2007, 6, 1, 11))
end
# Time, Single Date
it "Should show time span on the same day with same time" do
relative_time_span([Time.utc(2007, 11, 15, 17, 00, 00)]).should == '5:00 PM Nov 15th'
end
it "Should show time span on the same day with same time on different year" do
relative_time_span([Time.utc(2006, 11, 15, 17, 0), Time.utc(2006, 11, 15, 17, 0)]).should == '5:00 PM Nov 15th, 2006'
end
it "Should show time span on the same day with different times in same half of day" do
relative_time_span([Time.utc(2007, 11, 15, 10), Time.utc(2007, 11, 15, 11, 0)]).should == '10:00 - 11:00 AM Nov 15th'
end
it "Should show time span on the same day with different times in different half of day" do
relative_time_span([Time.utc(2007, 11, 15, 10, 0), Time.utc(2007, 11, 15, 14, 0)]).should == '10:00 AM - 2:00 PM Nov 15th'
end
it "Should show time span on the same day with different times in different half of day in different year" do
relative_time_span([Time.utc(2006, 11, 15, 10, 0), Time.utc(2006, 11, 15, 14, 0)]).should == '10:00 AM - 2:00 PM Nov 15th, 2006'
end
it "Should show time span on different days in same year" do
relative_time_span([Time.utc(2006, 11, 15, 10, 0), Time.utc(2006, 12, 16, 14, 0)]).should == '10:00 AM Nov 15th - 2:00 PM Dec 16th, 2006'
end
it "Should show time span on different days in different years" do
relative_time_span([Time.utc(2006, 11, 15, 10, 0), Time.utc(2007, 12, 16, 14, 0)]).should == '10:00 AM Nov 15th, 2006 - 2:00 PM Dec 16th, 2007'
end
it "Should show time span on different days in current year" do
relative_time_span([Time.utc(2007, 11, 15, 10, 0), Time.utc(2007, 12, 16, 14, 0)]).should == '10:00 AM Nov 15th - 2:00 PM Dec 16th'
end
end
describe "time_lost_in_words" do
include Merb::Helpers::DateAndTime
it "Should show seconds" do
time_lost_in_words(Time.now, Time.now, true).should == "less than 5 seconds"
end
it "Should not show seconds" do
time_lost_in_words(Time.now).should == "less than a minute"
end
it "Should do minutes" do
time_lost_in_words(2.minutes.ago).should == "2 minutes"
end
it "Should do hour" do
time_lost_in_words(50.minutes.ago).should == "about 1 hour"
end
it "Should do hours" do
time_lost_in_words(2.hours.ago).should == "about 2 hours"
end
it "Should do day" do
time_lost_in_words(1.day.ago).should == "1 day"
end
it "Should do days" do
time_lost_in_words(5.days.ago).should == "5 days"
end
it "Should do month" do
time_lost_in_words(1.month.ago).should == "about 1 month"
end
it "Should do months" do
time_lost_in_words(5.months.ago).should == "5 months"
end
it "Should do year" do
time_lost_in_words(1.2.years.ago).should == "about 1 year"
end
it "Should do years" do
time_lost_in_words(5.5.years.ago).should == "over 5 years"
end
end
describe "prettier_time" do
include Merb::Helpers::DateAndTime
# prettier time"
it "Should not show leading zero in hour" do
prettier_time(Time.utc(2007, 11, 15, 14, 0)).should == '2:00 PM'
end
it "Should convert to 12 hour time" do
prettier_time(Time.utc(2007, 11, 15, 2, 0)).should == '2:00 AM'
end
it "Should handle midnight correctly" do
prettier_time(Time.utc(2007, 11, 15, 0, 0)).should == '12:00 AM'
end
end
shared_examples_for "Date, DateTime, Time formatting" do
before(:each) do
Date.reset_formats
end
it "should list the available formats" do
Date.formats.should be_an_instance_of(Hash)
Date.formats.keys.length.should > 1
end
it "should support to be db formatted" do
@date.formatted(:db).should =~ /^2007-11-02 \d{2}:\d{2}:\d{2}$/
end
it "should support to be time formatted" do
@date.formatted(:time).should == "00:00"
end
it "should support to be short formatted" do
@date.formatted(:short).should == "02 Nov 00:00"
end
it "should support to be date formatted" do
@date.formatted(:date).should == "2007-11-02"
end
it "should support to be long formatted" do
@date.formatted(:long).should == "November 02, 2007 00:00"
end
it "should support a new date format" do
@date.formatted(:matt).should == @date.to_s
Date.add_format(:matt, "%H:%M:%S %Y-%m-%d")
@date.formatted(:matt).should == "00:00:00 2007-11-02"
end
end
describe "Date" do
before :each do
@date = Date.new(2007, 11, 02)
end
it "Should do to_time conversion and return a Time class" do
@date.is_a?(Date)
@date.to_time.is_a?(Time)
end
it "Should do to_time conversion to utc by default" do
if RUBY_VERSION < "1.9"
@date.to_time.to_s.should == 'Fri Nov 02 00:00:00 UTC 2007'
else
@date.to_time.to_s.should == '2007-11-02 00:00:00 UTC'
end
end
it "Should do to_time conversion to utc when param :utc is given" do
if RUBY_VERSION < "1.9"
@date.to_time(:utc).to_s.should == 'Fri Nov 02 00:00:00 UTC 2007'
else
@date.to_time(:utc).to_s.should == '2007-11-02 00:00:00 UTC'
end
end
it "Should do to_time conversion to local time when param :local is given" do
pending("Needs to have the call to figure out the local time stubbed so this test will work no matter what your local TZ is.")
@date.to_time(:local).to_s.should == 'Fri Nov 02 00:00:00 -0500 2007'
end
it "Should return itself when to_date is called" do
@date.to_date.should == @date
end
it_should_behave_like "Date, DateTime, Time formatting"
end
describe "DateTime" do
before(:each) do
@date = DateTime.new(2007, 11, 02)
end
it_should_behave_like "Date, DateTime, Time formatting"
end
merb-helpers-1.1.3/spec/merb_helpers_form_spec.rb 0000644 0001750 0001750 00000140433 11757204510 021377 0 ustar tfheen tfheen require 'spec_helper'
# Quick rundown of how these specs work
# please read before hacking on this plugin
#
# helpers must be tested through then entire stack
# what that means is that each spec must
# send a request to a controller and render a template
#
# Start by creating a spec controller subclassing SpecController
# which itself is a subclass of Merb::Controller
# specs_controller.rb (available at spec/fixture/app/controllers/specs_controller.rb)
# defines SpecController.
# Create a new controller in the spec/fixture/app/controllers/ if you are adding a new helper
#
# To test your helper, start by initializing a controller
#
# @controller = CustomHelperSpecs.new(Merb::Request.new({}))
#
# Note that we are sending a real request to the controller, feel free to use the request as needed
#
# You might need to access real objects in your views
# you can do that by setting them up in the controller
#
# @obj = FakeModel.new # FaKeModel is defined in spec/fixture/models/first_generic_fake_model.rb check it out!
# @controller.instance_variable_set(:@obj, @obj)
#
# To test a helper, you need to render a view:
#
# result = @controller.render :view_name
#
# Of course, you need to create a view:
# spec/fixture/app/views/custom_helper_specs/view_name.html.erb
# in the view, call the helper you want to test
#
# You can now test the helper in the view:
# result.should match_tag(:form, :method => "post")
#
Merb::Plugins.config[:helpers] = {
:default_builder => Merb::Helpers::Form::Builder::FormWithErrors
}
describe "error_messages_for" do
before :each do
@c = Application.new({})
@dm_obj = Object.new
@sq_obj = Object.new
@dm_errors = [["foo", "bar"],["baz","bat"]]
@sq_errors = {:foo => ["bar"],:baz => ["bat"]}
@dm_obj.stub!(:errors).and_return(@dm_errors)
@dm_obj.stub!(:new_record?).and_return(false)
@sq_obj.stub!(:errors).and_return(@sq_errors)
@sq_obj.stub!(:new_record?).and_return(false)
end
it "should build default error messages for AR-like models" do
errs = @c.error_messages_for(@dm_obj)
errs.should include("
Form submission failed because of 2 problems
")
errs.should include("
foo bar
")
errs.should include("
baz bat
")
end
it "should build default error messages for Sequel-like models" do
errs = @c.error_messages_for(@sq_obj)
errs.should include("
Form submission failed because of 2 problems
")
errs.should include("
foo bar
")
errs.should include("
baz bat
")
end
# it "should build default error messages for symbol" do
# errs = error_messages_for(:obj)
# errs.should include("
Form submittal failed because of 2 problems
")
# errs.should include("
foo bar
")
# errs.should include("
baz bat
")
# end
it "should accept a custom HTML class" do
errs = @c.error_messages_for(@dm_obj, :error_class => "foo")
errs.should include("
")
end
it "should accept a custom header block" do
errs = @c.error_messages_for(@dm_obj, :header => "
Failure: %s issue%s
")
errs.should include("
Failure: 2 issues
")
end
# it "should put the error messages inside a form if :before is false" do
# ret = @c.form_for @dm_obj do
# _buffer << error_messages
# end
# ret.should =~ /\A\s*/
# end
end
describe "form" do
before :each do
@c = FormSpecs.new(Merb::Request.new({}))
end
describe "when _default_builder is Merb::Helpers::Form::Builder::ResourcefulFormWithErrors" do
before(:each) do
@obj = FakeModel2.new
@c.instance_variable_set(:@obj, @obj)
end
it "should not explode when #form is called" do
r = @c.render :resourceful_form
pending
#r.should =~ /action="fake_model2\/#{@obj.id}"/
end
end
it "should use the post method by default" do
ret = @c.render(:post_by_default)
ret.should have_selector("form[method=post]")
ret.should include("CONTENT")
end
it "should use the get method if set" do
ret = @c.render(:get_if_set)
ret.should have_selector("form[method=get]")
end
it "should fake out the put method if set" do
ret = @c.render(:fake_put_if_set)
ret.should have_selector("form[method=post]")
ret.should have_selector("input[type=hidden][name=_method][value=put][class=hidden]")
end
it "should fake out the delete method if set" do
ret = @c.render(:fake_delete_if_set)
ret.should have_selector("form[method=post]")
ret.should have_selector("input[type=hidden][name=_method][value=delete][class=hidden]")
end
# TODO: Why is this required?
# ---------------------------
#
# it "should silently set method to post if an unsupported method is used" do
# form_tag :method => :dodgy do
# _buffer << "CONTENT"
# end
# _buffer.should match_tag(:form, :method => "post")
# _buffer.should_not match_tag(:input, :type => "hidden", :name => "_method", :value => "dodgy")
# end
it "should take create a form" do
ret = @c.render(:create_a_form)
ret.should have_selector("form[action=foo][method=post]")
ret.should include("Hello")
end
it "should set a form to be multipart" do
ret = @c.render(:create_a_multipart_form)
ret.should have_selector("form[action=foo][method=post][enctype='multipart/form-data']")
ret.should include("CONTENT")
end
end
describe "form_for" do
before :each do
@c = FormForSpecs.new(Merb::Request.new({}))
@c.instance_variable_set(:@obj, FakeModel.new)
end
it "should wrap the contents in a form tag" do
form = @c.render :basic
form.should have_selector("form[method=post]")
form.should have_selector("input[type=hidden][value=put][name=_method]")
end
it "should set the method to post be default" do
new_fake_model = FakeModel2.new
@c.instance_variable_set(:@obj, new_fake_model)
form = @c.render :basic
form.should have_selector("form[method=post]")
form.should_not have_selector("input[type=hidden][name=_method]")
end
it "should use POST if the object passed in is nil" do
@c.instance_variable_set(:@obj, nil)
form = @c.render :advanced
form.should have_selector("form[method=post]")
form.should_not have_selector("input[type=hidden][value=put][name=_method]")
end
it "should support PUT if the object passed in is not a new_record? via a hidden field" do
form = @c.render :basic
form.should have_selector("form[method=post]")
form.should have_selector("input[type=hidden][value=put][name=_method]")
end
end
describe "fields_for" do
before :each do
@c = FieldsForSpecs.new(Merb::Request.new({}))
@c.instance_variable_set(:@obj, FakeModel.new)
end
it "should dump the contents in the context of the object" do
r = @c.render :basic
r.should have_selector("input[type=text][value=foowee]")
end
it "should be able to modify the context midstream" do
@c.instance_variable_set(:@obj2, FakeModel2.new)
r = @c.render :midstream
r.should have_selector("input[type=text][value=foowee]")
r.should have_selector("input[name='fake_model2[foo]'][type=text][value=foowee2]")
end
it "should handle an explicit nil attribute" do
r = @c.render :nil
r.should have_selector("input[name='fake_model[foo]'][value=foowee][type=text]")
end
it "should pass context back to the old object after exiting block" do
@c.instance_variable_set(:@obj2, FakeModel2.new)
r = @c.render :midstream
r.should have_selector("input[id=fake_model_foo][name='fake_model[foo]'][type=text][extra=true]")
end
it "should be able to handle namespaced models by setting name attribute to concrete class name" do
@c.instance_variable_set(:@obj2, MyNamespace::NamespacedFakeModel.new)
r = @c.render :midstream
r.should have_selector("input[type=text][value=foowee]")
r.should have_selector("input[name='namespaced_fake_model[foo]'][type=text][value=named_foo]")
end
end
describe "text_field" do
before :each do
@c = TextFieldSpecs.new(Merb::Request.new({}))
end
it "should return a basic text field based on the values passed in" do
r = @c.render :basic
r.should have_selector("input[type=text][id=foo][name=foo][value=bar]")
end
it "should update an existing :class with a new class" do
r = @c.render :class
r.should == ""
end
it "should be disabled if :disabled => true is passed in" do
r = @c.render :disabled
r.should have_selector("input[type=text][disabled=disabled]")
end
it "should provide an additional label tag if the :label option is passed in as a hash" do
r = @c.render :label
r.should have_selector("label[class=cool][for=foo]:contains('LABEL')")
end
it "should allow a symbolized name" do
r = @c.render :symbolized_name
r.should have_selector("input[type=text][name=foo][value=bar]")
r.should have_selector("label[for=foo]:contains('LABEL')")
end
end
describe "bound_text_field" do
before :each do
@c = BoundTextFieldSpecs.new(Merb::Request.new({}))
@c.instance_variable_set(:@obj, FakeModel.new)
end
it "should take a string object and return a useful text control" do
r = @c.render :basic
r.should have_selector("input[type=text][id=fake_model_foo][name='fake_model[foo]'][value=foowee]")
end
it "should take a namespaced model and return correct fieldname" do
@c.instance_variable_set(:@obj, MyNamespace::NamespacedFakeModel.new)
r = @c.render :basic
r.should have_selector("input[type=text][id=namespaced_fake_model_foo][name='namespaced_fake_model[foo]'][value=named_foo]")
end
it "should take additional attributes and use them" do
r = @c.render :basic
r.should have_selector("input[type=text][name='fake_model[foo]'][value=foowee][bar='7']")
end
it "should provide an additional label tag if the :label option is passed in" do
form = @c.render :basic
form.should match(/LABEL<\/label> true is passed in" do
r = @c.render :disabled
r.should match_tag(:input, :type => "password", :disabled => "disabled")
end
end
describe "bound_password_field" do
before :each do
@c = BoundPasswordFieldSpecs.new(Merb::Request.new({}))
@obj = FakeModel.new
@c.instance_variable_set(:@obj, @obj)
end
it "should take a string object and return a useful password control, but omit the value" do
r = @c.render :basic
r.should match_tag(:input, :type => "password", :id => "fake_model_foo", :name => "fake_model[foo]")
end
it "should take additional attributes and use them" do
r = @c.render :basic
r.should match_tag(:input, :type => "password", :name => "fake_model[foo]", :bar => "7", :value => @obj.foo)
end
it "should provide an additional label tag if the :label option is passed in" do
r = @c.render :basic
r.should match(/LABEL<\/label> "LABEL")
end
it "should render the label tag with the proper for= atttribute" do
form = @c.render :basic
form.should have_selector("label[for=fake_model_foo]:contains('LABEL')")
end
it "should not errorify the field for a new object" do
r = @c.render :basic
r.should_not match_tag(:input, :class => "error")
end
it "should errorify a field for a model with errors" do
model = mock("model")
model.stub!(:new_record?).and_return(true)
model.stub!(:class).and_return("MyClass")
model.stub!(:foo).and_return("FOO")
errors = mock("errors")
errors.should_receive(:[]).with(:foo).and_return(true)
model.stub!(:errors).and_return(errors)
@c.instance_variable_set(:@obj, model)
r = @c.render :basic
r.should match_tag(:input, :class => "error password")
end
end
describe "check_box" do
before :each do
@c = CheckBoxSpecs.new(Merb::Request.new({}))
end
it "should return a basic checkbox based on the values passed in" do
r = @c.render :basic
r.should match_tag(:input, :class => "checkbox", :id => "foo", :name => "foo", :checked => "checked")
end
it "should provide an additional label tag if the :label option is passed in" do
result = @c.render :label
result.should have_selector("label[for=foo]:contains('LABEL')")
result.should match(/