mongoid_many_or_many_to_many_setter 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 +9 -0
- data/lib/mongoid_many_or_many_to_many_setter.rb +42 -0
- data/mongoid_many_or_many_to_many_setter.gemspec +16 -0
- metadata +83 -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,9 @@
|
|
1
|
+
mongoid_many_or_many_to_many_setter
|
2
|
+
===================================
|
3
|
+
在Mongoid里,解决在_id主键存在情况下,通过另外一个uuid键来做多对多,一对多关系的兼容。
|
4
|
+
|
5
|
+
具体原因请见 doc/mongoid.markdown#多对多用uuid关联,对方保存的*_ids是uuid,而自己保存的*_ids是_id。
|
6
|
+
|
7
|
+
TODO
|
8
|
+
-----------------------------------
|
9
|
+
可是如果一开始把_id直接改写为uuid就无此问题
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
module ::Mongoid
|
6
|
+
module ManyOrManytomanySetter
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
before_save :many_or_many_to_many_setter
|
11
|
+
end
|
12
|
+
|
13
|
+
def many_or_many_to_many_setter
|
14
|
+
item = self
|
15
|
+
item.class.relations.select do |rel1, meta1|
|
16
|
+
# 只有一对多,多对多有此问题
|
17
|
+
[Mongoid::Relations::Referenced::ManyToMany, Mongoid::Relations::Referenced::Many].include?(meta1.relation) &&
|
18
|
+
# 确保关联key是标准的_ids结尾
|
19
|
+
meta1.key.match(/_ids/)
|
20
|
+
end.each do |rel1, meta1|
|
21
|
+
array1 = item.send(meta1.key)
|
22
|
+
next if array1.blank?
|
23
|
+
|
24
|
+
# 只判断第一个即可
|
25
|
+
if Utils.is_id? array1[0]
|
26
|
+
puts "[ManyOrManytomanySetter][#{meta1.relation}][#{meta1.class_name}] Fix #{item.class} #{item.uuid}"
|
27
|
+
# TODO 通过配置主键方式读取 uuid
|
28
|
+
# item.write_attribute meta1.key, meta1.class_name.constantize.find(array1).map(&:uuid) # 当时有效,但是之后再Model.find后又变回原样了
|
29
|
+
item.send "#{meta1.key}=", meta1.class_name.constantize.find(array1).map(&:uuid)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Utils; end
|
35
|
+
# 直接这样判断长度的简单的判断就好
|
36
|
+
# _id : '528dba704f14f0bdea000001'.size => 24
|
37
|
+
# uuid : 'f2c5d023-fe0a-4dae-be0e-7a387c99924f'.size => 36
|
38
|
+
def Utils.is_id? str; str.to_s.size == 24; end
|
39
|
+
def Utils.is_uuid? str; str.to_s.size == 36; end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mongoid_many_or_many_to_many_setter'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-12-05'
|
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_many_or_many_to_many_setter/'
|
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,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_many_or_many_to_many_setter
|
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-05 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: 在Mongoid里,解决在_id主键存在情况下,通过另外一个uuid键来做多对多,一对多关系的兼容。
|
47
|
+
email: mvjome@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- LICENSE
|
54
|
+
- README.markdown
|
55
|
+
- doc/mongoid.markdown
|
56
|
+
- lib/mongoid_many_or_many_to_many_setter.rb
|
57
|
+
- mongoid_many_or_many_to_many_setter.gemspec
|
58
|
+
homepage: https://github.com/SunshineLibrary/mongoid_many_or_many_to_many_setter/
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.23
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: 在Mongoid里,解决在_id主键存在情况下,通过另外一个uuid键来做多对多,一对多关系的兼容。
|
83
|
+
test_files: []
|