Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test behaviour for nil or empty objects #74

Merged
merged 1 commit into from Mar 20, 2014
Merged

Test behaviour for nil or empty objects #74

merged 1 commit into from Mar 20, 2014

Conversation

mattheworiordan
Copy link
Contributor

If you are going to support empty objects, then I think you need to address how the validators and attributes will work when the object is nil.

I have also added a test for initialization without any object behind it. I would expect this to be have similarly.

This follows the discussion on #67 (comment)

Thanks.

@apotonick apotonick merged commit 5f30828 into trailblazer:master Mar 20, 2014
@apotonick
Copy link
Member

Idea:

collection :songs do
  validates :length => { :minimum => Fixnum}

  property :title
  validates :title, presence: true
end

With that new validator, calling form.validate({}) will complain about not enough elements.

What do you think?

@mattheworiordan
Copy link
Contributor Author

I like it!

Out of interest, are you against the idea of supporting FormObject.new with any arguments? Why not simply automatically use OpenStruct.new as a default thus allowing a more natural feeling Form object without an ActiveRecord object behind it?

On a separate note, would the validations for the following work with the proposed change:

class Contact < Reform::Form
  property :address do
    property :post_code
    validates_presence_of :post_code
  end
  property :email
  validates_presence_of :email
end

form = Contact.new # optionally with OpenStruct.new if you want for the initializer
form.valid?
form.errors.messages.must_equal('email' => 'can't be blank', 'address' => { 'post_code': 'can't be blank' })

This is different to the test I wrote because this uses a form within a form as opposed to a collection where the minimum property does not apply.

WDYT? I can write a quick test if you like?

@apotonick
Copy link
Member

Maybe we should have something like

property :address, required: true, do ..

or

validates :address, presence: true
property :address do

The automatic OpenStruct.new implies another if that is magic behaviour as I see and hate in Rails. I'm fine providing this from another module. The problem here is that the form might do things you don't expect it to do.

@matcouto
Copy link

matcouto commented Sep 9, 2014

collection :songs do
    validates :length => { :minimum => Fixnum}

    property :title
    validates :title, presence: true
end

Well, let's suppose that SongForm is a nested form and I need to create a spec that tests the validations of the AlbumForm's attributes as well as the SongForm's title.
So, how could I write a spec that tests the validations of the Song collection(i.e. presence, uniqueness, etc.) property?

@apotonick
Copy link
Member

@matcouto Straight-forward.

form = AlbumForm.new(..)
form.validate({"songs"=> [..]}).must_equal true
form.errors.messages.must_equal [..]

But why do you ask that in this issue?

@matcouto
Copy link

matcouto commented Sep 9, 2014

@apotonick I thought it'd be related to that. Sorry if it's not!
Congrats for the gem. It's very useful. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants