Hacker News new | ask | show | jobs
by gcao 5209 days ago
Shameless plug here. If you use the Aspector gem I created, you could move validation logic into an aspect and test it. And apply aspect is very easy. Here is what the code will look like.

class Request < ActiveRecord::Base

end

class RequestValidation < Aspector::Base

  target do
    def has_unique_hash_id?
      exists?(:hash_id => hash_id)
    end
  end

  before :create do
    hash_id = SecureRandom.hex(12) until has_unique_hash_id?
  end
end

RequestValidation.apply(Request)