Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 143 lines (122 sloc) 5.269 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
# encoding: utf-8
require 'spec_helper'

describe 'Formtastic::FormBuilder#fields_for' do

  include FormtasticSpecHelper

  before do
    @output_buffer = ''
    mock_everything
    @new_post.stub!(:author).and_return(::Author.new)
  end

  context 'outside a form_for block' do
    it 'yields an instance of FormHelper.builder' do
      semantic_fields_for(@new_post) do |nested_builder|
        nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
      end
      semantic_fields_for(@new_post.author) do |nested_builder|
        nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
      end
      semantic_fields_for(:author, @new_post.author) do |nested_builder|
        nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
      end
      semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
        nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
      end
    end
    
    it 'should respond to input' do
      semantic_fields_for(@new_post) do |nested_builder|
        nested_builder.respond_to?(:input).should be_true
      end
      semantic_fields_for(@new_post.author) do |nested_builder|
        nested_builder.respond_to?(:input).should be_true
      end
      semantic_fields_for(:author, @new_post.author) do |nested_builder|
        nested_builder.respond_to?(:input).should be_true
      end
      semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
        nested_builder.respond_to?(:input).should be_true
      end
    end
  end
  
  context 'within a form_for block' do
    it 'yields an instance of FormHelper.builder' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_fields_for(:author) do |nested_builder|
          nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
        end
      end
    end
    
    it 'yields an instance of FormHelper.builder with hash-like model' do
      semantic_form_for(:user) do |builder|
        builder.semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
          nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
        end
      end
    end
    
    it 'nests the object name' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_fields_for(@bob) do |nested_builder|
          nested_builder.object_name.should == 'post[author]'
        end
      end
    end
    
    it 'supports passing collection as second parameter' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_fields_for(:author, [@fred,@bob]) do |nested_builder|
          nested_builder.object_name.should =~ /post\[author_attributes\]\[\d+\]/
        end
      end
    end
    
    it 'should sanitize html id for li tag' do
      @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
      concat(semantic_form_for(@new_post) do |builder|
        concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
          concat(nested_builder.inputs(:login))
        end)
      end)
      output_buffer.should have_tag('form fieldset.inputs #post_author_1_login_input')
      # Not valid selector, so using good ol' regex
      output_buffer.should_not =~ /id="post\[author\]_1_login_input"/
      # <=> output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input')
    end
    
    it 'should use namespace provided in nested fields' do
      @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255))
      concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
        concat(builder.semantic_fields_for(@bob, :index => 1) do |nested_builder|
          concat(nested_builder.inputs(:login))
        end)
      end)
      output_buffer.should have_tag('form fieldset.inputs #context2_post_author_1_login_input')
    end
    
    it 'should render errors on the nested inputs' do
      @errors = mock('errors')
      @errors.stub!(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
      @bob.stub!(:errors).and_return(@errors)
      
      concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
        concat(builder.semantic_fields_for(@bob) do |nested_builder|
          concat(nested_builder.inputs(:login))
        end)
      end)
      output_buffer.should =~ /oh noes/
    end
    
  end
  
  context "when I rendered my own hidden id input" do
    
    before do
      output_buffer.replace ''
      
      @fred.posts.size.should == 1
      @fred.posts.first.stub!(:persisted?).and_return(true)
      @fred.stub!(:posts_attributes=)

      concat(semantic_form_for(@fred) do |builder|
        concat(builder.semantic_fields_for(:posts) do |nested_builder|
          concat(nested_builder.input(:id, :as => :hidden))
          concat(nested_builder.input(:title))
        end)
      end)
    end
  
    it "should only render one hidden input (my one)" do
      output_buffer.should have_tag 'input#author_posts_attributes_0_id', :count => 1
    end
    
    it "should render the hidden input inside an li.hidden" do
      output_buffer.should have_tag 'li.hidden input#author_posts_attributes_0_id'
    end
  end

end
Something went wrong with that request. Please try again.