Module: ValidationGroup::ActiveRecord::InstanceMethods

Defined in:
lib/validation_group.rb

Overview

included in every model which calls validation_group

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



42
43
44
45
# File 'lib/validation_group.rb', line 42

def self.included(base)
  # jeffp: add valid?(group = nil), see definition below
  base.prepend ValidWithValidationGroup
end

Instance Method Details

#disable_validation_groupObject



64
65
66
67
68
# File 'lib/validation_group.rb', line 64

def disable_validation_group
  @current_validation_group = nil
  # jeffp: delete fields
  @current_validation_fields = nil
end

#enable_validation_group(group) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/validation_group.rb', line 47

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/validation_group.rb', line 73

def should_validate?(attribute)
	!self.validation_group_enabled? || (@current_validation_fields && @current_validation_fields.include?(attribute.to_sym))
end

#validation_group_enabled?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/validation_group.rb', line 77

def validation_group_enabled?
  respond_to?(:current_validation_group) && !current_validation_group.nil?
end