Skip to content

Commit

Permalink
Change association behavior to return nils if records cannot be found (
Browse files Browse the repository at this point in the history
…#60)

* change association behavior to return nils if records cannot be found

* make find_by() behavior optional
  • Loading branch information
mnelson authored May 17, 2024
1 parent 487453d commit 2f54d06
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Subroutine 4.0.1

Association fields can now use `find_by()` instead of `find_by!()` by passing a `raise_on_miss: false` option. This places the responsibility on the op to manage nil cases rather than handling RecordNotFound errors.

## Subroutine 4.0

The `Subroutine::Fields` module now contains a class_attribute that allows the altering of param accessor behaviors. `include_defaults_in_params` is now available to opt into including the default values in usage of the `all_params (alias params)` method. Backwards compatibility is preserved by defaulting the value to `false`. If switched to true, when an input is omitted and the field is configured with a default value, it will be included in the `all_params` object.
Expand Down
6 changes: 5 additions & 1 deletion lib/subroutine/association_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ def fetch_association_instance(config)
scope = klass.all
scope = scope.unscoped if config.unscoped?

scope.find_by!(config.find_by => value)
if config.raise_on_miss?
scope.find_by!(config.find_by => value)
else
scope.find_by(config.find_by => value)
end
end

def maybe_raise_on_association_type_mismatch!(config, record)
Expand Down
4 changes: 4 additions & 0 deletions lib/subroutine/association_fields/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def polymorphic?
!!config[:polymorphic]
end

def raise_on_miss?
config[:raise_on_miss] != false
end

def as
config[:as] || field_name
end
Expand Down
4 changes: 2 additions & 2 deletions lib/subroutine/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Subroutine

MAJOR = 4
MINOR = 0
PATCH = 0
PRE = "alpha"
PATCH = 1
PRE = nil

VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")

Expand Down
10 changes: 10 additions & 0 deletions test/subroutine/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,15 @@ def test_groups_are_preserved_to_association_components
assert_equal({}, op.without_info_params)
end

def test_find_by_is_used_if_raise_on_miss_is_false
all_mock = mock

::User.expects(:all).returns(all_mock)
all_mock.expects(:find_by).with(id: 1).returns(nil)

op = SafeAssociationOp.new user_type: "User", user_id: 1
assert_nil op.user
end

end
end
10 changes: 6 additions & 4 deletions test/support/ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def self.find_by(params)
new(params)
end

def self.find_by!(params)
find_by(params) || raise
end

def self.type_for_attribute(attribute)
case attribute
when :id
Expand Down Expand Up @@ -342,6 +338,12 @@ class SimpleAssociationOp < ::OpWithAssociation

end

class SafeAssociationOp < ::OpWithAssociation

association :user, raise_on_miss: false

end

class SimpleAssociationWithStringIdOp < ::OpWithAssociation

association :string_id_user
Expand Down

0 comments on commit 2f54d06

Please sign in to comment.