Sha256: ded4d3c809d9e7bdc970223d86c29914198e17d06307a255dddb8bdbfe62dea7

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'test_helper'

class DslTest < MiniTest::Spec
  class SongForm < Reform::Form
    include DSL

    property  :title,  :on => :song
    properties [:name, :genre],   :on => :artist

    validates :name, :title, :genre, presence: true
  end

  let (:form) { SongForm.new(:song => OpenStruct.new(:title => "Rio"), :artist => OpenStruct.new()) }

  it "works by creating Representer and Composition for you" do
    form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb").must_equal false
  end
end

class ActiveModelTest < MiniTest::Spec
   class HitForm < Reform::Form
    include DSL
    include Reform::Form::ActiveModel

    property  :title,  :on => :song
    properties [:name, :genre],   :on => :artist

    model :hit, :on => :song
  end

  let (:rio) { OpenStruct.new(:title => "Rio") }
  let (:duran) { OpenStruct.new }
  let (:form) { HitForm.new(:song => rio, :artist => duran) }

  it "creates model readers" do
    form.hit.must_equal rio
  end

  it "creates composition readers" do
    form.song.must_equal rio
    form.artist.must_equal duran
  end

  it "provides ::model_name" do
    form.class.model_name.must_equal "Hit"
  end

  it "provides #persisted?" do
    HitForm.new(:song => OpenStruct.new.instance_eval { def persisted?; "yo!"; end; self }, :artist => OpenStruct.new).persisted?.must_equal "yo!"
  end

  it "provides #to_key" do
    HitForm.new(:song => OpenStruct.new.instance_eval { def to_key; "yo!"; end; self }, :artist => OpenStruct.new).to_key.must_equal "yo!"
  end

  it "provides #to_param" do
    HitForm.new(:song => OpenStruct.new.instance_eval { def to_param; "yo!"; end; self }, :artist => OpenStruct.new).to_param.must_equal "yo!"
  end

  it "works with any order of ::model and ::property" do
    class AnotherForm < Reform::Form
      include DSL
      include Reform::Form::ActiveModel

      model :song, :on => :song
      property  :title,  :on => :song
    end


    AnotherForm.new(:song => rio).song.must_equal rio
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-0.1.0 test/dsl_test.rb