require 'spec_helper' describe ActiveAdmin::FormBuilder do # Setup an ActionView::Base object which can be used for # generating the form for. let(:helpers) do view = action_view def view.posts_path "/posts" end def view.protect_against_forgery? false end def view.url_for(*args) if args.first == {:action => "index"} posts_path else super end end def view.a_helper_method "A Helper Method" end view end def build_form(options = {}, form_object = Post.new, &block) options = {:url => helpers.posts_path}.merge(options) render_arbre_component({:form_object => form_object, :form_options => options, :form_block => block}, helpers)do text_node active_admin_form_for(assigns[:form_object], assigns[:form_options], &assigns[:form_block]) end.to_s end context "in general with actions" do let :body do build_form do |f| f.inputs do f.input :title f.input :body end f.actions do f.action :submit, :label => "Submit Me" f.action :submit, :label => "Another Button" end end end it "should generate a text input" do body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should generate a textarea" do body.should have_tag("textarea", :attributes => { :name => "post[body]" }) end it "should only generate the form once" do body.scan(/Title/).size.should == 1 end it "should generate actions" do body.should have_tag("input", :attributes => { :type => "submit", :value => "Submit Me" }) body.should have_tag("input", :attributes => { :type => "submit", :value => "Another Button" }) end end context "in general with actions" do let :body do build_form do |f| f.inputs do f.input :title f.input :body end f.actions do f.action :submit, :button_html => { :value => "Submit Me" } f.action :submit, :button_html => { :value => "Another Button" } end end end it "should generate a text input" do body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should generate a textarea" do body.should have_tag("textarea", :attributes => { :name => "post[body]" }) end it "should only generate the form once" do body.scan(/Title/).size.should == 1 end it "should generate actions" do body.should have_tag("input", :attributes => { :type => "submit", :value => "Submit Me" }) body.should have_tag("input", :attributes => { :type => "submit", :value => "Another Button" }) end end context "when polymorphic relationship" do it "should raise error" do lambda { comment = ActiveAdmin::Comment.new build_form({:url => "admins/comments"}, comment) do |f| f.inputs :resource end }.should raise_error(Formtastic::PolymorphicInputWithoutCollectionError) end end describe "passing in options with actions" do let :body do build_form :html => { :multipart => true } do |f| f.inputs :title f.actions end end it "should pass the options on to the form" do body.should have_tag("form", :attributes => { :enctype => "multipart/form-data" }) end end describe "passing in options with actions" do let :body do build_form :html => { :multipart => true } do |f| f.inputs :title f.actions end end it "should pass the options on to the form" do body.should have_tag("form", :attributes => { :enctype => "multipart/form-data" }) end end context "with actions" do it "should generate the form once" do body = build_form do |f| f.inputs do f.input :title end f.actions end body.scan(/id=\"post_title\"/).size.should == 1 end it "should generate one button and a cancel link" do body = build_form do |f| f.actions end body.scan(/type=\"submit\"/).size.should == 1 body.scan(/class=\"cancel\"/).size.should == 1 end it "should generate multiple actions" do body = build_form do |f| f.actions do f.action :submit, :label => "Create & Continue" f.action :submit, :label => "Create & Edit" end end body.scan(/type=\"submit\"/).size.should == 2 body.scan(/class=\"cancel\"/).size.should == 0 end end context "with actons" do it "should generate the form once" do body = build_form do |f| f.inputs do f.input :title end f.actions end body.scan(/id=\"post_title\"/).size.should == 1 end it "should generate one button and a cancel link" do body = build_form do |f| f.actions end body.scan(/type=\"submit\"/).size.should == 1 body.scan(/class=\"cancel\"/).size.should == 1 end it "should generate multiple actions" do body = build_form do |f| f.actions do f.action :submit, :label => "Create & Continue" f.action :submit, :label => "Create & Edit" end end body.scan(/type=\"submit\"/).size.should == 2 body.scan(/class=\"cancel\"/).size.should == 0 end end context "without passing a block to inputs" do let :body do build_form do |f| f.inputs :title, :body end end it "should have a title input" do body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should have a body textarea" do body.should have_tag("textarea", :attributes => { :name => "post[body]" }) end end context "with semantic fields for" do let :body 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 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 let :body do build_form do |f| f.input :author end end it "should create 2 options" do body.scan(/\