From 29057c41f74fc33fd2aa1b3135fd0d75906cc587 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 21 Sep 2010 20:14:51 +1000 Subject: [PATCH] use Person for non-ORM specific specs --- spec/test_model.rb | 7 +- .../extensions/date_time_select_spec.rb | 184 +++++++++--------- .../helper_methods_spec.rb | 2 +- 3 files changed, 102 insertions(+), 91 deletions(-) diff --git a/spec/test_model.rb b/spec/test_model.rb index c456d53..169d6fa 100644 --- a/spec/test_model.rb +++ b/spec/test_model.rb @@ -14,12 +14,17 @@ module TestModel module ClassMethods def define_method_attribute=(attr_name) - generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=new_value ; end", __FILE__, __LINE__) + generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast(new_value); end", __FILE__, __LINE__) end def define_method_attribute(attr_name) generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__) end + + def type_cast(value) + return value unless value.is_a?(String) + value.to_time rescue nil + end end module DynamicMethods diff --git a/spec/validates_timeliness/extensions/date_time_select_spec.rb b/spec/validates_timeliness/extensions/date_time_select_spec.rb index de42627..179c955 100644 --- a/spec/validates_timeliness/extensions/date_time_select_spec.rb +++ b/spec/validates_timeliness/extensions/date_time_select_spec.rb @@ -2,17 +2,20 @@ require 'spec_helper' describe ValidatesTimeliness::Extensions::DateTimeSelect do include ActionView::Helpers::DateHelper + attr_reader :person, :params - attr_reader :employee, :params + before :all do + ValidatesTimeliness.use_plugin_parser = true + end before do - @employee = Employee.new + @person = Person.new @params = {} end describe "datetime_select" do it "should use param values when attribute is nil" do - params["employee"] = { + params["person"] = { "birth_datetime(1i)" => 2009, "birth_datetime(2i)" => 2, "birth_datetime(3i)" => 29, @@ -20,18 +23,18 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_datetime(5i)" => 13, "birth_datetime(6i)" => 14, } - employee.birth_datetime = nil - output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29') - output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14') + person.birth_datetime = nil + output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February') + output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29') + output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') end it "should override object values and use params if present" do - params["employee"] = { + params["person"] = { "birth_datetime(1i)" => 2009, "birth_datetime(2i)" => 2, "birth_datetime(3i)" => 29, @@ -39,101 +42,101 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_datetime(5i)" => 13, "birth_datetime(6i)" => 14, } - employee.birth_datetime = "2010-01-01 15:16:17" - output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29') - output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14') + person.birth_datetime = "2010-01-01 15:16:17" + output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February') + output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29') + output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') end it "should use attribute values from object if no params" do - employee.birth_datetime = "2009-01-02 12:13:14" - output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2') - output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14') + person.birth_datetime = "2009-01-02 12:13:14" + output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January') + output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2') + output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') end it "should use attribute values if params does not contain attribute params" do - employee.birth_datetime = "2009-01-02 12:13:14" - params["employee"] = { } - output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2') - output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14') + person.birth_datetime = "2009-01-02 12:13:14" + params["person"] = { } + output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January') + output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2') + output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') end it "should not select values when attribute value is nil and has no param values" do - employee.birth_datetime = nil - output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]') + person.birth_datetime = nil + output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + output.should_not have_tag('select[id=person_birth_datetime_1i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_datetime_2i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_datetime_3i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_datetime_4i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_datetime_5i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_datetime_6i] option[selected=selected]') end end describe "date_select" do it "should use param values when attribute is nil" do - params["employee"] = { + params["person"] = { "birth_date(1i)" => 2009, "birth_date(2i)" => 2, "birth_date(3i)" => 29, } - employee.birth_date = nil - output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29') + person.birth_date = nil + output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February') + output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29') end it "should override object values and use params if present" do - params["employee"] = { + params["person"] = { "birth_date(1i)" => 2009, "birth_date(2i)" => 2, "birth_date(3i)" => 29, } - employee.birth_date = "2009-03-01" - output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29') + person.birth_date = "2009-03-01" + output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February') + output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29') end it "should select attribute values from object if no params" do - employee.birth_date = "2009-01-02" - output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2') + person.birth_date = "2009-01-02" + output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January') + output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2') end it "should select attribute values if params does not contain attribute params" do - employee.birth_date = "2009-01-02" - params["employee"] = { } - output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2') + person.birth_date = "2009-01-02" + params["person"] = { } + output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') + output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January') + output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2') end it "should not select values when attribute value is nil and has no param values" do - employee.birth_date = nil - output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=employee_birth_date_1i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_date_2i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_date_3i] option[selected=selected]') + person.birth_date = nil + output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]') end end @@ -143,7 +146,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do end it "should use param values when attribute is nil" do - params["employee"] = { + params["person"] = { "birth_time(1i)" => 2000, "birth_time(2i)" => 1, "birth_time(3i)" => 1, @@ -151,28 +154,31 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_time(5i)" => 13, "birth_time(6i)" => 14, } - employee.birth_time = nil - output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14') + person.birth_time = nil + output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14') end it "should select attribute values from object if no params" do - employee.birth_time = "2000-01-01 12:13:14" - output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12') - output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13') - output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14') + person.birth_time = "2000-01-01 12:13:14" + output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12') + output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13') + output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14') end it "should not select values when attribute value is nil and has no param values" do - employee.birth_time = nil - output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=employee_birth_time_4i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_time_5i] option[selected=selected]') - output.should_not have_tag('select[id=employee_birth_time_6i] option[selected=selected]') + person.birth_time = nil + output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]') + output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]') end end + after :all do + ValidatesTimeliness.use_plugin_parser = false + end end diff --git a/spec/validates_timeliness/helper_methods_spec.rb b/spec/validates_timeliness/helper_methods_spec.rb index dd77692..a6f089a 100644 --- a/spec/validates_timeliness/helper_methods_spec.rb +++ b/spec/validates_timeliness/helper_methods_spec.rb @@ -14,7 +14,7 @@ describe ValidatesTimeliness::HelperMethods do end it 'should validate instance when validation method called' do - r = Employee.new + r = Person.new r.validates_date :birth_date r.errors[:birth_date].should_not be_empty end