mongoid_touch_parents_recursively 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/LICENSE +20 -0
- data/README.markdown +31 -0
- data/lib/mongoid_touch_parents_recursively.rb +59 -0
- data/mongoid_touch_parents_recursively.gemspec +16 -0
- metadata +82 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 SunshineLibrary
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
mongoid_touch_parents_recursively
|
2
|
+
=================================
|
3
|
+
|
4
|
+
touch parents recursively in Mongoid
|
5
|
+
|
6
|
+
Install
|
7
|
+
--------------------------------
|
8
|
+
gem 'mongoid_touch_parents_recursively'
|
9
|
+
|
10
|
+
Usage
|
11
|
+
--------------------------------
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
# 配置最公用的Mongoid::Sunshine
|
15
|
+
module Mongoid
|
16
|
+
module Sunshine
|
17
|
+
extend ActiveSupport::Concern
|
18
|
+
included do
|
19
|
+
include Mongoid::Document
|
20
|
+
include Mongoid::Timestamps
|
21
|
+
include Mongoid::TouchParentsRecursively
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# 配置需要touch的父级
|
27
|
+
::Mongoid::TouchParentsRecursively.allowed_models_proc = lambda do |model_name|
|
28
|
+
[Subject, Folder, FolderClassroom, FolderPiece, Piece].map(&:to_s).include? model_name
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# TODO 需要修改relations等,才能改为ActiveModel公用
|
3
|
+
|
4
|
+
require 'mongoid'
|
5
|
+
|
6
|
+
module ::Mongoid
|
7
|
+
module TouchParentsRecursively
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# 设置是否忽略某些model
|
11
|
+
mattr_accessor :allowed_models_proc
|
12
|
+
self.allowed_models_proc = proc {|model| true }
|
13
|
+
|
14
|
+
included do
|
15
|
+
|
16
|
+
after_save :touch_parents_recursively
|
17
|
+
def touch_parents_recursively
|
18
|
+
# cache @__parents
|
19
|
+
@__parents ||= begin
|
20
|
+
__parents = Set.new
|
21
|
+
|
22
|
+
# 按 @diyumoshushi 建议,CRUD需要通知到所有关联的父级对象,包括多对多,这样可以兼容被删除的情况。
|
23
|
+
# @mvj3 但是只在第一级就可兼容多对多情况下被删除的情况。因为关联的第二级以上就没有被删除了。
|
24
|
+
# 根本解决的是 **引用的问题**,去通知位于顶级的subject去更新。
|
25
|
+
Utils.fetch_and_store_parent __parents, self, true
|
26
|
+
end
|
27
|
+
|
28
|
+
@__parents.each(&:touch)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# put encapsulation method here
|
34
|
+
module Utils
|
35
|
+
def self.fetch_and_store_parent __parents, __parent, is_first_level
|
36
|
+
# parents 可能的是 has_and_belongs_to_many or belongs_to
|
37
|
+
Array(__parent).each do |__parent1| # compact with Mongoid::Relations::Targets::Enumerable as Enumerable
|
38
|
+
__parent1.class.relations.select do |k, v|
|
39
|
+
__result = false
|
40
|
+
__result ||= (v.macro == :belongs_to)
|
41
|
+
__result ||= (k.match(__parent1.class.name.singularize.downcase) && (v.macro != :embeds_many)) if is_first_level
|
42
|
+
__result = false if not ::Mongoid::TouchParentsRecursively.allowed_models_proc.call(v.class_name)
|
43
|
+
__result
|
44
|
+
end.map do |k, v|
|
45
|
+
Array(__parent1.send(k))
|
46
|
+
end.flatten.each do |__parent2|
|
47
|
+
if not __parents.include? __parent2
|
48
|
+
__parents.add __parent2
|
49
|
+
Utils.fetch_and_store_parent __parents, __parent2, false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
return __parents
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mongoid_touch_parents_recursively'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-12-04'
|
5
|
+
s.summary = File.read("README.markdown").split(/===+/)[1].strip.split("\n")[0]
|
6
|
+
s.description = s.summary
|
7
|
+
s.authors = ["David Chen"]
|
8
|
+
s.email = 'mvjome@gmail.com'
|
9
|
+
s.homepage = 'https://github.com/SunshineLibrary/mongoid_touch_parents_recursively/'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.add_dependency "mongoid"
|
13
|
+
s.add_dependency "activesupport", "> 3.2"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_touch_parents_recursively
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Chen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>'
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>'
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: touch parents recursively in Mongoid
|
47
|
+
email: mvjome@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- LICENSE
|
54
|
+
- README.markdown
|
55
|
+
- lib/mongoid_touch_parents_recursively.rb
|
56
|
+
- mongoid_touch_parents_recursively.gemspec
|
57
|
+
homepage: https://github.com/SunshineLibrary/mongoid_touch_parents_recursively/
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.23
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: touch parents recursively in Mongoid
|
82
|
+
test_files: []
|