Created
October 26, 2022 20:55
-
-
Save kaspth/a6e4b837d095871e054337077430b723 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Add a module builder that'll mask each class method? | |
def self.required_condition_via(scopes) | |
prepend RequiredCondition.new(scopes) | |
end | |
module RequiredCondition | |
def initialize(scopes) | |
@scopes = scopes | |
end | |
def singleton_method_added(name) | |
define_method(name) do | |
super.tap do |relation| | |
if relation&.required_query_condition_unsatisfied? | |
raise "this relation didn't call any of the required scopes upfront" | |
end | |
end | |
end | |
end | |
end | |
module RequiredConditionOnRelation | |
# I have no clue how to shadow/mask the chainable methods on relations. | |
def required_query_condition_unsatisfied? | |
(required_scopes & called_scope_methods).none? | |
end | |
end | |
ActiveRecord::Relation.prepend RequiredConditionOnRelation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment