Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 529 lines (442 sloc) 20.192 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
# encoding: utf-8

module CustomMacros

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

    def it_should_have_input_wrapper_with_class(class_name)
      it "should have input wrapper with class '#{class_name}'" do
        output_buffer.should have_tag("form li.#{class_name}")
      end
    end

    def it_should_have_input_wrapper_with_id(id_string)
      it "should have input wrapper with id '#{id_string}'" do
        output_buffer.should have_tag("form li##{id_string}")
      end
    end

    def it_should_not_have_a_label
      it "should not have a label" do
        output_buffer.should_not have_tag("form li label")
      end
    end

    def it_should_have_a_nested_fieldset
      it "should have a nested_fieldset" do
        output_buffer.should have_tag("form li fieldset")
      end
    end
    
    def it_should_have_a_nested_fieldset_with_class(klass)
      it "should have a nested_fieldset with class #{klass}" do
        output_buffer.should have_tag("form li fieldset.#{klass}")
      end
    end
    
    def it_should_have_a_nested_ordered_list_with_class(klass)
      it "should have a nested fieldset with class #{klass}" do
        output_buffer.should have_tag("form li ol.#{klass}")
      end
    end

    def it_should_have_label_with_text(string_or_regex)
      it "should have a label with text '#{string_or_regex}'" do
        output_buffer.should have_tag("form li label", string_or_regex)
      end
    end

    def it_should_have_label_for(element_id)
      it "should have a label for ##{element_id}" do
        output_buffer.should have_tag("form li label.label[@for='#{element_id}']")
      end
    end
    
    def it_should_have_an_inline_label_for(element_id)
      it "should have a label for ##{element_id}" do
        output_buffer.should have_tag("form li label[@for='#{element_id}']")
      end
    end

    def it_should_have_input_with_id(element_id)
      it "should have an input with id '#{element_id}'" do
        output_buffer.should have_tag("form li input##{element_id}")
      end
    end

    def it_should_have_select_with_id(element_id)
      it "should have a select box with id '#{element_id}'" do
        output_buffer.should have_tag("form li select##{element_id}")
      end
    end

    def it_should_have_input_with_type(input_type)
      it "should have a #{input_type} input" do
        output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]")
      end
    end

    def it_should_have_input_with_name(name)
      it "should have an input named #{name}" do
        output_buffer.should have_tag("form li input[@name=\"#{name}\"]")
      end
    end

    def it_should_have_select_with_name(name)
      it "should have an input named #{name}" do
        output_buffer.should have_tag("form li select[@name=\"#{name}\"]")
      end
    end

    def it_should_have_textarea_with_name(name)
      it "should have an input named #{name}" do
        output_buffer.should have_tag("form li textarea[@name=\"#{name}\"]")
      end
    end

    def it_should_have_textarea_with_id(element_id)
      it "should have an input with id '#{element_id}'" do
        output_buffer.should have_tag("form li textarea##{element_id}")
      end
    end

    def it_should_have_label_and_input_with_id(element_id)
      it "should have an input with id '#{element_id}'" do
        output_buffer.should have_tag("form li input##{element_id}")
        output_buffer.should have_tag("form li label[@for='#{element_id}']")
      end
    end

    def it_should_use_default_text_field_size_when_not_nil(as)
      it 'should use default_text_field_size when not nil' do
        with_config :default_text_field_size, 30 do
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => as))
          end)
          output_buffer.should have_tag("form li input[@size='#{Formtastic::FormBuilder.default_text_field_size}']")
        end
      end
    end

    def it_should_not_use_default_text_field_size_when_nil(as)
      it 'should not use default_text_field_size when nil' do
        with_config :default_text_field_size, nil do
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => as))
          end)
          output_buffer.should have_tag("form li input")
          output_buffer.should_not have_tag("form li input[@size]")
        end
      end
    end

    def it_should_apply_custom_input_attributes_when_input_html_provided(as)
      it 'it should apply custom input attributes when input_html provided' do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:title, :as => as, :input_html => { :class => 'myclass' }))
        end)
        output_buffer.should have_tag("form li input.myclass")
      end
    end

    def it_should_apply_custom_for_to_label_when_input_html_id_provided(as)
      it 'it should apply custom for to label when input_html :id provided' do
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:title, :as => as, :input_html => { :id => 'myid' }))
        end)
        output_buffer.should have_tag('form li label[@for="myid"]')
      end
    end

    def it_should_have_maxlength_matching_column_limit
      it 'should have a maxlength matching column limit' do
        @new_post.column_for_attribute(:title).limit.should == 50
        output_buffer.should have_tag("form li input[@maxlength='50']")
      end
    end

    def it_should_use_column_size_for_columns_shorter_than_default_text_field_size(as)
      it 'should use the column size for columns shorter than default_text_field_size' do
        column_limit_shorted_than_default = 1
        @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => as, :limit => column_limit_shorted_than_default))

        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:title, :as => as))
        end)

        output_buffer.should have_tag("form li input[@size='#{column_limit_shorted_than_default}']")
      end
    end

    def it_should_apply_error_logic_for_input_type(type)
      describe 'when there are errors on the object for this method' do
        before do
          @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
          @errors = mock('errors')
          @errors.stub!(:[]).with(:title).and_return(@title_errors)
          Formtastic::FormBuilder.file_metadata_suffixes.each do |suffix|
            @errors.stub!(:[]).with("title_#{suffix}".to_sym).and_return(nil)
          end
          @new_post.stub!(:errors).and_return(@errors)
        end

        it 'should apply an errors class to the list item' do
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => type))
          end)
          output_buffer.should have_tag('form li.error')
        end

        it 'should not wrap the input with the Rails default error wrapping' do
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => type))
          end)
          output_buffer.should_not have_tag('div.fieldWithErrors')
        end

        it 'should render a paragraph for the errors' do
          Formtastic::FormBuilder.inline_errors = :sentence
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => type))
          end)
          output_buffer.should have_tag('form li.error p.inline-errors')
        end

        it 'should not display an error list' do
          Formtastic::FormBuilder.inline_errors = :list
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => type))
          end)
          output_buffer.should have_tag('form li.error ul.errors')
        end
      end

      describe 'when there are no errors on the object for this method' do
        before do
          @form = semantic_form_for(@new_post) do |builder|
            concat(builder.input(:title, :as => type))
          end
        end

        it 'should not apply an errors class to the list item' do
          output_buffer.should_not have_tag('form li.error')
        end

        it 'should not render a paragraph for the errors' do
          output_buffer.should_not have_tag('form li.error p.inline-errors')
        end

        it 'should not display an error list' do
          output_buffer.should_not have_tag('form li.error ul.errors')
        end
      end

      describe 'when no object is provided' do
        before do
          concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
            concat(builder.input(:title, :as => type))
          end)
        end

        it 'should not apply an errors class to the list item' do
          output_buffer.should_not have_tag('form li.error')
        end

        it 'should not render a paragraph for the errors' do
          output_buffer.should_not have_tag('form li.error p.inline-errors')
        end

        it 'should not display an error list' do
          output_buffer.should_not have_tag('form li.error ul.errors')
        end
      end
    end

    def it_should_call_find_on_association_class_when_no_collection_is_provided(as)
      it "should call find on the association class when no collection is provided" do
        ::Author.should_receive(:where)
        concat(semantic_form_for(@new_post) do |builder|
          concat(builder.input(:author, :as => as))
        end)
      end
    end

    def it_should_use_the_collection_when_provided(as, countable)
      describe 'when the :collection option is provided' do

        before do
          @authors = ::Author.all * 2
          output_buffer.replace ''
        end

        it 'should use the provided collection' do
          concat(semantic_form_for(@new_post) do |builder|
            concat(builder.input(:author, :as => as, :collection => @authors))
          end)
          output_buffer.should have_tag("form li.#{as} #{countable}", :count => @authors.size + (as == :select ? 1 : 0))
        end

        describe 'and the :collection is an array of strings' do
          before do
            @categories = [ 'General', 'Design', 'Development', 'Quasi-Serious Inventions' ]
          end

          it "should use the string as the label text and value for each #{countable}" do
            concat(semantic_form_for(@new_post) do |builder|
              concat(builder.input(:category_name, :as => as, :collection => @categories))
            end)

            @categories.each do |value|
              output_buffer.should have_tag("form li.#{as}", /#{value}/)
              output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']")
            end
          end

          if as == :radio
            it 'should generate a sanitized label for attribute' do
              @bob.stub!(:category_name).and_return(@categories)
              concat(semantic_form_for(@new_post) do |builder|
                fields = builder.semantic_fields_for(@bob) do |bob_builder|
                  concat(bob_builder.input(:category_name, :as => as, :collection => @categories))
                end
                concat(fields)
              end)
              output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_general']")
              output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_design']")
              output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_development']")
              output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_quasi-serious_inventions']")
            end
          end
        end

        describe 'and the :collection is a hash of strings' do
          before do
            @categories = { 'General' => 'gen', 'Design' => 'des','Development' => 'dev' }
          end

          it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do
            concat(semantic_form_for(@new_post) do |builder|
              concat(builder.input(:category_name, :as => as, :collection => @categories))
            end)

            @categories.each do |label, value|
              output_buffer.should have_tag("form li.#{as}", /#{label}/)
              output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']")
            end
          end
        end

        describe 'and the :collection is an array of arrays' do
          before do
            @categories = { 'General' => 'gen', 'Design' => 'des', 'Development' => 'dev' }.to_a
          end

          it "should use the first value as the label text and the last value as the value attribute for #{countable}" do
            concat(semantic_form_for(@new_post) do |builder|
              concat(builder.input(:category_name, :as => as, :collection => @categories))
            end)

            @categories.each do |text, value|
              label = as == :select ? :option : :label
              output_buffer.should have_tag("form li.#{as} #{label}", /#{text}/i)
              output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value.to_s}']")
              output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_#{value.to_s}") if as == :radio
            end
          end
        end

        if as == :radio
          describe 'and the :collection is an array of arrays with boolean values' do
            before do
              @choices = { 'Yeah' => true, 'Nah' => false }.to_a
            end

            it "should use the first value as the label text and the last value as the value attribute for #{countable}" do
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:category_name, :as => as, :collection => @choices))
              end)

              output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_true")
              output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_false")
            end
          end
        end

        describe 'and the :collection is an array of symbols' do
          before do
            @categories = [ :General, :Design, :Development ]
          end

          it "should use the symbol as the label text and value for each #{countable}" do
            concat(semantic_form_for(@new_post) do |builder|
              concat(builder.input(:category_name, :as => as, :collection => @categories))
            end)

            @categories.each do |value|
              label = as == :select ? :option : :label
              output_buffer.should have_tag("form li.#{as} #{label}", /#{value}/i)
              output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value.to_s}']")
            end
          end
        end

        describe 'and the :collection is an OrderedHash of strings' do
          before do
            @categories = ActiveSupport::OrderedHash.new('General' => 'gen', 'Design' => 'des','Development' => 'dev')
          end

          it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do
            concat(semantic_form_for(@new_post) do |builder|
              concat(builder.input(:category_name, :as => as, :collection => @categories))
            end)

            @categories.each do |label, value|
              output_buffer.should have_tag("form li.#{as}", /#{label}/)
              output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']")
            end
          end

        end

        describe 'when the :member_label option is provided' do

          describe 'as a symbol' do
            before do
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_label => :login))
              end)
            end

            it 'should have options with text content from the specified method' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as}", /#{author.login}/)
              end
            end
          end

          describe 'as a proc' do
            before do
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_label => Proc.new {|a| a.login.reverse }))
              end)
            end

            it 'should have options with the proc applied to each' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as}", /#{author.login.reverse}/)
              end
            end
          end

          describe 'as a method object' do
            before do
              def reverse_login(a)
                a.login.reverse
              end
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_label => method(:reverse_login)))
              end)
            end

            it 'should have options with the proc applied to each' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as}", /#{author.login.reverse}/)
              end
            end
          end
        end

        describe 'when the :member_label option is not provided' do
          Formtastic::FormBuilder.collection_label_methods.each do |label_method|

            describe "when the collection objects respond to #{label_method}" do
              before do
                @fred.stub!(:respond_to?).and_return { |m| m.to_s == label_method || m.to_s == 'id' }
                ::Author.all.each { |a| a.stub!(label_method).and_return('The Label Text') }

                concat(semantic_form_for(@new_post) do |builder|
                  concat(builder.input(:author, :as => as))
                end)
              end

              it "should render the options with #{label_method} as the label" do
                ::Author.all.each do |author|
                  output_buffer.should have_tag("form li.#{as}", /The Label Text/)
                end
              end
            end

          end
        end

        describe 'when the :member_value option is provided' do

          describe 'as a symbol' do
            before do
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_value => :login))
              end)
            end

            it 'should have options with values from specified method' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{author.login}']")
              end
            end
          end

          describe 'as a proc' do
            before do
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_value => Proc.new {|a| a.login.reverse }))
              end)
            end

            it 'should have options with the proc applied to each value' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{author.login.reverse}']")
              end
            end
          end

          describe 'as a method object' do
            before do
              def reverse_login(a)
                a.login.reverse
              end
              concat(semantic_form_for(@new_post) do |builder|
                concat(builder.input(:author, :as => as, :member_value => method(:reverse_login)))
              end)
            end

            it 'should have options with the proc applied to each value' do
              ::Author.all.each do |author|
                output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{author.login.reverse}']")
              end
            end
          end
        end

      end
    end

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