one way to avoid autosave
This commit is contained in:
parent
64168cad37
commit
64ea41cbcd
@ -16,6 +16,7 @@ module Locomotive
|
||||
before_validation :get_wildcards_from_parent
|
||||
before_validation :add_slug_to_wildcards
|
||||
before_save :build_fullpath
|
||||
before_save :set_children_autosave
|
||||
# before_rearrange :foo #propagate_fullpath_changes
|
||||
# after_save :propagate_fullpath_changes
|
||||
|
||||
@ -61,6 +62,11 @@ module Locomotive
|
||||
|
||||
protected
|
||||
|
||||
def set_children_autosave
|
||||
@autosave_for_children = !must_propagate_fullpath_changes?
|
||||
true
|
||||
end
|
||||
|
||||
def get_wildcards_from_parent
|
||||
return true if self.parent.nil?
|
||||
|
||||
@ -88,8 +94,12 @@ module Locomotive
|
||||
end
|
||||
end
|
||||
|
||||
def must_propagate_fullpath_changes?
|
||||
self.wildcard_changed? || self.slug_changed?
|
||||
end
|
||||
|
||||
def propagate_fullpath_changes
|
||||
return true unless self.wildcard_changed? || self.slug_changed?
|
||||
return true unless must_propagate_fullpath_changes?
|
||||
|
||||
parent_identities = { self._id => self }
|
||||
|
||||
|
@ -74,6 +74,10 @@ module Locomotive
|
||||
|
||||
end
|
||||
|
||||
def autosave_for_children?
|
||||
@autosave_for_children != false
|
||||
end
|
||||
|
||||
# Returns the children of this node but with the minimal set of required attributes
|
||||
#
|
||||
# @return [ Array ] The children pages ordered by their position
|
||||
|
@ -12,6 +12,28 @@ module Mongoid#:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
module Relations #:nodoc:
|
||||
module AutoSave
|
||||
module ClassMethods #:nodoc:
|
||||
def autosave(metadata)
|
||||
if metadata.autosave?
|
||||
set_callback :save, :after do |document|
|
||||
relation = document.send(metadata.name)
|
||||
|
||||
return true if document.try(:"autosave_for_#{metadata.name}?") == false # FIXME (Didier L.) add more control on the document side
|
||||
|
||||
if relation
|
||||
(relation.do_or_do_not(:in_memory) || Array.wrap(relation)).each do |doc|
|
||||
doc.save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Fields #:nodoc:
|
||||
module Internal #:nodoc:
|
||||
class RawArray < Mongoid::Fields::Internal::Array
|
||||
@ -59,3 +81,5 @@ module Mongoid#:nodoc:
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user