2

I have an ActiveModel class whose instances should only be valid if they have been touched. The following code works:

class Base
  include ActiveModel::Model
  validates :touched?, inclusion: { in: [true] }

  def update(params = {})
    initialize(params)
    @touched = true
  end

  def touched?
    !!@touched
  end
end

But I don't like it. It's not nice. I'd like to write something like that:

validates: touched?, equality: true

Is there a better/shorter way of writing that without using a custom validator? Would be nice if it worked also for values other than booleans.

schmijos
  • 8,114
  • 3
  • 50
  • 58

1 Answers1

5
validates :touched?,  acceptance: true
Sergei Stralenia
  • 855
  • 7
  • 16