Module: GroupedValidations
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/grouped_validations.rb,
lib/grouped_validations/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
'0.3.3'
Instance Method Summary collapse
- #_run_group_validation_callbacks(group, context = nil) ⇒ Object
- #grouped_errors(context = nil) ⇒ Object
- #groups_valid?(*groups) ⇒ Boolean (also: #group_valid?)
- #valid?(context = nil) ⇒ Boolean
- #with_validation_context(context) ⇒ Object
Instance Method Details
#_run_group_validation_callbacks(group, context = nil) ⇒ Object
76 77 78 79 80 |
# File 'lib/grouped_validations.rb', line 76 def _run_group_validation_callbacks(group, context=nil) with_validation_context(context) do send(:"_run_validate_#{group}_callbacks") end end |
#grouped_errors(context = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/grouped_validations.rb', line 56 def grouped_errors(context=nil) original_errors = @errors.dup if @errors @errors = nil grouped = {} with_validation_context(context) do _run_validate_callbacks grouped[nil] = errors validation_groups.each do |group| @errors = nil send(:"_run_validate_#{group}_callbacks") grouped[group] = errors end end grouped.values.all?(&:empty?) ? Hash.new { |h,k| {} if validation_groups.include?(k) } : grouped ensure @errors = original_errors end |
#groups_valid?(*groups) ⇒ Boolean Also known as: group_valid?
45 46 47 48 49 50 51 52 53 |
# File 'lib/grouped_validations.rb', line 45 def groups_valid?(*groups) = groups. errors.clear groups.each do |group| raise "Validation group '#{group}' not defined" unless validation_groups.include?(group) _run_group_validation_callbacks(group, [:context]) end errors.empty? end |
#valid?(context = nil) ⇒ Boolean
37 38 39 40 41 42 43 |
# File 'lib/grouped_validations.rb', line 37 def valid?(context=nil) super validation_groups.each do |group| _run_group_validation_callbacks(group, context) end errors.empty? end |
#with_validation_context(context) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/grouped_validations.rb', line 82 def with_validation_context(context) context ||= (persisted? ? :update : :create) current_context, self.validation_context = validation_context, context yield ensure self.validation_context = current_context end |