Last active
April 23, 2024 16:07
-
-
Save searls/ee2a2aab1d4bdbeeca3345991c1a8d97 to your computer and use it in GitHub Desktop.
Is this overkill? Is there some other nice way to do this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValidatesDeeply | |
Failure = Struct.new(:model, :message) | |
Result = Struct.new(:valid?, :failures) | |
def validate(model, visited = Set.new) | |
return Result.new(true) if visited.include?(model) | |
visited.add(model) | |
combine_results(Result.new(model.valid?, extract_failures(model)), *validate_associations(model, visited)) | |
end | |
private | |
def combine_results(*results) | |
Result.new(results.all?(&:valid?), results.flat_map(&:failures).compact) | |
end | |
def extract_failures(model) | |
model.errors.full_messages.map { |message| Failure.new(model, message) } | |
end | |
SKIP_ASSOCIATED_TABLES = %w[ | |
active_storage_attachments | |
active_storage_blobs | |
active_storage_variant_records | |
].freeze | |
def validate_associations(model, visited) | |
model.class.reflect_on_all_associations.reject { |assoc| | |
assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection) || | |
assoc.is_a?(ActiveRecord::Reflection::ThroughReflection) || | |
SKIP_ASSOCIATED_TABLES.include?(assoc.table_name) | |
}.flat_map do |association| | |
associated_records = model.send(association.name) | |
validate_association(associated_records, visited) | |
end | |
end | |
def validate_association(associated, visited) | |
case associated | |
when ActiveRecord::Base | |
validate(associated, visited) | |
when Enumerable | |
associated.map { |record| validate(record, visited) } | |
else | |
Result.new(true) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So roughly:
HighLevelThing
-> lots ofDependentAssociatedModels
's, we don't necessarily care about Serious Validation™ untilHighLevelModel
flips somepublished?
bit/enum.There might still be a vanilla Rails approach:
Definitely a matter of taste—I'm sure
ValidatesDeeply
does the thing you want it to, but 6+ months out I know which code I'd rather have forgotten. 😅