Permalink
Browse files

basic fields_for_associated helper

git-svn-id: http://svn.jamesgolick.com/attribute_fu/trunk@17 80b79608-713f-0410-8737-d8c0d0c1b50c
  • Loading branch information...
1 parent b960807 commit 1ea4983a4f1349fef812dcb74fd31890c3d404b4 james committed Nov 22, 2007
View
@@ -1 +1,2 @@
-ActiveRecord::Base.class_eval { include AttributeFu::Associations }
+ActiveRecord::Base.class_eval { include AttributeFu::Associations }
+ActionView::Helpers::FormBuilder.class_eval { include AttributeFu::AssociatedFormHelper }
@@ -0,0 +1,5 @@
+module AttributeFu
+ class AssociatedFormBuilder < ::ActionView::Helpers::FormBuilder
+
+ end
+end
@@ -0,0 +1,19 @@
+module AttributeFu
+ module AssociatedFormHelper
+ def fields_for_associated(associated_name, *args, &block)
+ object = args.first
+ name = "#{@object_name}[#{associated_name}_attributes]"
+
+ unless object.new_record?
+ name << "[#{object.new_record? ? 'new' : object.id}]"
+ else
+ @new_objects ||= {}
+ @new_objects[associated_name] ||= -1 # we want naming to start at 0
+
+ name << "[new][#{@new_objects[associated_name]+=1}]"
+ end
+
+ @template.fields_for(name, *args, &block)
+ end
+ end
+end
View
@@ -1,36 +1,10 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
+require 'mocha'
class Test::Unit::TestCase
- # Transactional fixtures accelerate your tests by wrapping each test method
- # in a transaction that's rolled back on completion. This ensures that the
- # test database remains unchanged so your fixtures don't have to be reloaded
- # between every test method. Fewer database queries means faster tests.
- #
- # Read Mike Clark's excellent walkthrough at
- # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
- #
- # Every Active Record database supports transactions except MyISAM tables
- # in MySQL. Turn off transactional fixtures in this case; however, if you
- # don't care one way or the other, switching from MyISAM to InnoDB tables
- # is recommended.
self.use_transactional_fixtures = true
-
- # Instantiated fixtures are slow, but give you @david where otherwise you
- # would need people(:david). If you don't want to migrate your existing
- # test cases which use the @david style and don't mind the speed hit (each
- # instantiated fixtures translates to a database query per test method),
- # then set this back to true.
self.use_instantiated_fixtures = false
-
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- # If you need to control the loading order (due to foreign key constraints etc), you'll
- # need to change this line to explicitly name the order you desire.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
fixtures :all
-
- # Add more helper methods to be used by all tests here...
end
@@ -0,0 +1,62 @@
+require File.dirname(__FILE__)+'/../test_helper'
+
+class AssociatedFormHelperTest < Test::Unit::TestCase
+ include ActionView::Helpers::FormHelper
+ include ActionView::Helpers::FormTagHelper
+ include ActionView::Helpers::UrlHelper
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::TextHelper
+ include AttributeFu::AssociatedFormHelper
+
+ def setup
+ @photo = Photo.create
+ @controller = mock()
+ @controller.stubs(:url_for).returns 'asdf'
+ @controller.stubs(:protect_against_forgery?).returns false
+ stubs(:protect_against_forgery?).returns false
+ end
+
+ context "with existing object" do
+ setup do
+ @photo.comments.create :author => "Barry", :body => "Oooh I did good today..."
+
+ @erbout = assoc_output @photo.comments.first
+ end
+
+ should "name field with attribute_fu naming conventions" do
+ assert_match "photo[comment_attributes][#{@photo.comments.first.id}]", @erbout
+ end
+ end
+
+ context "with non-existent object" do
+ setup do
+ @erbout = assoc_output(@photo.comments.build) do |f|
+ f.fields_for_associated(:comment, @photo.comments.build) do |comment|
+ comment.text_field(:author)
+ end
+ end
+ end
+
+ should "name field with attribute_fu naming conventions" do
+ assert_match "photo[comment_attributes][new][0]", @erbout
+ end
+
+ should "maintain the numbering of the new object if called again" do
+ assert_match "photo[comment_attributes][new][1]", @erbout
+ end
+ end
+
+ private
+ def assoc_output(comment, &block)
+ _erbout = ''
+ fields_for(:photo) do |f|
+ f.fields_for_associated(:comment, comment) do |comment|
+ _erbout.concat comment.text_field(:author)
+ end
+
+ _erbout.concat yield(f) if block_given?
+ end
+
+ _erbout
+ end
+end

0 comments on commit 1ea4983

Please sign in to comment.