From dde6efbc5177bb92847ea638c8fb761ad47cbda3 Mon Sep 17 00:00:00 2001 From: Shaun Carlson Date: Wed, 29 May 2024 21:16:27 -1000 Subject: [PATCH] fix unexpected behavior around filters with validators --- lib/active_interaction/base.rb | 1 + lib/active_interaction/modules/validation.rb | 6 +++--- spec/active_interaction/base_spec.rb | 18 ++++++++++++++++++ .../modules/validation_spec.rb | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb index f3d5b2e0..96d06a04 100644 --- a/lib/active_interaction/base.rb +++ b/lib/active_interaction/base.rb @@ -205,6 +205,7 @@ def run_validations! filter super if errors.empty? + errors.empty? end private diff --git a/lib/active_interaction/modules/validation.rb b/lib/active_interaction/modules/validation.rb index 22b31f0e..6fbca89a 100644 --- a/lib/active_interaction/modules/validation.rb +++ b/lib/active_interaction/modules/validation.rb @@ -8,10 +8,10 @@ module Validation class << self # @param context [Base] # @param filters [Hash{Symbol => Filter}] - # @param inputs [Inputs] - def validate(context, filters, inputs) + # @param _inputs [Inputs] + def validate(context, filters, _inputs) filters.each_with_object([]) do |(name, filter), errors| - input = filter.process(inputs[name], context) + input = filter.process(context.send(name), context) input.errors.each do |error| errors << [error.name, error.type, error.options] diff --git a/spec/active_interaction/base_spec.rb b/spec/active_interaction/base_spec.rb index 361a8be3..13197909 100644 --- a/spec/active_interaction/base_spec.rb +++ b/spec/active_interaction/base_spec.rb @@ -5,6 +5,11 @@ float :thing end +InteractionWithFilterAndValidator = Class.new(TestInteraction) do + string :thing + validates :thing, presence: true +end + InteractionWithDateFilter = Class.new(TestInteraction) do date :thing end @@ -105,6 +110,19 @@ def execute expect(interaction.thing).to eql Date.new(year, month, day) end end + + context 'and a validator' do + let(:described_class) { InteractionWithFilterAndValidator } + + it 'returns false on validation with no value given' do + expect(interaction.valid?).to be false + end + + it 'passes validation when value is given later' do + interaction.thing = 'potato' + expect(interaction).to be_valid + end + end end end diff --git a/spec/active_interaction/modules/validation_spec.rb b/spec/active_interaction/modules/validation_spec.rb index 8cd0219e..cee5d70b 100644 --- a/spec/active_interaction/modules/validation_spec.rb +++ b/spec/active_interaction/modules/validation_spec.rb @@ -4,7 +4,7 @@ let(:filter) { ActiveInteraction::Filter.new(:name, {}) } let(:interaction) do name = filter.name - klass = Class.new(ActiveInteraction::Base) { attr_writer(name) } + klass = Class.new(ActiveInteraction::Base) { attr_accessor(name) } klass.filters[name] = filter klass.new end