6

I have a model named Calendar.

The validations that will be applied to it varies from the selections made by the user.

I know that I can use custom validation + conditional validation to do this, but doesn't look very clean to me.

I wonder if I can store it on a database column and pass it to a "generic" validator method.

What do you think?

Explaining further:

A user has a calendar.
Other users that have access to this calendar, can schedule appointments.

To schedule an appointment the app should validate according to the rules defined by the calendar's owner.

There are many combinations, so what I came to is:

Create custom validator classes to each of the possible validations and make then conditional.

class Calendar
  validate_allowed_in_hollydays :appointment_date if :allowedinhollydays?
  (tenths of other cases)
  ...
end

This works, but feels wrong.

I'm thinking about storing somewhere which rules should be applied to that calendar and then doing something like:

validate_stored_rules :appointment_date
4
  • 'Store' what in the database column?
    – Dogbert
    Commented Apr 12, 2011 at 20:07
  • Store the validations that should be applied to the object. Commented Apr 13, 2011 at 0:33
  • I'm not sure if that would be the best way. If you explain a bit more of the exact use case, someone might be able to suggest the best way to achieve it.
    – Dogbert
    Commented Apr 13, 2011 at 10:43
  • @Dogbert Check my answer, the idea is to wrap repeating option, in this case a condition for set of validations, this problem occurse multiple times, the most common problems to solve are validation depending on field (like roles), validation depending on the state of object - be more strict in certain states, validation of multi-page forms, where one object is displayed in few forms, so the validation could be limited to certain parts - those that are displayed actually
    – mpapis
    Commented Apr 16, 2011 at 17:56

3 Answers 3

1

It seems a little backwards to save the data in the database and then validate it.

I think your initial thought of going with some custom validation is the best bet. validates_with looks like your best option. You could then create a separate class and build all the validation inside that to keep it separate from your main model.

class Person < ActiveRecord::Base
  validates_with GoodnessValidator
end

class GoodnessValidator < ActiveModel::Validator
  def validate
    if record.first_name == "Evil"
      record.errors[:base] << "This person is evil"
    end
  end
end

Code lifted straight from the Rails Validation Guide

3
  • Tks that's what I'm doing right now. The problem is that I have lots of validations and lots of "ifs". My ideia is to store somewhere the valitations that a object should be validated against and then load it to the object at runtime. Commented Apr 13, 2011 at 11:23
  • That or validating it all with a background task with something like delayed_job might help to improve performance. It depends how quickly you need to access the validated model record after the data is submitted.
    – Pete
    Commented Apr 13, 2011 at 12:19
  • I have posted update to rails guides which includes usage of with_options love to give back to the community github.com/lifo/docrails/commit/…
    – mpapis
    Commented Apr 16, 2011 at 21:08
0

you should use with_options it allows to put default options into your code:

class User < ActiveRecord::Base
  with_options :if => :is_admin do |admin|
    admin.validates_length_of :password, :minimum => 10
  end
end

in the example is_admin might be an database column, attr_accessor or an method

0

Thank you all for your help.

I've got it working like this:

def after_initialize        
  singleton = class << self; self; end

  validations = eval(calendar.cofig)

  validations.each do |val|
    singleton.class_eval(val)
  end
end
4
  • Does it mean You put all your validations in a database column and record is validated to the data in itself ?
    – mpapis
    Commented Apr 18, 2011 at 16:19
  • Do you mind to post the metadata you put into the database? Have you thought of using another table dedicated to metadata instead of using the same table?
    – user938363
    Commented Nov 9, 2012 at 17:47
  • interesting problem, but your way doesn't work for me.
    – d34n5
    Commented Jun 30, 2014 at 22:06
  • in your example, what does calendar.cofig return?
    – d34n5
    Commented Jul 1, 2014 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.