Common Validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/student.rb | |
class Student < ActiveRecord::Base | |
include CommonValidations::StudentTeacherValidations | |
end | |
# app/models/teacher.rb | |
class Teacher < ActiveRecord::Base | |
include CommonValidations::StudentTeacherValidations | |
end | |
# lib/common_validations/student_teacher_validations.rb | |
module CommonValidations | |
module StudentTeacherValidations | |
def self.included(klass) | |
klass.validate :foo | |
end | |
def foo | |
class_name = self.class.class_name | |
errors.add("name", "must be the class name: #{class_name}") unless name == class_name | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment