Skip to content

Commit

Permalink
Add syntax to specify association limits as hashes and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Jan 30, 2025
1 parent 6def6e0 commit 58c2564
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
end
```

- Association limits also can be specified as hashes and/or arrays. [@Envek]

```ruby
config.root('Forum', featured: true) do |forum|
forum.limit_associations_size(15, questions: %i[answers votes])
end
```

Which is equivalent to:

```ruby
config.root('Forum', featured: true) do |forum|
forum.limit_associations_size(15, 'forum.questions.answers')
forum.limit_associations_size(15, 'forum.questions.votes')
end
```


- Print reason of association exclusion or inclusion in verbose mode. [@Envek]

- Allow to apply custom scoping to included associations. [@Envek]
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ EvilSeed.configure do |config|
root.limit_associations_size(100)

# Or for certain association only
root.limit_associations_size(10, 'forum.questions')
root.limit_associations_size(5, 'forum.questions')
root.limit_associations_size(15, 'forum.questions.answers')
# or
root.limit_associations_size(5, :questions)
root.limit_associations_size(15, questions: :answers)

# Limit the depth of associations to be dumped from the root level
# All traverses through has_many, belongs_to, etc are counted
Expand Down
35 changes: 22 additions & 13 deletions lib/evil_seed/configuration/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,20 @@ def order(order = nil)

# Limit number of records in all (if pattern is not provided) or given associations to include into dump
# @param limit [Integer] Maximum number of records in associations to include into dump
# @param association_pattern [String, Regex] Pattern to limit number of records for certain associated models
def limit_associations_size(limit, association_pattern = nil)
if association_pattern
@association_limits[association_pattern] = limit
else
@total_limit = limit
# @param association_pattern Array<String, Regex, Hash> Pattern to limit number of records for certain associated models
def limit_associations_size(limit, *association_patterns)
return @total_limit = limit if association_patterns.empty?

association_patterns.each do |pattern|
case pattern
when String, Regexp
@association_limits[pattern] = limit
else
path_prefix = model.constantize.model_name.singular
compile_patterns(pattern, prefix: path_prefix, partial: false).map do |p|
@association_limits[Regexp.new(/\A#{p}\z/)] = limit
end
end
end
end

Expand Down Expand Up @@ -109,22 +117,23 @@ def excluded_optional_belongs_to?

private

def compile_patterns(pattern, prefix: "")
def compile_patterns(pattern, prefix: "", partial: true)
wrap = -> (p) { partial ? "(?:\\.#{p})?" : "\\.#{p}" }
case pattern
when String, Symbol
["#{prefix}(?:\\.#{pattern.to_s})?"]
["#{prefix}#{wrap.(pattern.to_s)}"]
when Regexp
["#{prefix}(?:\\.(?:#{pattern.source}))?"]
["#{prefix}#{wrap.("(?:#{pattern.source})")}"]
when Array
pattern.map { |p| compile_patterns(p, prefix: prefix) }.flatten
pattern.map { |p| compile_patterns(p, prefix: prefix, partial: partial) }.flatten
when Hash
pattern.map do |k, v|
next nil unless v
subpatterns = compile_patterns(v)
next "#{prefix}(?:\\.#{k})?" if subpatterns.empty?
subpatterns = compile_patterns(v, partial: partial)
next "#{prefix}#{wrap.(k)}" if subpatterns.empty?

subpatterns.map do |p|
"#{prefix}(?:\\.#{k}#{p})?"
"#{prefix}#{wrap.("#{k}#{p}")}"
end
end.compact.flatten
when false, nil
Expand Down

0 comments on commit 58c2564

Please sign in to comment.