Sha256: a2d78ecce5592b38e8b788974cdc406457ac4c15fce33f20d78264aa9f56c4d2

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'test_helper'

class SkipIfTest < BaseTest

  class AlbumForm < TestForm
    property :title

    property :hit, skip_if: lambda { |options| options[:fragment]["title"]=="" } do
      property :title
      validation do
        required(:title).filled
      end
    end

    collection :songs, skip_if: :skip_song?, populate_if_empty: BaseTest::Song do
      property :title
    end

    def skip_song?(options)
      options[:fragment]["title"].nil?
    end
  end


  let (:hit) { Song.new }
  let (:album) { Album.new(nil, hit, [], nil) }

  # deserializes when present.
  it do
    form = AlbumForm.new(album)
    form.validate("hit" => {"title" => "Altar Of Sacrifice"}).must_equal true
    form.hit.title.must_equal "Altar Of Sacrifice"
  end

  # skips deserialization when not present.
  it do
    form = AlbumForm.new(Album.new)
    form.validate("hit" => {"title" => ""}).must_equal true
    assert_nil form.hit # hit hasn't been deserialised.
  end

  # skips deserialization when not present.
  it do
    form = AlbumForm.new(Album.new(nil, nil, []))
    form.validate("songs" => [{"title" => "Waste Of Breath"}, {"title" => nil}]).must_equal true
    form.songs.size.must_equal 1
    form.songs[0].title.must_equal "Waste Of Breath"
  end
end

class SkipIfAllBlankTest < BaseTest
  # skip_if: :all_blank"
  class AlbumForm < TestForm
    collection :songs, skip_if: :all_blank, populate_if_empty: BaseTest::Song do
      property :title
      property :length
    end
  end

  # create only one object.
  it do
    form = AlbumForm.new(OpenStruct.new(songs: []))
    form.validate("songs" => [{"title"=>"Apathy"}, {"title"=>"", "length" => ""}]).must_equal true
    form.songs.size.must_equal 1
    form.songs[0].title.must_equal "Apathy"
  end

  it do
    form = AlbumForm.new(OpenStruct.new(songs: []))
    form.validate("songs" => [{"title"=>"", "length" => ""}, {"title"=>"Apathy"}]).must_equal true
    form.songs.size.must_equal 1
    form.songs[0].title.must_equal "Apathy"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-2.3.0.rc1 test/skip_if_test.rb