-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixes for embedded touch * Fix touch loop fix on Mongoid 4 and 5
- Loading branch information
1 parent
9fca9d8
commit 6a1fadd
Showing
9 changed files
with
151 additions
and
38 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,96 @@ | ||
# Backport Mongoid 4 :touch option for #embedded_in to Mongoid 3. | ||
# Backport support #embedded_in :touch option from Mongoid 4 to Mongoid 3. | ||
# Also support touch callback on update, and fix infinite loop issue. | ||
|
||
if Mongoid::VERSION =~ /\A3\./ | ||
|
||
module Mongoid | ||
module Relations | ||
module Embedded | ||
class In < Relations::One | ||
class << self | ||
def valid_options | ||
[ :autobuild, :cyclic, :polymorphic, :touch ] | ||
module Relations | ||
module Embedded | ||
class In < Relations::One | ||
class << self | ||
def valid_options | ||
[ :autobuild, :cyclic, :polymorphic, :touch ] | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Macros | ||
module ClassMethods | ||
|
||
def embedded_in(name, options = {}, &block) | ||
if ancestors.include?(Mongoid::Versioning) | ||
raise Errors::VersioningNotOnRoot.new(self) | ||
end | ||
meta = characterize(name, Embedded::In, options, &block) | ||
self.embedded = true | ||
relate(name, meta) | ||
builder(name, meta).creator(name, meta) | ||
touchable(meta) | ||
add_counter_cache_callbacks(meta) if meta.counter_cached? | ||
meta | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Macros | ||
module ClassMethods | ||
module Touchable | ||
module ClassMethods | ||
|
||
def embedded_in(name, options = {}, &block) | ||
if ancestors.include?(Mongoid::Versioning) | ||
raise Errors::VersioningNotOnRoot.new(self) | ||
def touchable(metadata) | ||
if metadata.touchable? | ||
name = metadata.name | ||
method_name = define_relation_touch_method(name) | ||
after_save method_name | ||
after_destroy method_name | ||
after_touch method_name | ||
end | ||
self | ||
end | ||
meta = characterize(name, Embedded::In, options, &block) | ||
self.embedded = true | ||
relate(name, meta) | ||
builder(name, meta).creator(name, meta) | ||
touchable(meta) | ||
add_counter_cache_callbacks(meta) if meta.counter_cached? | ||
meta | ||
end | ||
end | ||
end | ||
|
||
module Callbacks | ||
def cascadable_children(kind, children = Set.new) | ||
embedded_relations.each_pair do |name, metadata| | ||
next unless metadata.cascading_callbacks? | ||
without_autobuild do | ||
delayed_pulls = delayed_atomic_pulls[name] | ||
delayed_unsets = delayed_atomic_unsets[name] | ||
children.merge(delayed_pulls) if delayed_pulls | ||
children.merge(delayed_unsets) if delayed_unsets | ||
relation = send(name) | ||
Array.wrap(relation).each do |child| | ||
next if children.include?(child) | ||
children.add(child) if cascadable_child?(kind, child, metadata) | ||
child.send(:cascadable_children, kind, children) | ||
end | ||
end | ||
end | ||
children.to_a | ||
end | ||
|
||
def cascadable_child?(kind, child, metadata) | ||
return false if kind == :initialize || kind == :find || kind == :touch | ||
return false if kind == :validate && metadata.validate? | ||
child.callback_executable?(kind) ? child.in_callback_state?(kind) : false | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
if Mongoid::VERSION =~ /\A[45]\./ | ||
|
||
module Mongoid | ||
|
||
module Interceptable | ||
def cascadable_child?(kind, child, metadata) | ||
return false if kind == :initialize || kind == :find || kind == :touch | ||
return false if kind == :validate && metadata.validate? | ||
child.callback_executable?(kind) ? child.in_callback_state?(kind) : false | ||
end | ||
end | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module MongoidMonkey | ||
VERSION = '0.2.4' | ||
VERSION = '0.2.5' | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Book | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
embeds_many :pages, cascade_callbacks: true | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Edit | ||
include Mongoid::Document | ||
include Mongoid::Timestamps::Updated | ||
embedded_in :wiki_page, touch: true | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Page | ||
include Mongoid::Document | ||
|
||
embedded_in :book, touch: true | ||
field :content, :type => String | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class WikiPage | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
field :title, type: String | ||
|
||
embeds_many :edits, validate: false | ||
end |
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