require 'test/unit' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper' # require File.dirname(__FILE__) + '/../../lib/action_view/helpers/active_record_helper' class ActiveRecordHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper include ActionView::Helpers::ActiveRecordHelper include ActionView::Helpers::TextHelper include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper include ActionView::Helpers::FormTagHelper silence_warnings do Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) Post.class_eval do alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) end User = Struct.new("User", :email) User.class_eval do alias_method :email_before_type_cast, :email unless respond_to?(:email_before_type_cast) end Column = Struct.new("Column", :type, :name, :human_name) end def setup_post @post = Post.new def @post.errors Class.new { def on(field) field == "author_name" || field == "body" end def empty?() false end def count() 1 end def full_messages() [ "Author name can't be empty" ] end }.new end def @post.new_record?() true end def @post.to_param() nil end def @post.column_for_attribute(attr_name) Post.content_columns.select { |column| column.name == attr_name }.first end silence_warnings do def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end end @post.title = "Hello World" @post.author_name = "" @post.body = "Back to the hill and over it again!" @post.secret = 1 @post.written_on = Date.new(2004, 6, 15) end def setup_user @user = User.new def @user.errors Class.new { def on(field) field == "email" end def empty?() false end def count() 1 end def full_messages() [ "User email can't be empty" ] end }.new end def @user.new_record?() true end def @user.to_param() nil end def @user.column_for_attribute(attr_name) User.content_columns.select { |column| column.name == attr_name }.first end silence_warnings do def User.content_columns() [ Column.new(:string, "email", "Email") ] end end @user.email = "" end def setup setup_post setup_user @controller = Object.new def @controller.url_for(options, *parameters_for_method_reference) options = options.symbolize_keys [options[:action], options[:id].to_param].compact.join('/') end end def test_generic_input_tag assert_dom_equal( %(), input("post", "title") ) end def test_text_area_with_errors assert_dom_equal( %(
), text_area("post", "body") ) end def test_text_field_with_errors assert_dom_equal( %(
), text_field("post", "author_name") ) end def test_form_with_string assert_dom_equal( %(


\n


), form("post") ) silence_warnings do class << @post def new_record?() false end def to_param() id end def id() 1 end end end assert_dom_equal( %(


\n


), form("post") ) end def test_form_with_date silence_warnings do def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end end assert_dom_equal( %(


\n\n\n

), form("post") ) end def test_form_with_datetime silence_warnings do def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end end @post.written_on = Time.gm(2004, 6, 15, 16, 30) assert_dom_equal( %(


\n\n\n — \n : \n

), form("post") ) end def test_error_for_block assert_dom_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post") assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1") assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1") assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1") end def test_error_messages_for_handles_nil assert_equal "", error_messages_for("notthere") end def test_error_messages_for_many_objects assert_dom_equal %(

2 errors prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", "user") # reverse the order, error order changes and so does the title assert_dom_equal %(

2 errors prohibited this user from being saved

There were problems with the following fields:

), error_messages_for("user", "post") # add the default to put post back in the title assert_dom_equal %(

2 errors prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("user", "post", :object_name => "post") # symbols work as well assert_dom_equal %(

2 errors prohibited this post from being saved

There were problems with the following fields:

), error_messages_for(:user, :post, :object_name => :post) # any default works too assert_dom_equal %(

2 errors prohibited this monkey from being saved

There were problems with the following fields:

), error_messages_for(:user, :post, :object_name => "monkey") end def test_form_with_string_multipart assert_dom_equal( %(


\n


), form("post", :multipart => true) ) end end