views:

21

answers:

2

In modern versions of ActiveRecord you can define any number of before_validation handlers using a simple declaration:

class MyModel < ActiveRecord::Base
  before_validation :do_something
  before_validation :do_something_else
end

Using Sequel it looks like the only way you can do this is similar to the older ActiveRecord method:

class MyModel < Sequel::Model
  def before_validation
    super

    do_something
    do_something_else
  end
end

Is there a plugin that adds the simplified declarations, or is that just how it is? The Sequel documentation doesn't paint a very clear picture. There is no MyModel.before_validation class method defined.

Update: As the answers below indicate, this behavior is not present by default. I've made a Sequel::Model plugin that fixes this called sequel_simple_callbacks

+1  A: 

It looks like Sequel has a plugin, validation_class_methods, which provide a class-method based interface for adding validations. According to the validation documentation it is intended only for legacy compatibility, but if you prefer that interface, it should be fine to use it.

Brian Campbell
That's for the validations themselves, which is a nice thing to have, but I'm talking about the defined "hooks": before_X, after_X which aren't covered in that plugin.
tadman
I don't see anything in the documentation that allow you to add such hooks on the class level. You could write such a thing yourself, as a plugin, but I don't think they provide exactly what you're looking for, as they seem to prefer making you just define an instance method.
Brian Campbell
I was hoping that wasn't the case. Re-defining the instance method seems like a step back to Rails 1.
tadman
+1  A: 

As Brian Campbell mentioned, you can use the validation_class_methods plugin. It's not recommended, but it is supported. The validation_helpers plugin gives you similar helpers that the validation_class_methods plugin gives you, for use as instance methods inside the validate method.

Writing validations as instance methods is a better approach, IMO, which is why Sequel encourages it. Validations are inherently an instance level activity, not a class level activity. With the class level validations, handling things like conditional validation has to have special syntax (like :if=>proc{}), where you can just use normal conditional ruby statements in the instance method (such as if, else, case, etc.).

Obviously, if you are coming from ActiveRecord, it's a different approach. Sequel does not attempt to clone ActiveRecord's behavior, though the two libraries operate similarly in some respects.

Jeremy Evans
ActiveRecord calls them "callbacks", Sequel calls them "hooks" but they're those `after_` and `before_` class methods like `after_update` and `before_save` which are at least named the same thing in both camps. That plugin makes validations more flexible, but does not add the ActiveRecord-style methods for callbacks/hooks, just gives you finer grained control over your validation methods ActiveRecord-style.
tadman
You can use the `hook_class_methods` plugin for a similar interface for to ActiveRecord for hooks/callbacks. I should have read your example code more closely, since that plugin would pretty much work directly for your first example.
Jeremy Evans