Class: ForeignKeyValidation::Validator
- Inherits:
-
Object
- Object
- ForeignKeyValidation::Validator
- Defined in:
- lib/foreign_key_validation/validator.rb
Constant Summary collapse
- DEFAULT_VALIDATE_AGAINST =
:user
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#reflection_names ⇒ Object
Returns the value of attribute reflection_names.
-
#reflections ⇒ Object
Returns the value of attribute reflections.
-
#validate_against ⇒ Object
Returns the value of attribute validate_against.
-
#validate_against_key ⇒ Object
Returns the value of attribute validate_against_key.
-
#validate_with ⇒ Object
Returns the value of attribute validate_with.
Instance Method Summary collapse
- #check ⇒ Object
- #filter_method_name ⇒ Object
-
#initialize(klass, opt = {}) ⇒ Validator
constructor
A new instance of Validator.
Constructor Details
#initialize(klass, opt = {}) ⇒ Validator
Returns a new instance of Validator.
8 9 10 11 12 13 14 15 |
# File 'lib/foreign_key_validation/validator.rb', line 8 def initialize(klass, opt={}) self.klass = klass self.validate_against = find_validate_against(opt[:on]) self.reflections = klass.reflect_on_all_associations(:belongs_to) self.reflection_names = reflections.map(&:name).map(&:to_s) self.validate_against_key = find_validate_against_key self.validate_with = find_validate_with(opt[:with]) end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def klass @klass end |
#reflection_names ⇒ Object
Returns the value of attribute reflection_names.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def reflection_names @reflection_names end |
#reflections ⇒ Object
Returns the value of attribute reflections.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def reflections @reflections end |
#validate_against ⇒ Object
Returns the value of attribute validate_against.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def validate_against @validate_against end |
#validate_against_key ⇒ Object
Returns the value of attribute validate_against_key.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def validate_against_key @validate_against_key end |
#validate_with ⇒ Object
Returns the value of attribute validate_with.
4 5 6 |
# File 'lib/foreign_key_validation/validator.rb', line 4 def validate_with @validate_with end |
Instance Method Details
#check ⇒ Object
17 18 19 20 21 |
# File 'lib/foreign_key_validation/validator.rb', line 17 def check raise Errors::NoReleationFoundError.new(klass.name) if reflection_names.empty? raise Errors::NoForeignKeyFoundError.new(validate_against, klass.table_name) unless reflection_names.include?(validate_against) raise Errors::UnknownRelationError.new(validate_with) unless validate_with.all? {|k| reflection_names.include?(k)} end |
#filter_method_name ⇒ Object
23 24 25 |
# File 'lib/foreign_key_validation/validator.rb', line 23 def filter_method_name "validate_foreign_keys_on_#{validate_against}" end |