Skip to content

Commit

Permalink
Allow to exclude all has_many and has_one associations by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Nov 1, 2024
1 parent e1c5868 commit 2114fcd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Options to exclude all `has_many` and `has_one` or optional `belongs_to` associations by default. [@Envek]

```ruby
root.exclude_has_relations
root.exclude_optional_belongs_to
```

Excluded associations can be re-included by `include` with matching pattern.

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

### Fixed
Expand Down
16 changes: 16 additions & 0 deletions lib/evil_seed/configuration/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def include(*association_patterns)
@inclusions += association_patterns
end

def exclude_has_relations
@excluded_has_relations = :exclude_has_relations
end

def exclude_optional_belongs_to
@excluded_optional_belongs_to = :exclude_optional_belongs_to
end

# 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
Expand Down Expand Up @@ -60,6 +68,14 @@ def excluded?(association_path)
def included?(association_path)
inclusions.find { |inclusion| association_path.match(inclusion) } #.match(association_path) }
end

def excluded_has_relations?
@excluded_has_relations
end

def excluded_optional_belongs_to?
@excluded_optional_belongs_to
end
end
end
end
4 changes: 3 additions & 1 deletion lib/evil_seed/relation_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def setup_belongs_to_reflections
model_class.reflect_on_all_associations(:belongs_to).reject do |reflection|
next false if reflection.options[:polymorphic] # TODO: Add support for polymorphic belongs_to
included = root.included?("#{association_path}.#{reflection.name}")
excluded = root.excluded?("#{association_path}.#{reflection.name}")
excluded = reflection.options[:optional] && root.excluded_optional_belongs_to?
excluded ||= root.excluded?("#{association_path}.#{reflection.name}")
inverse = reflection.name == inverse_reflection
puts " -- belongs_to #{reflection.name} #{"excluded by #{excluded}" if excluded} #{"re-included by #{included}" if included}" if verbose
if excluded and not included
Expand Down Expand Up @@ -200,6 +201,7 @@ def setup_has_many_reflections

included = root.included?("#{association_path}.#{reflection.name}")
excluded = :inverse if reflection.name == inverse_reflection
excluded ||= root.excluded_has_relations?
excluded ||= root.excluded?("#{association_path}.#{reflection.name}")
puts " -- #{reflection.macro} #{reflection.name} #{"excluded by #{excluded}" if excluded} #{"re-included by #{included}" if included}" if verbose
!(excluded and not included)
Expand Down

0 comments on commit 2114fcd

Please sign in to comment.