include validations on ancestors #855

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

ciscou commented May 31, 2012

Hi

Validations on parent classes were not taken into accout. I mean, if we have

class Post < ActiveRecord::Base
  validates :title, :presence => true
end

class SpecialPost < Post
end

semantic_form_for SpecialPost.new do |f|
  f.input :title
end

Then the label for title would not include an asterisk, meaning this field is required (and it should)

This pull request fixes that

Thanks!

Owner

justinfrench commented Jun 4, 2012

Hi, can you please add a test for this. The implementation seems a bit brittle, so I'd like some coverage to make sure we understand what the intent is, and to ensure we don't regress.

ciscou commented Jun 14, 2012

Hi

I've just realized this wasn't a formtastic bug at all, sorry. I will explain what happened to me just in case it helps someone.

I was using thinking sphinx in the parent class, like that

# app/models/parent.rb

class Parent
  define_index do
    # blah
  end

  validates :foo, :bar, :presence => true
end

# app/models/child.rb

class Child < Parent
end

It happens that thinking_sphinx calls a method called load_models, that will load everything that inherits from ActiveRecord::Base, so that Child will be eventually load (notice that parent has no validators at the moment define_index is called)

Then, Child will copy the validators from Parent (none yet!), and then will finish the loading of Parent, adding the proper validations to :foo and :bar (in Parent but NOT in Child)

So, thank you anyway and sorry for opening an invalid pull request.

ciscou closed this Jun 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment