Skip to content

Commit

Permalink
Merge 92b17e5 into 80c3390
Browse files Browse the repository at this point in the history
  • Loading branch information
l8nite committed Mar 18, 2015
2 parents 80c3390 + 92b17e5 commit a397407
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/mongoid/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Config

option :include_root_in_json, default: false
option :include_type_for_serialization, default: false
option :ignore_type_attribute, default: false
option :preload_models, default: false
option :raise_not_found_error, default: true
option :scope_overwrite_exception, default: false
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def empty_and_chainable?
def only(*args)
return clone if args.flatten.empty?
args = args.flatten
if klass.hereditary?
if klass.hereditary? && !::Mongoid::Config.ignore_type_attribute?
super(*args.push(:_type))
else
super(*args)
Expand Down Expand Up @@ -550,7 +550,8 @@ def merge_type_selection
def type_selectable?
klass.hereditary? &&
!selector.keys.include?("_type") &&
!selector.keys.include?(:_type)
!selector.keys.include?(:_type) &&
!::Mongoid::Config.ignore_type_attribute?
end

# Get the selector for type selection.
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Factory
# @return [ Document ] The instantiated document.
def build(klass, attributes = nil)
type = (attributes || {})["_type"]
if type && klass._types.include?(type)
if type && klass._types.include?(type) && !::Mongoid::Config.ignore_type_attribute?
type.constantize.new(attributes)
else
klass.new(attributes)
Expand All @@ -38,7 +38,7 @@ def build(klass, attributes = nil)
# @return [ Document ] The instantiated document.
def from_db(klass, attributes = nil, selected_fields = nil)
type = (attributes || {})["_type"]
if type.blank?
if type.blank? || ::Mongoid::Config.ignore_type_attribute?
klass.instantiate(attributes, selected_fields)
else
type.camelize.constantize.instantiate(attributes, selected_fields)
Expand Down
8 changes: 8 additions & 0 deletions spec/mongoid/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
expect(described_class.include_type_for_serialization).to be false
end

it "sets the ignore type attribute option" do
expect(described_class.ignore_type_attribute).to be false
end

it "sets the scope overwrite option" do
expect(described_class.scope_overwrite_exception).to be false
end
Expand Down Expand Up @@ -153,6 +157,10 @@
expect(described_class.include_type_for_serialization).to be false
end

it "sets the ignore type attribute option" do
expect(described_class.ignore_type_attribute).to be false
end

it "sets the scope overwrite option" do
expect(described_class.scope_overwrite_exception).to be false
end
Expand Down
14 changes: 14 additions & 0 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,20 @@ class D
it "adds _type to the fields" do
expect(criteria.options[:fields]["_type"]).to eq(1)
end

context "when ignore_type_attribute is true" do
before do
::Mongoid::Config.ignore_type_attribute = true
end

after do
::Mongoid::Config.ignore_type_attribute = false
end

it "does not add _type to the fields" do
expect(criteria.options[:fields]["_type"]).to be_nil
end
end
end

context "when limiting to embedded documents" do
Expand Down
28 changes: 28 additions & 0 deletions spec/mongoid/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@
it "instantiates the subclass" do
expect(person.class).to eq(Doctor)
end

context "when ignore_type_attribute is true" do
before do
::Mongoid::Config.ignore_type_attribute = true
end

after do
::Mongoid::Config.ignore_type_attribute = false
end

it "instantiates the calling class" do
expect(person.class).to eq(Person)
end
end
end

context "when type is an empty string" do
Expand Down Expand Up @@ -126,6 +140,20 @@
it "sets the attributes" do
expect(document.title).to eq("Sir")
end

context "when ignore_type_attribute is true" do
before do
::Mongoid::Config.ignore_type_attribute = true
end

after do
::Mongoid::Config.ignore_type_attribute = false
end

it "instantiates the calling class" do
expect(document.class).to eq(Address)
end
end
end

context "when the type is empty" do
Expand Down

0 comments on commit a397407

Please sign in to comment.