Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 98 lines (73 sloc) 2.699 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
# encoding: utf-8
require 'spec_helper'

describe 'Formtastic::Helpers::FormHelper.builder' do

  include FormtasticSpecHelper

  class MyCustomFormBuilder < Formtastic::FormBuilder
    def awesome_input(method, options)
      self.text_field(method)
    end
  end

  before do
    @output_buffer = ''
    mock_everything
  end

  it 'is the Formtastic::FormBuilder by default' do
    Formtastic::Helpers::FormHelper.builder.should == Formtastic::FormBuilder
  end

  it 'can be configured to use your own custom form builder' do
    # Set it to a custom builder class
    Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
    Formtastic::Helpers::FormHelper.builder.should == MyCustomFormBuilder

    # Reset it to the default
    Formtastic::Helpers::FormHelper.builder = Formtastic::FormBuilder
    Formtastic::Helpers::FormHelper.builder.should == Formtastic::FormBuilder
  end

  it 'should allow custom settings per form builder subclass' do
    with_config(:all_fields_required_by_default, true) do
      MyCustomFormBuilder.all_fields_required_by_default = false

      MyCustomFormBuilder.all_fields_required_by_default.should be_false
      Formtastic::FormBuilder.all_fields_required_by_default.should be_true
    end
  end

  describe "when using a custom builder" do

    before do
      @new_post.stub!(:title)
      Formtastic::Helpers::FormHelper.builder = MyCustomFormBuilder
    end

    after do
      Formtastic::Helpers::FormHelper.builder = Formtastic::FormBuilder
    end

    describe "semantic_form_for" do

      it "should yield an instance of the custom builder" do
        semantic_form_for(@new_post) do |builder|
          builder.class.should == MyCustomFormBuilder
        end
      end

      it "should allow me to call my custom input" do
        semantic_form_for(@new_post) do |builder|
          concat(builder.input(:title, :as => :awesome))
        end
      end

    end

    describe "semantic_fields_for" do

      it "should yield an instance of the parent form builder" do
        semantic_form_for(@new_post) do |builder|
          builder.semantic_fields_for(:author) do |nested_builder|
            nested_builder.class.should == MyCustomFormBuilder
          end
        end
      end

    end

  end

  describe "when using a builder passed to form options" do

    describe "semantic_fields_for" do

      it "should yield an instance of the parent form builder" do
        semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
          builder.semantic_fields_for(:author) do |nested_builder|
            nested_builder.class.should == MyCustomFormBuilder
          end
        end
      end

    end

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