Created
October 12, 2015 23:19
-
-
Save ElMassimo/f59432e4211c6fbb6e04 to your computer and use it in GitHub Desktop.
Mongoid: No Heritage
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 Mongoid | |
# Public: Allows to use inheritance to reuse logic, without using Single- | |
# Collection Inheritance, storing the model and superclass in different | |
# collections. | |
module NoHeritage | |
extend ActiveSupport::Concern | |
included do | |
# Internal: Preserve the default storage options instead of storing in the | |
# same collection than the superclass. | |
delegate :storage_options, to: :class | |
end | |
module ClassMethods | |
# Internal: Prevent adding _type in query selectors, and adding an index | |
# for _type. | |
def hereditary? | |
false | |
end | |
# Internal: Prevent Mongoid from defining a _type getter and setter. | |
def field(name, options = {}) | |
super unless name.to_sym == :_type | |
end | |
# Internal: Preserve the default storage options instead of storing in the | |
# same collection than the superclass. | |
def inherited(subclass) | |
super | |
def subclass.storage_options | |
@storage_options ||= storage_options_defaults | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ElMassimo,
What is the license on this code? Can we use it freely for commercial use without attribution?
Thank you!
Warmest regards,
Ben