Created
August 21, 2012 17:37
-
-
Save lukelex/3417664 to your computer and use it in GitHub Desktop.
Getting the relational hierarchy of a mongo embedded document
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
module EmbeddedSearch | |
def self.included(base) # :nodoc: | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def hierarchy | |
value = "" | |
klass = self | |
while klass.embedded? | |
klass = klass.relations.select do |k,v| | |
v.relation == Mongoid::Relations::Embedded::In | |
end.first.first | |
value += klass + "." | |
klass = klass.classify.constantize | |
value += 's' if klass.embedded? | |
end | |
value += self.to_s.downcase + "s" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment