Skip to content

Commit

Permalink
Merge pull request mongodb#3327 from mongoid/autosave_clean
Browse files Browse the repository at this point in the history
Autosave reword
  • Loading branch information
arthurnn committed Oct 16, 2013
2 parents 3c0735a + 2522b7c commit 5c44524
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 47 deletions.
50 changes: 15 additions & 35 deletions lib/mongoid/relations/auto_save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ module Relations
module AutoSave
extend ActiveSupport::Concern

included do
class_attribute :autosaved_relations
self.autosaved_relations = []
end

# Used to prevent infinite loops in associated autosaves.
#
# @example Is the document autosaved?
Expand Down Expand Up @@ -44,15 +39,8 @@ def __autosaving__
# document.changed_for_autosave?
#
# @since 3.1.3
def changed_for_autosave?
new_record? || changed? || marked_for_destruction?
end

# Returns the relation, if it exists
#
# @since 3.1.3
def relation_changed_for_autosave(metadata)
ivar(metadata.name) if self.class.autosaved_relations.include?(metadata.name)
def changed_for_autosave?(doc)
doc.new_record? || doc.changed? || doc.marked_for_destruction?
end

module ClassMethods
Expand All @@ -68,38 +56,30 @@ module ClassMethods
#
# @since 2.0.0.rc.1
def autosave(metadata)
if metadata.autosave? && autosavable?(metadata)
autosaved_relations.push(metadata.name)
set_callback :save, :after, unless: :autosaved? do |document|
if metadata.autosave? && !metadata.embedded?
save_method = :"autosave_documents_for_#{metadata.name}"
define_method(save_method) do

if before_callback_halted?
self.before_callback_halted = false
else
__autosaving__ do
if document.changed_for_autosave? || relation = document.relation_changed_for_autosave(metadata)
relation = document.__send__(metadata.name) unless relation
(relation.do_or_do_not(:in_memory) || Array.wrap(relation)).each do |doc|
doc.save
end if relation
if relation = ivar(metadata.name)
options = persistence_options || {}
if :belongs_to == metadata.macro
relation.with(options).save if changed_for_autosave?(relation)
else
Array(relation).each { |d| d.with(options).save if changed_for_autosave?(d) }
end
end
end
end
end

after_save save_method, unless: :autosaved?
end
end

# Can the autosave be added?
#
# @example Can the autosave be added?
# Person.autosavable?(metadata)
#
# @param [ Metadata ] metadata The relation metadata.
#
# @return [ true, false ] If the autosave is able to be added.
#
# @since 3.0.0
def autosavable?(metadata)
!autosaved_relations.include?(metadata.name) && !metadata.embedded?
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/mongoid/traversable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def inherited(subclass)
subclass.pre_processed_defaults = pre_processed_defaults.dup
subclass.post_processed_defaults = post_processed_defaults.dup
subclass._declared_scopes = Hash.new { |hash,key| self._declared_scopes[key] }
subclass.autosaved_relations = autosaved_relations.dup

# We only need the _type field if inheritance is in play, but need to
# add to the root class as well for backwards compatibility.
Expand Down
26 changes: 23 additions & 3 deletions spec/mongoid/relations/auto_save_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
describe ".auto_save" do

before(:all) do
Person.autosaved_relations.delete_one(:account)
Person.autosave(Person.relations["account"].merge!(autosave: true))
end

Expand Down Expand Up @@ -48,7 +47,6 @@
end

before do
Person.autosaved_relations.delete_one(:drugs)
Person.autosave(metadata)
Person.autosave(metadata)
end
Expand Down Expand Up @@ -129,6 +127,10 @@
it "saves the relation" do
expect(account).to be_persisted
end

it "persists on the database" do
expect(account.reload).to_not be_nil
end
end

context "when saving an existing parent document" do
Expand All @@ -142,6 +144,25 @@
it "saves the relation" do
expect(account).to be_persisted
end

it "persists on the database" do
expect(account.reload).to_not be_nil
end
end

context "when updating the child" do

before do
person.account = account
person.save
end

it "sends one insert" do
account.name = "account"
expect_query(1) do
person.with(write: {w:0}).save
end
end
end

context "when not updating the document" do
Expand Down Expand Up @@ -201,7 +222,6 @@
context "when it has two ralations with autosaves" do

before do
Person.autosaved_relations.delete_one(:drugs)
Person.autosave(Person.relations["drugs"].merge!(autosave: true))
end

Expand Down
1 change: 0 additions & 1 deletion spec/mongoid/relations/referenced/many_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

before(:all) do
Mongoid.raise_not_found_error = true
Person.autosaved_relations.delete_one(:preferences)
Person.autosave(Person.relations["preferences"].merge!(autosave: true))
Person.synced(Person.relations["preferences"])
end
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/validatable/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,10 @@
context "when the relation is a has one" do

before do
Person.autosaved_relations.delete_one(:game)
Person.validates :game, presence: true
end

after do
Person.autosaved_relations.clear
Person.reset_callbacks(:save)
Person.reset_callbacks(:validate)
end
Expand Down Expand Up @@ -263,10 +261,6 @@
expect(Person.relations["game"][:autosave]).to be_false
end

it "does not add any autosaved relations" do
expect(Person.autosaved_relations).to be_empty
end

context "when the relation is new" do

let(:person) do
Expand Down Expand Up @@ -329,7 +323,6 @@
end

after do
Person.autosaved_relations.clear
Person.reset_callbacks(:save)
Person.reset_callbacks(:validate)
end
Expand Down

0 comments on commit 5c44524

Please sign in to comment.