From da257eb81ba1eab76ef2c1a256916193858418d4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 30 Apr 2007 01:17:56 +0000 Subject: Added the first part of Simply Helpful to core. The rest is pending a clean integartion of polymorphic urls [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6633 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/controller/record_identifier_test.rb | 102 +++++++++++++++++++++ actionpack/test/template/prototype_helper_test.rb | 7 ++ 2 files changed, 109 insertions(+) create mode 100644 actionpack/test/controller/record_identifier_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/record_identifier_test.rb b/actionpack/test/controller/record_identifier_test.rb new file mode 100644 index 0000000000..d805a4a385 --- /dev/null +++ b/actionpack/test/controller/record_identifier_test.rb @@ -0,0 +1,102 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class Post + attr_reader :id + def save; @id = 1 end + def new_record?; @id.nil? end + def name + @id.nil? ? 'new post' : "post ##{@id}" + end + class Nested < Post; end +end + +class Test::Unit::TestCase + protected + def posts_url + 'http://www.example.com/posts' + end + + def post_url(post) + "http://www.example.com/posts/#{post.id}" + end +end + + +class RecordIdentifierTest < Test::Unit::TestCase + include ActionController::RecordIdentifier + + def setup + @klass = Post + @record = @klass.new + @singular = 'post' + @plural = 'posts' + end + + def test_dom_id_with_new_record + assert_equal "new_#{@singular}", dom_id(@record) + end + + def test_dom_id_with_new_record_and_prefix + assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix) + end + + def test_dom_id_with_saved_record + @record.save + assert_equal "#{@singular}_1", dom_id(@record) + end + + def test_dom_id_with_prefix + @record.save + assert_equal "edit_#{@singular}_1", dom_id(@record, :edit) + end + + def test_partial_path + expected = "#{@plural}/#{@singular}" + assert_equal expected, partial_path(@record) + assert_equal expected, partial_path(Post) + end + + def test_dom_class + assert_equal @singular, dom_class(@record) + end + + def test_dom_class_with_prefix + assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix) + end + + def test_singular_class_name + assert_equal @singular, singular_class_name(@record) + end + + def test_singular_class_name_for_class + assert_equal @singular, singular_class_name(@klass) + end + + def test_plural_class_name + assert_equal @plural, plural_class_name(@record) + end + + def test_plural_class_name_for_class + assert_equal @plural, plural_class_name(@klass) + end + + private + def method_missing(method, *args) + RecordIdentifier.send(method, *args) + end +end + +class NestedRecordIdentifierTest < RecordIdentifierTest + def setup + @klass = Post::Nested + @record = @klass.new + @singular = 'post_nested' + @plural = 'post_nesteds' + end + + def test_partial_path + expected = "post/nesteds/nested" + assert_equal expected, partial_path(@record) + assert_equal expected, partial_path(Post::Nested) + end +end \ No newline at end of file diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index cc8b650039..ef658517ff 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -1,5 +1,7 @@ require "#{File.dirname(__FILE__)}/../abstract_unit" +Bunny = Struct.new(:Bunny, :id) + module BaseTest include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::PrototypeHelper @@ -253,6 +255,11 @@ Element.update("baz", "

This is a test

"); assert_equal %($("hello");), @generator['hello'] end + def test_element_access_on_records + assert_equal %($("bunny_5");), @generator[Bunny.new(:id => 5)] + assert_equal %($("new_bunny");), @generator[Bunny.new] + end + def test_element_proxy_one_deep @generator['hello'].hide assert_equal %($("hello").hide();), @generator.to_s -- cgit v1.2.3