Skip to content

Instantly share code, notes, and snippets.

@kaspth
Created October 26, 2022 20:55
Show Gist options
  • Save kaspth/a6e4b837d095871e054337077430b723 to your computer and use it in GitHub Desktop.
Save kaspth/a6e4b837d095871e054337077430b723 to your computer and use it in GitHub Desktop.
# 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