require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe_with_render ActiveAdmin::FormBuilder do def build_form(&block) Admin::PostsController.form(&block) get :new end context "in general" do before do build_form do |f| f.inputs do f.input :title f.input :body end f.label :title, "My Super Title" f.text_field :title f.buttons do f.submit "My Submit Button" f.commit_button "Submit Me" f.commit_button "Another Button" end end end it "should generate a text input" do response.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should generate a label using the default form methods" do response.should have_tag("label", "My Super Title") end it "should generate a textarea" do response.should have_tag("textarea", :attributes => { :name => "post[body]" }) end it "should only generate the form once" do response.body.scan(/My Super Title/).size.should == 1 end it "should generate buttons" do response.should have_tag("input", :attributes => { :type => "submit", :value => "Submit Me" }) response.should have_tag("input", :attributes => { :type => "submit", :value => "Another Button" }) end end describe "passing in options" do before do Admin::PostsController.form :html => { :multipart => true } do |f| f.inputs :title f.buttons end get :new end it "should pass the options on to the form" do response.should have_tag("form", :attributes => { :enctype => "multipart/form-data" }) end end context "with default settings" do before do Admin::PostsController.reset_form_config! get :new end it "should generate one post title field" do response.body.scan('id="post_title"').size.should == 1 end end context "with buttons" do it "should generate the form once" do build_form do |f| f.inputs do f.input :title end f.buttons end response.body.scan(/id=\"post_title\"/).size.should == 1 end it "should generate one button" do build_form do |f| f.buttons end response.body.scan(/type=\"submit\"/).size.should == 1 end it "should generate multiple buttons" do build_form do |f| f.buttons do f.submit "Create" f.commit_button "Create & Continue" f.commit_button "Create & Edit" end end response.body.scan(/type=\"submit\"/).size.should == 3 end end context "without passing a block to inputs" do before do build_form do |f| f.inputs :title, :body end end it "should have a title input" do response.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should have a body textarea" do response.should have_tag("textarea", :attributes => { :name => "post[body]" }) end end context "with semantic fields for" do before do build_form do |f| f.inputs do f.input :title f.input :body end f.instance_eval do @object.author = User.new end f.semantic_fields_for :author do |author| author.inputs :first_name, :last_name end end end it "should generate a nested text input once" do response.body.scan("post_author_attributes_first_name_input").size.should == 1 end end context "with collection inputs" do before do User.create :first_name => "John", :last_name => "Doe" User.create :first_name => "Jane", :last_name => "Doe" end describe "as select" do before do build_form do |f| f.input :author end end it "should create 2 options" do response.body.scan(/\