Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Created November 21, 2019 18:16
Show Gist options
  • Save krainboltgreene/e780e275d235720fd4b0252c2f22cb8f to your computer and use it in GitHub Desktop.
Save krainboltgreene/e780e275d235720fd4b0252c2f22cb8f to your computer and use it in GitHub Desktop.
module ActiveRecordStructuralOrAddition
def or!(other) # :nodoc:
raise
incompatible_values = structurally_incompatible_values_for_or(other)
unless incompatible_values.empty?
raise ArgumentError, "Relations passed to #or must be structurally compatible. Incompatible values: #{incompatible_values}"
end
self.where_clause = self.where_clause.or(other.where_clause)
self.having_clause = having_clause.or(other.having_clause)
self.references_values += other.references_values
self
end
STRUCTURAL_OR_METHODS = ActiveRecord::Relation::VALUE_METHODS - [:extending, :where, :having, :unscope, :references]
private def structurally_incompatible_values_for_or(other)
ActiveRecordStructuralOrAddition::STRUCTURAL_OR_METHODS.reject do |method|
get_value(method) == other.get_value(method)
end
end
end
ActiveRecord::QueryMethods.prepend(ActiveRecordStructuralOrAddition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment