hummingbird-me/kitsu-server

View on GitHub
app/validators/active_ama_validator.rb

Summary

Maintainability
A
35 mins
Test Coverage
A
100%
class ActiveAMAValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    ama = if value.respond_to?(:post)
            AMA.for_original_post(value.post).first
          elsif value.is_a?(Post)
            AMA.for_original_post(value).first
          else
            value
          end
    return unless ama
    return if ama.author == options[:user]
    return if ama.open?
    record.errors.add(attribute, message: options[:message])
  end
end