mongoid_ext 0.7.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -5
- data/Gemfile.lock +76 -27
- data/VERSION +1 -1
- data/bin/mongoid_console +9 -3
- data/lib/mongoid_ext.rb +8 -1
- data/lib/mongoid_ext/encryptor.rb +1 -1
- data/lib/mongoid_ext/file.rb +41 -14
- data/lib/mongoid_ext/file_list.rb +7 -5
- data/lib/mongoid_ext/file_server.rb +1 -3
- data/lib/mongoid_ext/filter.rb +21 -6
- data/lib/mongoid_ext/modifiers.rb +1 -1
- data/lib/mongoid_ext/paranoia.rb +2 -1
- data/lib/mongoid_ext/random.rb +3 -3
- data/lib/mongoid_ext/slugizer.rb +6 -2
- data/lib/mongoid_ext/storage.rb +10 -3
- data/lib/mongoid_ext/tags.rb +35 -4
- data/lib/mongoid_ext/types/embedded_hash.rb +4 -5
- data/lib/mongoid_ext/types/open_struct.rb +12 -8
- data/lib/mongoid_ext/types/timestamp.rb +2 -4
- data/lib/mongoid_ext/types/translation.rb +2 -4
- data/lib/mongoid_ext/versioning.rb +7 -6
- data/lib/mongoid_ext/voteable.rb +17 -12
- data/mongoid_ext.gemspec +21 -14
- data/test/helper.rb +4 -5
- data/test/models.rb +1 -1
- data/test/mongoid.yml +19 -0
- data/test/test_encryptor.rb +1 -1
- data/test/test_storage.rb +7 -7
- data/test/test_versioning.rb +5 -5
- data/test/types/test_timestamp.rb +2 -2
- metadata +89 -56
data/Gemfile
CHANGED
@@ -1,21 +1,23 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
|
-
gem 'mongoid'
|
3
|
+
gem 'mongoid'
|
4
4
|
gem 'uuidtools', '>= 2.1.1'
|
5
5
|
gem 'i18n'
|
6
6
|
gem 'tzinfo'
|
7
7
|
gem 'differ', '>= 0.1.2'
|
8
8
|
gem 'encryptor', '~> 1.1.3'
|
9
|
+
gem 'rack'
|
10
|
+
gem 'mongoid-grid_fs'
|
9
11
|
|
10
12
|
group :development do
|
11
13
|
gem "yard", "~> 0.6.0"
|
12
|
-
gem "
|
13
|
-
gem "jeweler", "~> 1.5.2"
|
14
|
+
gem "jeweler", "~> 1.8.4"
|
14
15
|
|
15
16
|
gem "shoulda", "~> 2.11.3"
|
16
17
|
gem "jnunemaker-matchy", "~> 0.4"
|
17
18
|
gem 'shoulda', '~> 2.11.3'
|
18
|
-
gem 'mocha', '~> 0.
|
19
|
+
gem 'mocha', '~> 0.11.4'
|
19
20
|
gem 'timecop'
|
21
|
+
gem 'pry'
|
20
22
|
end
|
21
23
|
|
data/Gemfile.lock
CHANGED
@@ -1,50 +1,99 @@
|
|
1
1
|
GEM
|
2
|
-
remote:
|
2
|
+
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.2.
|
5
|
-
activesupport (= 3.2.
|
4
|
+
activemodel (3.2.15)
|
5
|
+
activesupport (= 3.2.15)
|
6
6
|
builder (~> 3.0.0)
|
7
|
-
activesupport (3.2.
|
8
|
-
i18n (~> 0.6)
|
7
|
+
activesupport (3.2.15)
|
8
|
+
i18n (~> 0.6, >= 0.6.4)
|
9
9
|
multi_json (~> 1.0)
|
10
|
-
|
11
|
-
builder (3.0.
|
10
|
+
addressable (2.3.5)
|
11
|
+
builder (3.0.4)
|
12
|
+
coderay (1.0.9)
|
12
13
|
differ (0.1.2)
|
13
14
|
encryptor (1.1.3)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
faraday (0.8.8)
|
16
|
+
multipart-post (~> 1.2.0)
|
17
|
+
git (1.2.6)
|
18
|
+
github_api (0.10.1)
|
19
|
+
addressable
|
20
|
+
faraday (~> 0.8.1)
|
21
|
+
hashie (>= 1.2)
|
22
|
+
multi_json (~> 1.4)
|
23
|
+
nokogiri (~> 1.5.2)
|
24
|
+
oauth2
|
25
|
+
hashie (2.0.5)
|
26
|
+
highline (1.6.20)
|
27
|
+
httpauth (0.2.0)
|
28
|
+
i18n (0.6.5)
|
29
|
+
jeweler (1.8.8)
|
30
|
+
builder
|
31
|
+
bundler (~> 1.0)
|
18
32
|
git (>= 1.2.5)
|
33
|
+
github_api (= 0.10.1)
|
34
|
+
highline (>= 1.6.15)
|
35
|
+
nokogiri (= 1.5.10)
|
19
36
|
rake
|
37
|
+
rdoc
|
20
38
|
jnunemaker-matchy (0.4.0)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
json (1.8.1)
|
40
|
+
jwt (0.1.8)
|
41
|
+
multi_json (>= 1.5)
|
42
|
+
metaclass (0.0.1)
|
43
|
+
method_source (0.8.2)
|
44
|
+
mime-types (1.25)
|
45
|
+
mocha (0.11.4)
|
46
|
+
metaclass (~> 0.0.1)
|
47
|
+
mongoid (3.1.5)
|
48
|
+
activemodel (~> 3.2)
|
49
|
+
moped (~> 1.4)
|
50
|
+
origin (~> 1.0)
|
51
|
+
tzinfo (~> 0.3.29)
|
52
|
+
mongoid-grid_fs (1.9.0)
|
53
|
+
mime-types (~> 1.19)
|
54
|
+
mongoid (~> 3.0)
|
55
|
+
moped (1.5.1)
|
56
|
+
multi_json (1.8.2)
|
57
|
+
multi_xml (0.5.5)
|
58
|
+
multipart-post (1.2.0)
|
59
|
+
nokogiri (1.5.10)
|
60
|
+
oauth2 (0.9.2)
|
61
|
+
faraday (~> 0.8)
|
62
|
+
httpauth (~> 0.2)
|
63
|
+
jwt (~> 0.1.4)
|
64
|
+
multi_json (~> 1.0)
|
65
|
+
multi_xml (~> 0.5)
|
66
|
+
rack (~> 1.2)
|
67
|
+
origin (1.1.0)
|
68
|
+
pry (0.9.12.2)
|
69
|
+
coderay (~> 1.0.5)
|
70
|
+
method_source (~> 0.8)
|
71
|
+
slop (~> 3.4)
|
72
|
+
rack (1.5.2)
|
73
|
+
rake (10.1.0)
|
74
|
+
rdoc (4.0.1)
|
75
|
+
json (~> 1.4)
|
30
76
|
shoulda (2.11.3)
|
31
|
-
|
32
|
-
|
33
|
-
|
77
|
+
slop (3.4.6)
|
78
|
+
timecop (0.6.3)
|
79
|
+
tzinfo (0.3.38)
|
80
|
+
uuidtools (2.1.4)
|
34
81
|
yard (0.6.8)
|
35
82
|
|
36
83
|
PLATFORMS
|
37
84
|
ruby
|
38
85
|
|
39
86
|
DEPENDENCIES
|
40
|
-
bundler (~> 1.0.0)
|
41
87
|
differ (>= 0.1.2)
|
42
88
|
encryptor (~> 1.1.3)
|
43
89
|
i18n
|
44
|
-
jeweler (~> 1.
|
90
|
+
jeweler (~> 1.8.4)
|
45
91
|
jnunemaker-matchy (~> 0.4)
|
46
|
-
mocha (~> 0.
|
47
|
-
mongoid
|
92
|
+
mocha (~> 0.11.4)
|
93
|
+
mongoid
|
94
|
+
mongoid-grid_fs
|
95
|
+
pry
|
96
|
+
rack
|
48
97
|
shoulda (~> 2.11.3)
|
49
98
|
timecop
|
50
99
|
tzinfo
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/bin/mongoid_console
CHANGED
@@ -18,10 +18,16 @@ irb.context.evaluate("require 'irb/completion'", 0)
|
|
18
18
|
irb.context.evaluate(%@
|
19
19
|
include Mongoid
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['PADRINO_ENV'] || 'development'
|
22
|
+
|
23
|
+
if File.exist?("config/mongoid.yml")
|
24
|
+
Mongoid.load!("config/mongoid.yml", env)
|
25
|
+
elsif File.exist?("test/mongoid.yml")
|
26
|
+
Mongoid.load!("test/mongoid.yml", env)
|
27
|
+
elsif File.exist?("spec/mongoid.yml")
|
28
|
+
Mongoid.load!("test/mongoid.yml", env)
|
23
29
|
end
|
24
|
-
$db = Mongoid.
|
30
|
+
$db = Mongoid.session("default")
|
25
31
|
|
26
32
|
@, 0)
|
27
33
|
|
data/lib/mongoid_ext.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
|
+
require 'bundler/setup'
|
2
3
|
|
3
4
|
if RUBY_VERSION =~ /^1\.8/
|
4
5
|
$KCODE = 'u'
|
5
6
|
end
|
6
7
|
|
8
|
+
Bundler.require
|
7
9
|
require 'mongoid'
|
10
|
+
require 'mongoid-grid_fs'
|
8
11
|
require 'uuidtools'
|
9
12
|
require 'differ'
|
10
13
|
require 'active_support/inflector'
|
@@ -56,6 +59,8 @@ require 'mongoid_ext/modifiers'
|
|
56
59
|
|
57
60
|
module MongoidExt
|
58
61
|
def self.init
|
62
|
+
Mongoid::GridFS.file_model.field :_id, :type => String # to keep backwards compat
|
63
|
+
Mongoid.allow_dynamic_fields = true
|
59
64
|
load_jsfiles(::File.dirname(__FILE__)+"/mongoid_ext/js")
|
60
65
|
end
|
61
66
|
|
@@ -65,7 +70,9 @@ module MongoidExt
|
|
65
70
|
name = ::File.basename(js_path, ".js")
|
66
71
|
|
67
72
|
# HACK: looks like ruby driver doesn't support this
|
68
|
-
Mongoid.
|
73
|
+
Mongoid.sessions.each do |session_name, _|
|
74
|
+
Mongoid.session(session_name).command(:eval => "db.system.js.save({_id: '#{name}', value: #{code}})")
|
75
|
+
end
|
69
76
|
end
|
70
77
|
end
|
71
78
|
end
|
data/lib/mongoid_ext/file.rb
CHANGED
@@ -47,16 +47,21 @@ module MongoidExt
|
|
47
47
|
gridfs.put(io, options)
|
48
48
|
|
49
49
|
if file = self.get
|
50
|
-
file.
|
51
|
-
self['md5'] = file.server_md5
|
50
|
+
self['md5'] = file.md5
|
52
51
|
end
|
53
52
|
end
|
54
53
|
|
55
54
|
def get
|
56
55
|
@io ||= begin
|
57
|
-
|
58
|
-
|
59
|
-
|
56
|
+
io = nil
|
57
|
+
begin
|
58
|
+
io = gridfs.get(grid_filename)
|
59
|
+
def io.read
|
60
|
+
self.data
|
61
|
+
end
|
62
|
+
rescue Mongoid::Errors::DocumentNotFound => e
|
63
|
+
end
|
64
|
+
io
|
60
65
|
end
|
61
66
|
end
|
62
67
|
|
@@ -77,23 +82,45 @@ module MongoidExt
|
|
77
82
|
end
|
78
83
|
|
79
84
|
def read(size = nil)
|
80
|
-
|
85
|
+
if size != nil
|
86
|
+
puts "#{__FILE__}:#{__LINE__} Passing size to read() is deprecated and will be removed soon. Use .each {} to read in blocks."
|
87
|
+
end
|
88
|
+
|
89
|
+
self.get.data
|
81
90
|
end
|
82
91
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
92
|
+
def data
|
93
|
+
if self.get
|
94
|
+
self.get.data
|
95
|
+
else
|
96
|
+
puts "WARNING: the file you are trying to read doesn't exist: #{self.inspect}"
|
97
|
+
nil
|
98
|
+
end
|
86
99
|
end
|
87
100
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
f.send(name, *args, &block)
|
101
|
+
def each(&block)
|
102
|
+
if self.get
|
103
|
+
self.get.each(&block)
|
92
104
|
else
|
93
|
-
|
105
|
+
puts "WARNING: the file you are trying to read doesn't exist: #{self.inspect}"
|
106
|
+
nil
|
94
107
|
end
|
95
108
|
end
|
96
109
|
|
110
|
+
def delete
|
111
|
+
@io = nil
|
112
|
+
gridfs.delete(grid_filename)
|
113
|
+
end
|
114
|
+
|
115
|
+
#def method_missing(name, *args, &block)
|
116
|
+
#f = self.get
|
117
|
+
#if f && f.respond_to?(name)
|
118
|
+
#f.send(name, *args, &block)
|
119
|
+
#else
|
120
|
+
#super(name, *args, &block)
|
121
|
+
#end
|
122
|
+
#end
|
123
|
+
|
97
124
|
protected
|
98
125
|
def gridfs
|
99
126
|
_root_document.class.gridfs
|
@@ -44,11 +44,11 @@ module MongoidExt
|
|
44
44
|
end
|
45
45
|
|
46
46
|
id = id.to_s.gsub(".", "_")
|
47
|
-
file = self
|
47
|
+
file = self[id]
|
48
48
|
|
49
49
|
if file.nil?
|
50
50
|
file = self[id] = MongoidExt::File.new
|
51
|
-
elsif file.
|
51
|
+
elsif !file.kind_of?(MongoidExt::File) && file.kind_of?(::Hash)
|
52
52
|
file = self[id] = MongoidExt::File.new(file)
|
53
53
|
end
|
54
54
|
|
@@ -80,11 +80,13 @@ module MongoidExt
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
def
|
84
|
-
v
|
83
|
+
def self.mongoize(v)
|
84
|
+
Hash[v]
|
85
85
|
end
|
86
86
|
|
87
|
-
def
|
87
|
+
def self.demongoize(v)
|
88
|
+
return if v.nil?
|
89
|
+
|
88
90
|
doc = self.class.new
|
89
91
|
v.each do |k,v|
|
90
92
|
doc[k] = MongoidExt::File.new(v)
|
data/lib/mongoid_ext/filter.rb
CHANGED
@@ -11,7 +11,7 @@ module MongoidExt
|
|
11
11
|
extend ClassMethods
|
12
12
|
|
13
13
|
field :_keywords, :type => Set, :default => Set.new
|
14
|
-
index
|
14
|
+
index({:_keywords => 1})
|
15
15
|
|
16
16
|
before_save :_update_keywords
|
17
17
|
|
@@ -55,12 +55,27 @@ module MongoidExt
|
|
55
55
|
query = Mongoid::Criteria.new(self)
|
56
56
|
conds = query.where(opts).where(conds).selector
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
retval = Mongoid.session(:default).command({
|
59
|
+
:eval => "function(collection, q, config) { return filter(collection, q, config); }",
|
60
|
+
:args => [
|
61
|
+
self.collection_name,
|
62
|
+
conds,
|
63
|
+
{
|
64
|
+
:words => parsed_query[:words].to_a,
|
65
|
+
:stemmed => parsed_query[:stemmed].to_a,
|
66
|
+
:limit => limit,
|
67
|
+
:min_score => min_score,
|
68
|
+
:select => select
|
69
|
+
}
|
70
|
+
]
|
71
|
+
})['retval']
|
72
|
+
results = retval['results']
|
73
|
+
|
74
|
+
pagination = MongoidExt::Filter::ResultSet.new(retval["total_entries"], parsed_query, conds, page, limit)
|
75
|
+
|
76
|
+
pagination.subject = results.map do |result|
|
63
77
|
item = self.new(result['doc'])
|
78
|
+
item.id = result['doc']['_id']
|
64
79
|
item.search_score = result['score']
|
65
80
|
|
66
81
|
item
|
data/lib/mongoid_ext/paranoia.rb
CHANGED
@@ -75,7 +75,8 @@ module MongoidExt #:nodoc:
|
|
75
75
|
cattr_accessor :parent_class
|
76
76
|
self.parent_class = parent_klass
|
77
77
|
|
78
|
-
|
78
|
+
collection_name = "#{self.parent_class.collection.name}.trash"
|
79
|
+
self.storage_options = {:collection => collection_name}
|
79
80
|
|
80
81
|
field :document, :type => Hash
|
81
82
|
|
data/lib/mongoid_ext/random.rb
CHANGED
@@ -6,8 +6,8 @@ module MongoidExt
|
|
6
6
|
field :_random, :type => Float, :default => lambda{rand}
|
7
7
|
field :_random_times, :type => Float, :default => 0.0
|
8
8
|
|
9
|
-
index
|
10
|
-
index
|
9
|
+
index({:_random => 1})
|
10
|
+
index({:_random_times => 1})
|
11
11
|
end
|
12
12
|
|
13
13
|
module ClassMethods
|
@@ -20,4 +20,4 @@ module MongoidExt
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
data/lib/mongoid_ext/slugizer.rb
CHANGED
@@ -15,7 +15,8 @@ module MongoidExt
|
|
15
15
|
extend ClassMethods
|
16
16
|
extend Finder
|
17
17
|
|
18
|
-
field :slug, :type => String
|
18
|
+
field :slug, :type => String
|
19
|
+
index({:slug => 1})
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
@@ -26,7 +27,10 @@ module MongoidExt
|
|
26
27
|
protected
|
27
28
|
|
28
29
|
def generate_slug
|
29
|
-
|
30
|
+
if self[self.class.slug_key].blank?
|
31
|
+
Mongoid.logger.warn "Can't generate a slug because the given slug key is blank"
|
32
|
+
return false
|
33
|
+
end
|
30
34
|
max_length = self.class.slug_options[:max_length]
|
31
35
|
min_length = self.class.slug_options[:min_length] || 0
|
32
36
|
|