mongoid_ext 0.6.2 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +20 -18
- data/VERSION +1 -1
- data/lib/mongoid_ext/criteria_ext.rb +0 -3
- data/lib/mongoid_ext/document_ext.rb +8 -10
- data/lib/mongoid_ext/file.rb +20 -4
- data/lib/mongoid_ext/file_list.rb +28 -2
- data/lib/mongoid_ext/filter/result_set.rb +3 -0
- data/lib/mongoid_ext/js/filter.js +42 -21
- data/lib/mongoid_ext/js/find_tags.js +7 -4
- data/lib/mongoid_ext/js/tag_cloud.js +17 -15
- data/lib/mongoid_ext/modifiers.rb +24 -25
- data/lib/mongoid_ext/paranoia.rb +1 -1
- data/lib/mongoid_ext/patches.rb +14 -1
- data/lib/mongoid_ext/storage.rb +23 -14
- data/lib/mongoid_ext/types/embedded_hash.rb +40 -5
- data/lib/mongoid_ext/types/open_struct.rb +13 -9
- data/lib/mongoid_ext/types/timestamp.rb +6 -4
- data/lib/mongoid_ext/types/translation.rb +4 -2
- data/lib/mongoid_ext/versioning.rb +62 -65
- data/lib/mongoid_ext/voteable.rb +8 -8
- data/mongoid_ext.gemspec +11 -13
- data/test/helper.rb +1 -2
- data/test/models.rb +1 -1
- data/test/test_storage.rb +1 -1
- data/test/types/test_open_struct.rb +1 -1
- metadata +172 -165
data/lib/mongoid_ext/paranoia.rb
CHANGED
data/lib/mongoid_ext/patches.rb
CHANGED
data/lib/mongoid_ext/storage.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
module MongoidExt
|
2
2
|
module Storage
|
3
|
-
|
4
|
-
model.class_eval do
|
5
|
-
extend ClassMethods
|
3
|
+
extend ActiveSupport::Concern
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
included do
|
6
|
+
validate :__add_storage_errors
|
7
|
+
file_list :file_list
|
10
8
|
end
|
11
9
|
|
12
10
|
def put_file(name, io, options = {})
|
@@ -29,12 +27,12 @@ module MongoidExt
|
|
29
27
|
file_list.files
|
30
28
|
end
|
31
29
|
|
32
|
-
def
|
33
|
-
@
|
30
|
+
def storage_errors
|
31
|
+
@storage_errors ||= {}
|
34
32
|
end
|
35
33
|
|
36
|
-
def
|
37
|
-
|
34
|
+
def __add_storage_errors
|
35
|
+
storage_errors.each do |k, msgs|
|
38
36
|
msgs.each do |msg|
|
39
37
|
self.errors.add(k, msg)
|
40
38
|
end
|
@@ -53,18 +51,23 @@ module MongoidExt
|
|
53
51
|
|
54
52
|
if list.nil?
|
55
53
|
list = self[name] = MongoidExt::FileList.new
|
56
|
-
elsif list.class == BSON::OrderedHash || list.class == Hash
|
54
|
+
elsif list.class == BSON::OrderedHash || list.class == ::Hash
|
57
55
|
list = self[name] = MongoidExt::FileList.new(list)
|
58
56
|
end
|
59
57
|
|
60
58
|
list.parent_document = self
|
59
|
+
list.list_name = name
|
61
60
|
list
|
62
61
|
end
|
63
62
|
|
64
63
|
set_callback(:create, :after) do |doc|
|
65
64
|
l = doc.send(name)
|
66
65
|
l.sync_files
|
67
|
-
|
66
|
+
|
67
|
+
query = doc._updates
|
68
|
+
if !query.blank?
|
69
|
+
doc.collection.update({:_id => doc.id}, query)
|
70
|
+
end
|
68
71
|
end
|
69
72
|
|
70
73
|
set_callback(:destroy, :before) do |doc|
|
@@ -89,10 +92,16 @@ module MongoidExt
|
|
89
92
|
end
|
90
93
|
|
91
94
|
if self.errors[name].blank?
|
92
|
-
send(opts[:in])
|
95
|
+
fl = send(opts[:in])
|
96
|
+
|
97
|
+
if file.kind_of?(String)
|
98
|
+
file = StringIO.new(file)
|
99
|
+
end
|
100
|
+
|
101
|
+
fl.get(name.to_s).put(name.to_s, file)
|
93
102
|
else
|
94
103
|
# we store the errors here because we want to validate before storing the file
|
95
|
-
|
104
|
+
storage_errors.merge!(self.errors)
|
96
105
|
end
|
97
106
|
end
|
98
107
|
|
@@ -1,16 +1,33 @@
|
|
1
1
|
class EmbeddedHash < Hash
|
2
|
+
include Mongoid::Fields::Serializable
|
2
3
|
include ActiveModel::Validations
|
3
4
|
|
4
5
|
def initialize(other = {})
|
5
|
-
|
6
|
-
|
6
|
+
super()
|
7
|
+
|
8
|
+
if other
|
9
|
+
other.each do |k,v|
|
10
|
+
self[k] = v
|
11
|
+
end
|
7
12
|
end
|
8
|
-
|
13
|
+
|
14
|
+
self.assign_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.allocate
|
18
|
+
obj = super
|
19
|
+
obj.assign_id
|
20
|
+
|
21
|
+
obj
|
9
22
|
end
|
10
23
|
|
11
24
|
def self.field(name, opts = {})
|
12
25
|
define_method(name) do
|
13
|
-
|
26
|
+
if fetch(name.to_s, nil).nil?
|
27
|
+
self[name.to_s] = opts[:default].kind_of?(Proc) ? opts[:default].call : opts[:default]
|
28
|
+
else
|
29
|
+
self[name.to_s]
|
30
|
+
end
|
14
31
|
end
|
15
32
|
|
16
33
|
define_method("#{name}=") do |v|
|
@@ -19,7 +36,25 @@ class EmbeddedHash < Hash
|
|
19
36
|
end
|
20
37
|
|
21
38
|
def id
|
22
|
-
|
39
|
+
self["_id"]
|
23
40
|
end
|
24
41
|
alias :_id :id
|
42
|
+
|
43
|
+
def serialize(v)
|
44
|
+
v
|
45
|
+
end
|
46
|
+
|
47
|
+
def deserialize(v)
|
48
|
+
self.class.new(v)
|
49
|
+
end
|
50
|
+
|
51
|
+
# def method_missing(name, *args, &block)
|
52
|
+
# @table.send(name, *args, &block)
|
53
|
+
# end
|
54
|
+
|
55
|
+
def assign_id
|
56
|
+
if fetch("_id", nil).nil?
|
57
|
+
self["_id"] = BSON::ObjectId.new.to_s
|
58
|
+
end
|
59
|
+
end
|
25
60
|
end
|
@@ -1,15 +1,19 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
3
|
+
module MongoidExt
|
4
|
+
class OpenStruct < ::OpenStruct
|
5
|
+
include Mongoid::Fields::Serializable
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def serialize(value)
|
8
|
+
value.nil? ? nil : value.to_hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def deserialize(value)
|
12
|
+
value.nil? ? nil : OpenStruct.new(value || {})
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
def to_hash
|
16
|
+
send(:table)
|
17
|
+
end
|
14
18
|
end
|
15
19
|
end
|
@@ -1,15 +1,17 @@
|
|
1
1
|
class Timestamp
|
2
|
-
|
2
|
+
include Mongoid::Fields::Serializable
|
3
|
+
|
4
|
+
def deserialize(value)
|
3
5
|
if value.nil? || value == ''
|
4
6
|
nil
|
5
7
|
else
|
6
|
-
Time.zone.at(value.to_i)
|
8
|
+
::Time.zone.at(value.to_i)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
12
|
+
def serialize(value)
|
11
13
|
value.to_i
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
Time.zone ||= 'UTC'
|
17
|
+
::Time.zone ||= 'UTC'
|
@@ -1,4 +1,6 @@
|
|
1
1
|
class Translation < String
|
2
|
+
include Mongoid::Fields::Serializable
|
3
|
+
|
2
4
|
attr_accessor :keys
|
3
5
|
|
4
6
|
def initialize(*args)
|
@@ -33,13 +35,13 @@ class Translation < String
|
|
33
35
|
tr
|
34
36
|
end
|
35
37
|
|
36
|
-
def
|
38
|
+
def serialize(value)
|
37
39
|
return value.keys if value.kind_of?(self)
|
38
40
|
|
39
41
|
@keys
|
40
42
|
end
|
41
43
|
|
42
|
-
def
|
44
|
+
def deserialize(value)
|
43
45
|
return value if value.kind_of?(self)
|
44
46
|
|
45
47
|
result = self.new
|
@@ -3,7 +3,6 @@ module Versioning
|
|
3
3
|
def self.included(klass)
|
4
4
|
klass.class_eval do
|
5
5
|
extend ClassMethods
|
6
|
-
include InstanceMethods
|
7
6
|
|
8
7
|
cattr_accessor :versionable_options
|
9
8
|
|
@@ -17,92 +16,90 @@ module Versioning
|
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
version = self.version_at(pos)
|
19
|
+
def rollback!(pos = nil)
|
20
|
+
pos = self.versions_count-1 if pos.nil?
|
21
|
+
version = self.version_at(pos)
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
owner_field = self.class.versionable_options[:owner_field]
|
31
|
-
self[owner_field] = version[owner_field] if !self.changes.include?(owner_field)
|
32
|
-
self.updated_at = version.date if self.respond_to?(:updated_at) && !self.updated_at_changed?
|
23
|
+
if version
|
24
|
+
version.data.each do |key, value|
|
25
|
+
self.send("#{key}=", value)
|
33
26
|
end
|
34
27
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
r
|
28
|
+
owner_field = self.class.versionable_options[:owner_field]
|
29
|
+
self[owner_field] = version[owner_field] if !self.changes.include?(owner_field)
|
30
|
+
self.updated_at = version.date if self.respond_to?(:updated_at) && !self.updated_at_changed?
|
39
31
|
end
|
40
32
|
|
41
|
-
|
42
|
-
|
43
|
-
|
33
|
+
@rolling_back = true
|
34
|
+
r = save!
|
35
|
+
@rolling_back = false
|
36
|
+
r
|
37
|
+
end
|
38
|
+
|
39
|
+
def load_version(pos = nil)
|
40
|
+
pos = self.versions_count-1 if pos.nil?
|
41
|
+
version = self.version_at(pos)
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
43
|
+
if version
|
44
|
+
version.data.each do |key, value|
|
45
|
+
self.send("#{key}=", value)
|
49
46
|
end
|
50
47
|
end
|
48
|
+
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def diff(key, pos1, pos2, format = :html)
|
51
|
+
version1 = self.version_at(pos1)
|
52
|
+
version2 = self.version_at(pos2)
|
55
53
|
|
56
|
-
|
57
|
-
|
54
|
+
Differ.diff(version1.content(key), version2.content(key)).format_as(format).html_safe
|
55
|
+
end
|
58
56
|
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
def diff_by_word(key, pos1, pos2, format = :html)
|
58
|
+
version1 = self.version_at(pos1)
|
59
|
+
version2 = self.version_at(pos2)
|
62
60
|
|
63
|
-
|
64
|
-
|
61
|
+
Differ.diff_by_word(version1.content(key), version2.content(key)).format_as(format).html_safe
|
62
|
+
end
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
def diff_by_line(key, pos1, pos2, format = :html)
|
65
|
+
version1 = self.version_at(pos1)
|
66
|
+
version2 = self.version_at(pos2)
|
69
67
|
|
70
|
-
|
71
|
-
|
68
|
+
Differ.diff_by_line(version1.content(key), version2.content(key)).format_as(format).html_safe
|
69
|
+
end
|
72
70
|
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
def diff_by_char(key, pos1, pos2, format = :html)
|
72
|
+
version1 = self.version_at(pos1)
|
73
|
+
version2 = self.version_at(pos2)
|
76
74
|
|
77
|
-
|
78
|
-
|
75
|
+
Differ.diff_by_char(version1.content(key), version2.content(key)).format_as(format).html_safe
|
76
|
+
end
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
def current_version
|
79
|
+
version_klass.new(:data => self.attributes, self.class.versionable_options[:owner_field] => (self.updated_by_id_was || self.updated_by_id), :created_at => Time.now)
|
80
|
+
end
|
83
81
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
82
|
+
def version_at(pos)
|
83
|
+
case pos.to_s
|
84
|
+
when "current"
|
85
|
+
current_version
|
86
|
+
when "first"
|
87
|
+
version_klass.find(self.version_ids.first)
|
88
|
+
when "last"
|
89
|
+
version_klass.find(self.version_ids.last)
|
90
|
+
else
|
91
|
+
if version_id = self.version_ids[pos]
|
92
|
+
version_klass.find(self.version_ids[pos])
|
96
93
|
end
|
97
94
|
end
|
95
|
+
end
|
98
96
|
|
99
|
-
|
100
|
-
|
101
|
-
|
97
|
+
def versions
|
98
|
+
version_klass.where(:target_id => self.id)
|
99
|
+
end
|
102
100
|
|
103
|
-
|
104
|
-
|
105
|
-
end
|
101
|
+
def version_klass
|
102
|
+
self.class.version_klass
|
106
103
|
end
|
107
104
|
|
108
105
|
module ClassMethods
|
data/lib/mongoid_ext/voteable.rb
CHANGED
@@ -46,12 +46,12 @@ module MongoidExt
|
|
46
46
|
|
47
47
|
def add_vote!(value, voter_id, &block)
|
48
48
|
if embedded?
|
49
|
-
updates = {self.
|
50
|
-
self.
|
49
|
+
updates = {self.atomic_position+".votes_count" => 1,
|
50
|
+
self.atomic_position+".votes_average" => value.to_i}
|
51
51
|
if value == 1
|
52
|
-
updates[self.
|
52
|
+
updates[self.atomic_position+".votes_up"] = 1
|
53
53
|
elsif value == -1
|
54
|
-
updates[self.
|
54
|
+
updates[self.atomic_position+".votes_down"] = 1
|
55
55
|
end
|
56
56
|
|
57
57
|
self._parent.increment(updates)
|
@@ -73,12 +73,12 @@ module MongoidExt
|
|
73
73
|
|
74
74
|
def remove_vote!(value, voter_id, &block)
|
75
75
|
if embedded?
|
76
|
-
updates = {self.
|
77
|
-
self.
|
76
|
+
updates = {self.atomic_position+".votes_count" => -1,
|
77
|
+
self.atomic_position+".votes_average" => -value.to_i}
|
78
78
|
if value == 1
|
79
|
-
updates[self.
|
79
|
+
updates[self.atomic_position+".votes_up"] = -1
|
80
80
|
elsif value == -1
|
81
|
-
updates[self.
|
81
|
+
updates[self.atomic_position+".votes_down"] = -1
|
82
82
|
end
|
83
83
|
|
84
84
|
self._parent.increment(updates)
|
data/mongoid_ext.gemspec
CHANGED
@@ -4,15 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "mongoid_ext"
|
8
|
+
s.version = "0.7.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["David A. Cuadrado"]
|
12
|
-
s.date =
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
s.email = %q{krawek@gmail.com}
|
12
|
+
s.date = "2012-07-27"
|
13
|
+
s.description = "mongoid plugins"
|
14
|
+
s.email = "krawek@gmail.com"
|
16
15
|
s.executables = ["mongoid_console"]
|
17
16
|
s.extra_rdoc_files = [
|
18
17
|
"LICENSE",
|
@@ -74,17 +73,16 @@ Gem::Specification.new do |s|
|
|
74
73
|
"test/types/test_set.rb",
|
75
74
|
"test/types/test_timestamp.rb"
|
76
75
|
]
|
77
|
-
s.homepage =
|
76
|
+
s.homepage = "http://github.com/dcu/mongoid_ext"
|
78
77
|
s.require_paths = ["lib"]
|
79
|
-
s.rubygems_version =
|
80
|
-
s.summary =
|
78
|
+
s.rubygems_version = "1.8.24"
|
79
|
+
s.summary = "mongoid plugins"
|
81
80
|
|
82
81
|
if s.respond_to? :specification_version then
|
83
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
84
82
|
s.specification_version = 3
|
85
83
|
|
86
84
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
87
|
-
s.add_runtime_dependency(%q<mongoid>, ["~> 2"])
|
85
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 2.3"])
|
88
86
|
s.add_runtime_dependency(%q<uuidtools>, [">= 2.1.1"])
|
89
87
|
s.add_runtime_dependency(%q<i18n>, [">= 0"])
|
90
88
|
s.add_runtime_dependency(%q<tzinfo>, [">= 0"])
|
@@ -99,7 +97,7 @@ Gem::Specification.new do |s|
|
|
99
97
|
s.add_development_dependency(%q<mocha>, ["~> 0.9.4"])
|
100
98
|
s.add_development_dependency(%q<timecop>, [">= 0"])
|
101
99
|
else
|
102
|
-
s.add_dependency(%q<mongoid>, ["~> 2"])
|
100
|
+
s.add_dependency(%q<mongoid>, ["~> 2.3"])
|
103
101
|
s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
|
104
102
|
s.add_dependency(%q<i18n>, [">= 0"])
|
105
103
|
s.add_dependency(%q<tzinfo>, [">= 0"])
|
@@ -115,7 +113,7 @@ Gem::Specification.new do |s|
|
|
115
113
|
s.add_dependency(%q<timecop>, [">= 0"])
|
116
114
|
end
|
117
115
|
else
|
118
|
-
s.add_dependency(%q<mongoid>, ["~> 2"])
|
116
|
+
s.add_dependency(%q<mongoid>, ["~> 2.3"])
|
119
117
|
s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
|
120
118
|
s.add_dependency(%q<i18n>, [">= 0"])
|
121
119
|
s.add_dependency(%q<tzinfo>, [">= 0"])
|