From b63537f5e09c4724c72ff8cf6b4d66285339e70b Mon Sep 17 00:00:00 2001 From: Miller Date: Sat, 15 Apr 2023 18:51:22 +0900 Subject: [PATCH 1/2] Provide more fluent interface to handle boolean filter --- lib/active_interaction/base.rb | 3 +++ .../integration/boolean_interaction_spec.rb | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb index 04386179..22f6ae4d 100644 --- a/lib/active_interaction/base.rb +++ b/lib/active_interaction/base.rb @@ -146,6 +146,9 @@ def initialize_filter(filter) filters[attribute] = filter attr_accessor attribute + if filter.is_a?(BooleanFilter) + alias_method "#{attribute}?", attribute + end eagerly_evaluate_default(filter) end diff --git a/spec/active_interaction/integration/boolean_interaction_spec.rb b/spec/active_interaction/integration/boolean_interaction_spec.rb index 35078a0f..3517db7f 100644 --- a/spec/active_interaction/integration/boolean_interaction_spec.rb +++ b/spec/active_interaction/integration/boolean_interaction_spec.rb @@ -1,3 +1,11 @@ -RSpec.describe 'BooleanInteraction' do +BooleanInteraction = Class.new(TestInteraction) do + boolean :x +end + +RSpec.describe BooleanInteraction do it_behaves_like 'an interaction', :boolean, -> { [false, true].sample } + + it "responds to #x?" do + expect(described_class.new).to respond_to(:x?) + end end From 97ad1950acc0caecdd268728a46f43ede6e260c4 Mon Sep 17 00:00:00 2001 From: Miller Date: Thu, 20 Apr 2023 23:32:46 +0900 Subject: [PATCH 2/2] Lint --- lib/active_interaction/base.rb | 5 ++--- .../integration/boolean_interaction_spec.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb index 22f6ae4d..f3d5b2e0 100644 --- a/lib/active_interaction/base.rb +++ b/lib/active_interaction/base.rb @@ -146,9 +146,8 @@ def initialize_filter(filter) filters[attribute] = filter attr_accessor attribute - if filter.is_a?(BooleanFilter) - alias_method "#{attribute}?", attribute - end + + alias_method "#{attribute}?", attribute if filter.is_a?(BooleanFilter) eagerly_evaluate_default(filter) end diff --git a/spec/active_interaction/integration/boolean_interaction_spec.rb b/spec/active_interaction/integration/boolean_interaction_spec.rb index 3517db7f..1ec891d1 100644 --- a/spec/active_interaction/integration/boolean_interaction_spec.rb +++ b/spec/active_interaction/integration/boolean_interaction_spec.rb @@ -5,7 +5,7 @@ RSpec.describe BooleanInteraction do it_behaves_like 'an interaction', :boolean, -> { [false, true].sample } - it "responds to #x?" do + it 'responds to #x?' do expect(described_class.new).to respond_to(:x?) end end