Skip to content

Commit 58c2564

Browse files
committed
Add syntax to specify association limits as hashes and arrays
1 parent 6def6e0 commit 58c2564

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
end
3636
```
3737

38+
- Association limits also can be specified as hashes and/or arrays. [@Envek]
39+
40+
```ruby
41+
config.root('Forum', featured: true) do |forum|
42+
forum.limit_associations_size(15, questions: %i[answers votes])
43+
end
44+
```
45+
46+
Which is equivalent to:
47+
48+
```ruby
49+
config.root('Forum', featured: true) do |forum|
50+
forum.limit_associations_size(15, 'forum.questions.answers')
51+
forum.limit_associations_size(15, 'forum.questions.votes')
52+
end
53+
```
54+
55+
3856
- Print reason of association exclusion or inclusion in verbose mode. [@Envek]
3957

4058
- Allow to apply custom scoping to included associations. [@Envek]

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ EvilSeed.configure do |config|
7474
root.limit_associations_size(100)
7575

7676
# Or for certain association only
77-
root.limit_associations_size(10, 'forum.questions')
77+
root.limit_associations_size(5, 'forum.questions')
78+
root.limit_associations_size(15, 'forum.questions.answers')
79+
# or
80+
root.limit_associations_size(5, :questions)
81+
root.limit_associations_size(15, questions: :answers)
7882

7983
# Limit the depth of associations to be dumped from the root level
8084
# All traverses through has_many, belongs_to, etc are counted

lib/evil_seed/configuration/root.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ def order(order = nil)
7272

7373
# Limit number of records in all (if pattern is not provided) or given associations to include into dump
7474
# @param limit [Integer] Maximum number of records in associations to include into dump
75-
# @param association_pattern [String, Regex] Pattern to limit number of records for certain associated models
76-
def limit_associations_size(limit, association_pattern = nil)
77-
if association_pattern
78-
@association_limits[association_pattern] = limit
79-
else
80-
@total_limit = limit
75+
# @param association_pattern Array<String, Regex, Hash> Pattern to limit number of records for certain associated models
76+
def limit_associations_size(limit, *association_patterns)
77+
return @total_limit = limit if association_patterns.empty?
78+
79+
association_patterns.each do |pattern|
80+
case pattern
81+
when String, Regexp
82+
@association_limits[pattern] = limit
83+
else
84+
path_prefix = model.constantize.model_name.singular
85+
compile_patterns(pattern, prefix: path_prefix, partial: false).map do |p|
86+
@association_limits[Regexp.new(/\A#{p}\z/)] = limit
87+
end
88+
end
8189
end
8290
end
8391

@@ -109,22 +117,23 @@ def excluded_optional_belongs_to?
109117

110118
private
111119

112-
def compile_patterns(pattern, prefix: "")
120+
def compile_patterns(pattern, prefix: "", partial: true)
121+
wrap = -> (p) { partial ? "(?:\\.#{p})?" : "\\.#{p}" }
113122
case pattern
114123
when String, Symbol
115-
["#{prefix}(?:\\.#{pattern.to_s})?"]
124+
["#{prefix}#{wrap.(pattern.to_s)}"]
116125
when Regexp
117-
["#{prefix}(?:\\.(?:#{pattern.source}))?"]
126+
["#{prefix}#{wrap.("(?:#{pattern.source})")}"]
118127
when Array
119-
pattern.map { |p| compile_patterns(p, prefix: prefix) }.flatten
128+
pattern.map { |p| compile_patterns(p, prefix: prefix, partial: partial) }.flatten
120129
when Hash
121130
pattern.map do |k, v|
122131
next nil unless v
123-
subpatterns = compile_patterns(v)
124-
next "#{prefix}(?:\\.#{k})?" if subpatterns.empty?
132+
subpatterns = compile_patterns(v, partial: partial)
133+
next "#{prefix}#{wrap.(k)}" if subpatterns.empty?
125134

126135
subpatterns.map do |p|
127-
"#{prefix}(?:\\.#{k}#{p})?"
136+
"#{prefix}#{wrap.("#{k}#{p}")}"
128137
end
129138
end.compact.flatten
130139
when false, nil

0 commit comments

Comments
 (0)