Module: ValidationGroup::ActiveRecord::InstanceMethods
- Defined in:
- lib/validation_group.rb
Overview
included in every model which calls validation_group
Instance Method Summary collapse
- #disable_validation_group ⇒ Object
- #enable_validation_group(group) ⇒ Object
-
#should_validate?(attribute) ⇒ Boolean
jeffp: optimizer for someone writing custom :validate method – no need to validate fields outside the current validation group note: could also use in validation modules to improve performance.
-
#valid_with_validation_group?(group = nil) ⇒ Boolean
eliminates need to use :enable_validation_group before :valid? call – nice.
- #validation_group_enabled? ⇒ Boolean
Instance Method Details
#disable_validation_group ⇒ Object
61 62 63 64 65 |
# File 'lib/validation_group.rb', line 61 def disable_validation_group @current_validation_group = nil # jeffp: delete fields @current_validation_fields = nil end |
#enable_validation_group(group) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/validation_group.rb', line 44 def enable_validation_group(group) # Check if given validation group is defined for current class or one of # its ancestors group_classes = self.class.validation_group_classes found = ValidationGroup::Util.current_and_ancestors(self.class). find do |klass| group_classes[klass] && group_classes[klass].include?(group) end if found @current_validation_group = group # jeffp: capture current fields for performance optimization @current_validation_fields = group_classes[found][group] else raise ArgumentError, "No validation group of name :#{group}" end end |
#should_validate?(attribute) ⇒ Boolean
jeffp: optimizer for someone writing custom :validate method – no need to validate fields outside the current validation group note: could also use in validation modules to improve performance
70 71 72 |
# File 'lib/validation_group.rb', line 70 def should_validate?(attribute) !self.validation_group_enabled? || (@current_validation_fields && @current_validation_fields.include?(attribute.to_sym)) end |
#valid_with_validation_group?(group = nil) ⇒ Boolean
eliminates need to use :enable_validation_group before :valid? call – nice
80 81 82 83 |
# File 'lib/validation_group.rb', line 80 def valid_with_validation_group?(group=nil) self.enable_validation_group(group) if group valid_without_validation_group? end |
#validation_group_enabled? ⇒ Boolean
74 75 76 |
# File 'lib/validation_group.rb', line 74 def validation_group_enabled? respond_to?(:current_validation_group) && !current_validation_group.nil? end |