diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..b502146 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.0.2 diff --git a/Gemfile.lock b/Gemfile.lock index 70b6249..8d4f3d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,43 +6,39 @@ PATH GEM remote: https://rubygems.org/ specs: - codecov (0.1.4) + codecov (0.2.12) json simplecov - url - coderay (1.1.0) - diff-lcs (1.2.5) + coderay (1.1.3) + diff-lcs (1.4.4) docile (1.1.5) - json (1.8.3) - method_source (0.8.2) - pry (0.10.3) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - rake (10.4.2) - rspec (3.3.0) - rspec-core (~> 3.3.0) - rspec-expectations (~> 3.3.0) - rspec-mocks (~> 3.3.0) - rspec-core (3.3.2) - rspec-support (~> 3.3.0) - rspec-expectations (3.3.1) + json (1.8.6) + method_source (1.0.0) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + rake (10.5.0) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-mocks (3.3.2) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-support (3.3.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.3) simplecov (0.10.0) docile (~> 1.1.0) json (~> 1.8) simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) - slop (3.6.0) - url (0.3.2) + simplecov-html (0.10.2) PLATFORMS - ruby + x86_64-darwin-19 DEPENDENCIES action_logic! @@ -53,4 +49,4 @@ DEPENDENCIES simplecov (~> 0.10.0) BUNDLED WITH - 1.10.6 + 2.2.33 diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index 4946808..76e091d 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -5,28 +5,36 @@ module ActionLogic module ActionValidation module ClassMethods + def validates_before!(args) + @validates_before = args.merge(raise_action_logic_exception: true) + end + def validates_before(args) - @validates_before = args + @validates_before = args.merge(raise_action_logic_exception: false) end def validates_after(args) - @validates_after = args + @validates_after = args.merge(raise_action_logic_exception: true) + end + + def validates_around!(args) + @validates_around = args.merge(raise_action_logic_exception: true) end def validates_around(args) - @validates_around = args + @validates_around = args.merge(raise_action_logic_exception: false) end def get_validates_before - @validates_before ||= {} + @validates_before ||= { raise_action_logic_exception: true } end def get_validates_after - @validates_after ||= {} + @validates_after ||= { raise_action_logic_exception: true } end def get_validates_around - @validates_around ||= {} + @validates_around ||= { raise_action_logic_exception: true } end end diff --git a/lib/action_logic/action_validation/attribute_validation.rb b/lib/action_logic/action_validation/attribute_validation.rb index d07a2ba..29bc51b 100644 --- a/lib/action_logic/action_validation/attribute_validation.rb +++ b/lib/action_logic/action_validation/attribute_validation.rb @@ -6,11 +6,15 @@ module ActionValidation class AttributeValidation < BaseValidation def self.validate!(validation_rules, context) + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) existing_attributes = context.to_h.keys - expected_attributes = validation_rules.keys || [] + expected_attributes = tmp_rules.keys || [] missing_attributes = expected_attributes - existing_attributes - raise ActionLogic::MissingAttributeError.new(error_message_format(missing_attributes.join(", ") + " attributes are missing")) if missing_attributes.any? + if raise_exception + raise ActionLogic::MissingAttributeError.new(error_message_format(missing_attributes.join(", ") + " attributes are missing")) if missing_attributes.any? + end end end end diff --git a/lib/action_logic/action_validation/presence_validation.rb b/lib/action_logic/action_validation/presence_validation.rb index ab42c45..b2f55b1 100644 --- a/lib/action_logic/action_validation/presence_validation.rb +++ b/lib/action_logic/action_validation/presence_validation.rb @@ -6,13 +6,17 @@ module ActionValidation class PresenceValidation < BaseValidation def self.validate!(validation_rules, context) - return unless validation_rules.values.find { |expected_validation| expected_validation[:presence] } - errors = presence_errors(validation_rules, context) - raise ActionLogic::PresenceError.new(errors) if errors.any? + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) + return unless tmp_rules.values.find { |expected_validation| expected_validation[:presence] } + errors = presence_errors(tmp_rules, context) + if raise_exception + raise ActionLogic::PresenceError.new(errors) if errors.any? + end end - def self.presence_errors(validation_rules, context) - validation_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| + def self.presence_errors(tmp_rules, context) + tmp_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| next unless expected_validation[:presence] error_collection << error_message(expected_attribute, expected_validation, context) error_collection diff --git a/lib/action_logic/action_validation/type_validation.rb b/lib/action_logic/action_validation/type_validation.rb index 094218d..c5773ed 100644 --- a/lib/action_logic/action_validation/type_validation.rb +++ b/lib/action_logic/action_validation/type_validation.rb @@ -6,9 +6,11 @@ module ActionValidation class TypeValidation < BaseValidation def self.validate!(validation_rules, context) - return unless validation_rules.values.find { |expected_validation| expected_validation[:type] } + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) + return unless tmp_rules.values.find { |expected_validation| expected_validation[:type] } - type_errors = validation_rules.reduce([]) do |collection, (expected_attribute, expected_validation)| + type_errors = tmp_rules.reduce([]) do |collection, (expected_attribute, expected_validation)| next unless expected_validation[:type] if context.to_h[expected_attribute].class != expected_validation[:type] @@ -17,7 +19,9 @@ def self.validate!(validation_rules, context) collection end - raise ActionLogic::AttributeTypeError.new(error_message_format(type_errors.join(", "))) if type_errors.any? + if raise_exception + raise ActionLogic::AttributeTypeError.new(error_message_format(type_errors.join(", "))) if type_errors.any? + end end end end diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index 60c9e79..69357d0 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -22,17 +22,35 @@ module ActionLogic describe "around validations" do describe "required attributes and type validation" do + it "does not have errors if context has required keys and values are of the correct type" do + result = ValidateAroundTestTask.execute(Constants::VALID_ATTRIBUTES) + expect(result.message).to be_nil + expect(result.success?).to be_truthy + end + + it "does not raise error if context is missing required keys" do + expect { ValidateAroundTestTask.execute() }.to_not\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "raises error if context has required keys but values are not of correct type" do + expect { ValidateAroundTestTask.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + + describe "required attributes and type validation with bang" do it "does not raise error if context has required keys and values are of the correct type" do - expect { ValidateAroundTestTask.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + expect { ValidateAroundTestTaskWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end it "raises error if context is missing required keys" do - expect { ValidateAroundTestTask.execute() }.to\ + expect { ValidateAroundTestTaskWithBang.execute() }.to\ raise_error(ActionLogic::MissingAttributeError) end it "raises error if context has required keys but values are not of correct type" do - expect { ValidateAroundTestTask.execute(Constants::INVALID_ATTRIBUTES) }.to\ + expect { ValidateAroundTestTaskWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ raise_error(ActionLogic::AttributeTypeError) end end @@ -42,8 +60,19 @@ module ActionLogic expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType1.new) }.to_not raise_error end + it "doesn't raise error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType2.new) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + + describe "custom types with bang" do + it "allows validation against custom defined types" do + expect { ValidateAroundCustomTypeTestTaskWithBang.execute(:custom_type => CustomType1.new) }.to_not raise_error + end + it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType2.new) }.to\ + expect { ValidateAroundCustomTypeTestTaskWithBang.execute(:custom_type => CustomType2.new) }.to\ raise_error(ActionLogic::AttributeTypeError) end end @@ -53,8 +82,19 @@ module ActionLogic expect { ValidateAroundPresenceTestTask.execute(:integer_test => 1) }.to_not raise_error end + it "doesn't raise error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestTask.execute(:integer_test => nil) }.to_not\ + raise_error(ActionLogic::PresenceError) + end + end + + describe "presence with bang" do + it "validates presence if presence is specified" do + expect { ValidateAroundPresenceTestTaskWithBang.execute(:integer_test => 1) }.to_not raise_error + end + it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateAroundPresenceTestTask.execute(:integer_test => nil) }.to\ + expect { ValidateAroundPresenceTestTaskWithBang.execute(:integer_test => nil) }.to\ raise_error(ActionLogic::PresenceError) end end @@ -64,8 +104,8 @@ module ActionLogic expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => [1]) }.to_not raise_error end - it "raises error if custom presence validation is not satisfied" do - expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => []) }.to\ + it "doesn't raise error if custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => []) }.to_not\ raise_error(ActionLogic::PresenceError) end @@ -74,6 +114,22 @@ module ActionLogic raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end + + describe "custom presence with bang" do + it "allows custom presence validation if custom presence is defined" do + expect { ValidateAroundCustomPresenceTestTaskWithBang.execute(:array_test => [1]) }.to_not raise_error + end + + it "raises error if custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestTaskWithBang.execute(:array_test => []) }.to\ + raise_error(ActionLogic::PresenceError) + end + + it "raises error if custom presence validation is not supported" do + expect { ValidateAroundUnrecognizablePresenceTestTaskWithBang.execute(:integer_test => 1) }.to\ + raise_error(ActionLogic::UnrecognizablePresenceValidatorError) + end + end end describe "before validations" do diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 004df10..6f31954 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -48,13 +48,29 @@ module ActionLogic expect { ValidateAroundTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end + it "doesn't raise error if context is missing required keys" do + expect { ValidateAroundTestUseCase.execute() }.to_not\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "doesn't raise error if context has required keys but values are not of correct type" do + expect { ValidateAroundTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + + describe "required attributes and type validation with bang" do + it "does not raise error if context has required keys and values are of the correct type" do + expect { ValidateAroundTestUseCaseWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + end + it "raises error if context is missing required keys" do - expect { ValidateAroundTestUseCase.execute() }.to\ + expect { ValidateAroundTestUseCaseWithBang.execute() }.to\ raise_error(ActionLogic::MissingAttributeError) end it "raises error if context has required keys but values are not of correct type" do - expect { ValidateAroundTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to\ + expect { ValidateAroundTestUseCaseWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ raise_error(ActionLogic::AttributeTypeError) end end @@ -64,8 +80,19 @@ module ActionLogic expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType1.new) }.to_not raise_error end + it "doesn't raise error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType2.new) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + + describe "custom types with bang" do + it "allows validation against custom defined types" do + expect { ValidateAroundCustomTypeTestUseCaseWithBang.execute(:custom_type => CustomType1.new) }.to_not raise_error + end + it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType2.new) }.to\ + expect { ValidateAroundCustomTypeTestUseCaseWithBang.execute(:custom_type => CustomType2.new) }.to\ raise_error(ActionLogic::AttributeTypeError) end end @@ -75,8 +102,19 @@ module ActionLogic expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error end + it "doesn't raise error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => nil) }.to_not\ + raise_error(ActionLogic::PresenceError) + end + end + + describe "presence with bang" do + it "validates presence if presence is specified" do + expect { ValidateAroundPresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to_not raise_error + end + it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => nil) }.to\ + expect { ValidateAroundPresenceTestUseCaseWithBang.execute(:integer_test => nil) }.to\ raise_error(ActionLogic::PresenceError) end end @@ -86,8 +124,8 @@ module ActionLogic expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error end - it "raises error if custom presence validation is not satisfied" do - expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => []) }.to\ + it "doesn't raise error when custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => []) }.to_not\ raise_error(ActionLogic::PresenceError) end @@ -96,59 +134,92 @@ module ActionLogic raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end + + describe "custom presence with bang" do + it "allows custom presence validation if custom presence is defined" do + expect { ValidateAroundCustomPresenceTestUseCaseWithBang.execute(:array_test => [1]) }.to_not raise_error + end + + it "raises error when custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestUseCaseWithBang.execute(:array_test => []) }.to\ + raise_error(ActionLogic::PresenceError) + end + + it "raises error if custom presence validation is not supported" do + expect { ValidateAroundUnrecognizablePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to\ + raise_error(ActionLogic::UnrecognizablePresenceValidatorError) + end + end end describe "before validations" do + describe "required attributes and type validation with bang" do + it "does not raise error if context has required keys and values are of the correct type" do + expect { ValidateBeforeTestUseCaseWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + end + + it "raises error if context is missing required keys" do + expect { ValidateBeforeTestUseCaseWithBang.execute() }.to\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "raises error if context has required key but is not of correct type" do + expect { ValidateBeforeTestUseCaseWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ + raise_error(ActionLogic::AttributeTypeError) + end + end + describe "required attributes and type validation" do it "does not raise error if context has required keys and values are of the correct type" do expect { ValidateBeforeTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end - it "raises error if context is missing required keys" do - expect { ValidateBeforeTestUseCase.execute() }.to\ + it "doesn't raise error if context is missing required keys" do + expect { ValidateBeforeTestUseCase.execute() }.to_not\ raise_error(ActionLogic::MissingAttributeError) end it "raises error if context has required key but is not of correct type" do - expect { ValidateBeforeTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to\ + expect { ValidateBeforeTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ raise_error(ActionLogic::AttributeTypeError) end end + describe "custom types" do it "allows validation against custom defined types" do - expect { ValidateBeforeCustomTypeTestUseCase.execute(Constants::CUSTOM_TYPE_ATTRIBUTES1) }.to_not raise_error + expect { ValidateBeforeCustomTypeTestUseCaseWithBang.execute(Constants::CUSTOM_TYPE_ATTRIBUTES1) }.to_not raise_error end it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateBeforeCustomTypeTestUseCase.execute(Constants::CUSTOM_TYPE_ATTRIBUTES2) }.to\ + expect { ValidateBeforeCustomTypeTestUseCaseWithBang.execute(Constants::CUSTOM_TYPE_ATTRIBUTES2) }.to\ raise_error(ActionLogic::AttributeTypeError) end end describe "presence" do it "validates presence if presence is specified" do - expect { ValidateBeforePresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error + expect { ValidateBeforePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to_not raise_error end it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateBeforePresenceTestUseCase.execute(:integer_test => nil) }.to\ + expect { ValidateBeforePresenceTestUseCaseWithBang.execute(:integer_test => nil) }.to\ raise_error(ActionLogic::PresenceError) end end describe "custom presence" do it "allows custom presence validation if custom presence is defined" do - expect { ValidateBeforeCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error + expect { ValidateBeforeCustomPresenceTestUseCaseWithBang.execute(:array_test => [1]) }.to_not raise_error end it "raises error if custom presence validation is not satisfied" do - expect { ValidateBeforeCustomPresenceTestUseCase.execute(:array_test => []) }.to\ + expect { ValidateBeforeCustomPresenceTestUseCaseWithBang.execute(:array_test => []) }.to\ raise_error(ActionLogic::PresenceError) end it "raises error if custom presence validation is not supported" do - expect { ValidateBeforeUnrecognizablePresenceTestUseCase.execute(:integer_test => 1) }.to\ + expect { ValidateBeforeUnrecognizablePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to\ raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end diff --git a/spec/action_logic/configuration_spec.rb b/spec/action_logic/configuration_spec.rb index e76a2a3..b753c0d 100644 --- a/spec/action_logic/configuration_spec.rb +++ b/spec/action_logic/configuration_spec.rb @@ -63,7 +63,7 @@ class CustomFormatter; end end it "uses a custom benchmark handler if one is provided" do - custom_benchmark_handler = -> { yield } + custom_benchmark_handler = -> { } described_class.configure do |config| config.benchmark_handler = custom_benchmark_handler diff --git a/spec/fixtures/coordinators.rb b/spec/fixtures/coordinators.rb index ac82e90..5d6c0c2 100644 --- a/spec/fixtures/coordinators.rb +++ b/spec/fixtures/coordinators.rb @@ -61,7 +61,7 @@ def plan class ValidateBeforeTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call end @@ -80,7 +80,7 @@ def plan class ValidateBeforeCustomTypeTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::CUSTOM_TYPE_VALIDATION1 + validates_before! Constants::CUSTOM_TYPE_VALIDATION1 def call end @@ -99,7 +99,7 @@ def plan class ValidateBeforePresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::PRESENCE_VALIDATION + validates_before! Constants::PRESENCE_VALIDATION def call end @@ -118,7 +118,7 @@ def plan class ValidateBeforeCustomPresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::CUSTOM_PRESENCE_VALIDATION + validates_before! Constants::CUSTOM_PRESENCE_VALIDATION def call end @@ -137,7 +137,7 @@ def plan class ValidateBeforeUnrecognizablePresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before :integer_test => { :presence => :true } + validates_before! :integer_test => { :presence => :true } def call end diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index b858ac5..efe33bb 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -10,6 +10,15 @@ def call end end +class ValidateAroundTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! Constants::ALL_VALIDATIONS + + def call + end +end + class ValidateAroundTestTask include ActionLogic::ActionTask @@ -19,6 +28,15 @@ def call end end +class ValidateAroundCustomTypeTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :custom_type => { :type => CustomType1, :presence => true } + + def call + end +end + class ValidateAroundCustomTypeTestTask include ActionLogic::ActionTask @@ -28,6 +46,15 @@ def call end end +class ValidateAroundUnrecognizablePresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :integer_test => { :presence => :true } + + def call + end +end + class ValidateAroundUnrecognizablePresenceTestTask include ActionLogic::ActionTask @@ -37,6 +64,15 @@ def call end end +class ValidateAroundPresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :integer_test => { :presence => true } + + def call + end +end + class ValidateAroundPresenceTestTask include ActionLogic::ActionTask @@ -46,6 +82,19 @@ def call end end +class ValidateAroundCustomPresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :array_test => { :presence => ->(array_test) { array_test.any? } } + + def call + end + + def tasks + [] + end +end + class ValidateAroundCustomPresenceTestTask include ActionLogic::ActionTask @@ -62,7 +111,7 @@ def tasks class ValidateBeforeTestTask include ActionLogic::ActionTask - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call end @@ -71,7 +120,7 @@ def call class ValidateBeforeCustomTypeTestTask include ActionLogic::ActionTask - validates_before :custom_type => { :type => CustomType1, :presence => true } + validates_before! :custom_type => { :type => CustomType1, :presence => true } def call end @@ -80,7 +129,7 @@ def call class ValidateBeforeUnrecognizablePresenceTestTask include ActionLogic::ActionTask - validates_before :integer_test => { :presence => :true } + validates_before! :integer_test => { :presence => :true } def call end @@ -89,7 +138,7 @@ def call class ValidateBeforePresenceTestTask include ActionLogic::ActionTask - validates_before :integer_test => { :presence => true } + validates_before! :integer_test => { :presence => true } def call end @@ -98,7 +147,7 @@ def call class ValidateBeforeCustomPresenceTestTask include ActionLogic::ActionTask - validates_before :array_test => { :presence => ->(array_test) { array_test.any? } } + validates_before! :array_test => { :presence => ->(array_test) { array_test.any? } } def call end @@ -236,7 +285,7 @@ def error(e) class ErrorHandlerInvalidAttributesBeforeTestTask include ActionLogic::ActionTask - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call raise diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index b9fe6e7..d1f6feb 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -63,6 +63,20 @@ def tasks end end +class ValidateAroundTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! Constants::ALL_VALIDATIONS + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundCustomTypeTestUseCase include ActionLogic::ActionUseCase @@ -77,6 +91,34 @@ def tasks end end +class ValidateAroundCustomTypeTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :custom_type => { :type => CustomType1, :presence => true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateAroundUnrecognizablePresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :integer_test => { :presence => :true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundUnrecognizablePresenceTestUseCase include ActionLogic::ActionUseCase @@ -91,6 +133,20 @@ def tasks end end +class ValidateAroundPresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :integer_test => { :presence => true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundPresenceTestUseCase include ActionLogic::ActionUseCase @@ -105,6 +161,20 @@ def tasks end end +class ValidateAroundCustomPresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :array_test => { :presence => ->(array_test) { array_test.any? } } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundCustomPresenceTestUseCase include ActionLogic::ActionUseCase @@ -189,6 +259,76 @@ def tasks end end +class ValidateBeforeTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_before! Constants::ALL_VALIDATIONS + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforePresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_before! Constants::PRESENCE_VALIDATION + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeCustomPresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_before! Constants::CUSTOM_PRESENCE_VALIDATION + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeCustomTypeTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_before! Constants::CUSTOM_TYPE_VALIDATION1 + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeUnrecognizablePresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_before :integer_test => { :presence => :true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAfterTestUseCase include ActionLogic::ActionUseCase