Created
December 13, 2016 22:49
-
-
Save kml/e529c64baeae9e287f6806147ae76505 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
require "mongo/collection/view" | |
# Add default "comment" for every request to MongoDB. | |
# https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/collection/view.rb#L132 | |
Mongo::Collection::View.class_eval do | |
alias :mongo_collection_view_initialize :initialize | |
def initialize(collection, filter = {}, options = {}) | |
filter["$comment"] ||= default_comment | |
mongo_collection_view_initialize(collection, filter, options) | |
end | |
private | |
def default_comment | |
@application_name ||= Rails.application.class.parent_name.downcase | |
line = caller.find { |line| line.start_with?(Rails.root.to_s) && !line.start_with?(__FILE__) } | |
line = line ? line.split('/')[-1] : "(console)" | |
[@application_name, line].join("|") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment