From 2114fcd0a1702240b85ff957f182a442ebc5726b Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Fri, 1 Nov 2024 23:13:54 +0900 Subject: [PATCH] Allow to exclude all `has_many` and `has_one` associations by default --- CHANGELOG.md | 9 +++++++++ lib/evil_seed/configuration/root.rb | 16 ++++++++++++++++ lib/evil_seed/relation_dumper.rb | 4 +++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0f77b1..9486c11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/evil_seed/configuration/root.rb b/lib/evil_seed/configuration/root.rb index 299fdfa..c852cac 100644 --- a/lib/evil_seed/configuration/root.rb +++ b/lib/evil_seed/configuration/root.rb @@ -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 @@ -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 diff --git a/lib/evil_seed/relation_dumper.rb b/lib/evil_seed/relation_dumper.rb index 04f4519..eef7063 100644 --- a/lib/evil_seed/relation_dumper.rb +++ b/lib/evil_seed/relation_dumper.rb @@ -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 @@ -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)