Skip to content

Commit 810b881

Browse files
committed
Add filter for ActiveRecord::Relation to stop preloading
1 parent d29f1c2 commit 810b881

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/active_interaction/extras.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def include_errors!(model, **mapping)
111111
end
112112

113113
require 'active_interaction/extras/filters/anything_filter'
114+
require 'active_interaction/extras/filters/ar_relation_filter'
114115
require 'active_interaction/extras/filters/uuid_filter'
115116

116117
I18n.load_path.unshift(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
class ActiveInteraction::Extras::Filters::ArRelationFilter < ActiveInteraction::ObjectFilter
4+
register :ar_relation
5+
6+
def initialize(...)
7+
super
8+
@options.reverse_merge!(class: ActiveRecord::Relation)
9+
end
10+
11+
def matches?(value)
12+
# value == nil triggers active record loading
13+
if value.nil? && default?
14+
# as per v5 there is no way to know if value of `nil` is given by caller or
15+
# is a default value when it's missing. We want to maintain standard default
16+
# behaviour when value is `nil`. Returning `false` will trigger default value
17+
false
18+
else
19+
true
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)