Enumerating validated attributes?

It seems as though there is currently no way to enumerate required attributes in a model. This functionality could be quite nice for seeing which fields are mandatory in a form. Would this be of interest to anyone?

Here is a quick overview:

schema.rb

create_table “people”, :force => true do |t|

t.string “first_name”

t.string “last_name”

t.integer “age”

end

person.rb

class Person < ActiveRecord::Base

validates_presence_of :first_name, :last_name

end

Person.required_attributes

=> [:first_name, :last_name]

CSS Styles

.mandatory_field {

background-color: red;

}

.form_field {

background-color: grey;

}

people_helper.rb

module PeopleHelper

def css_class(field_name)

Person.required_attributes.include?(fieldname) ? 'mandatory_field' : 'form_field'

end

end

new / edit.html.erb

<%= form_for(@person) do |f| %>

">
<%= f.text_field :first_name %>

<% end %>

+1. Looks like a good way to eliminate the hassle of all those little red asteriks.

I’ve submitted a patch to Lighthouse on this subject:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/750-patch-activerecord-required_attributes

Please show some +1 love if this is helpful to you!

Thanks!

Matt Darby**, M.S.**

Rails | PHP | Linux | MySQL | IT

Email: matt@matt-darby.com

Skype: matt-darby

Web: http://blog.matt-darby.com

Couldn't the same thing be accomplished by doing the following?

# person.rb
class Person < ActiveRecord::Base
  REQUIRED_ATTRS = %w(first_name last_name)

  validates_presence_of REQUIRED_ATTRS
end

# people_helper.rb
module PeopleHelper
  def css_class(field_name)
    Person::REQUIRED_ATTRS.include?(fieldname) ? 'mandatory_field' :
'form_field'
  end
end

+1. Looks like a good way to eliminate the hassle of all those little red
asteriks.

I've submitted a patch to Lighthouse on this subject:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/750-patch-activerecord-required_attributes
Please show some +1 love if this is helpful to you!

We also had a thread earlier from Ruy Asan (
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/4cce3966cd61f278/dc000651f75a575a?lnk=gst
) about reimplementing the internals of the validation code to make
stuff like this *much* easier. I'm not sure if Ruy and Rick have
started investigating merging that stuff, but rather than patch in
this kind of behaviour it'd be nice to get it for 'free' when we make
a design improvement.

I made a patch that solves this problem in a more elegant and useful
way.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/750-patch-activerecord-required_attributes
(2nd Patch)

Usage:
class Person < ActiveRecord::Base
  validates_uniqueness_of :name
  validates_presence_of :name, :city
end

Person.validations
=> {:uniqueness => [:name], :presence => [:name, :city]}

As far as the branches that are reworking validations- they seem to be
dead. I'd be all for helping out, if there's still an impetus behind
merging in the branches at some point in time.

I added some comments to Erik Peterson last patch on Lighthouse. It
would be nice if more people help with the discussion:

Wouldn't be nice with validations also store the options sent?

  class Person < ActiveRecord::Base
    validates_presence_of :name, :city
    validates_length_of :name, :within => 3..20,
  end

  Person.validations
  => {:presence => { :name => {}, :city => {} }, :length => { :name =>
{ :within => 3..20 } }}

I think it would be even more readable if it is "attribute oriented":

  Person.validations
  => {:name => { :presence => {}, :length => { :within =>
3..20 } }, :city => { :presence => {} }}

Why? This would DRY javascript code generation from our models
completely.

As far as the branches that are reworking validations- they seem to be
dead. I'd be all for helping out, if there's still an impetus behind
merging in the branches at some point in time.

Actually the main validations rework is happening in active_model [1]
with the hope that one day activerecord could make use of active_model,
perhaps also using active_relation [2] for the sql generation.

[1] http://github.com/rubyruy/rails/tree/active_model_validations
[2] git://github.com/nkallen/arel.git

I made a patch that solves this problem in a more elegant and useful
way.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/750-patch-activerecord-required_attributes
(2nd Patch)

Usage:
class Person < ActiveRecord::Base
validates_uniqueness_of :name
validates_presence_of :name, :city
end

Person.validations
=> {:uniqueness => [:name], :presence => [:name, :city]}

This is nice, but I think it'd be good to do this with some classes
rather than hashes. Have a look at ruy asan's branch for some ideas
but something like having a set of Validation objects associated with
the model class would be nice. Then you can make the set itself
ducktype a little more like you've mentioned above.

Person.validations[:uniqueness] => [ValidationObjectForName]
Person.validations[:presence] => [ValidationObjectForName,
ValidationObjectForCity]

We can then, at a later date, move the callback chains to introspect
those validation instances rather than just generating code at
runtime.

As far as the branches that are reworking validations- they seem to be
dead. I'd be all for helping out, if there's still an impetus behind
merging in the branches at some point in time.

The branches in particular aren't that important, it's more important
that there are people thinking about the issues to fix. If the
initial authors have lost interest or don't have time, you should
either reinvigorate that branch or take the ideas / patches into your
own branch and get that ready for review :slight_smile: