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 CHANGED
@@ -1,21 +1,23 @@
1
- source :gemcutter
1
+ source "https://rubygems.org"
2
2
 
3
- gem 'mongoid', '~> 2.3'
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 "bundler", "~> 1.0.0"
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.9.4'
19
+ gem 'mocha', '~> 0.11.4'
19
20
  gem 'timecop'
21
+ gem 'pry'
20
22
  end
21
23
 
@@ -1,50 +1,99 @@
1
1
  GEM
2
- remote: http://rubygems.org/
2
+ remote: https://rubygems.org/
3
3
  specs:
4
- activemodel (3.2.1)
5
- activesupport (= 3.2.1)
4
+ activemodel (3.2.15)
5
+ activesupport (= 3.2.15)
6
6
  builder (~> 3.0.0)
7
- activesupport (3.2.1)
8
- i18n (~> 0.6)
7
+ activesupport (3.2.15)
8
+ i18n (~> 0.6, >= 0.6.4)
9
9
  multi_json (~> 1.0)
10
- bson (1.5.2)
11
- builder (3.0.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
- git (1.2.5)
15
- i18n (0.6.0)
16
- jeweler (1.5.2)
17
- bundler (~> 1.0.0)
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
- mocha (0.9.12)
22
- mongo (1.5.2)
23
- bson (= 1.5.2)
24
- mongoid (2.4.4)
25
- activemodel (~> 3.1)
26
- mongo (~> 1.3)
27
- tzinfo (~> 0.3.22)
28
- multi_json (1.0.4)
29
- rake (0.9.2.2)
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
- timecop (0.3.5)
32
- tzinfo (0.3.31)
33
- uuidtools (2.1.2)
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.5.2)
90
+ jeweler (~> 1.8.4)
45
91
  jnunemaker-matchy (~> 0.4)
46
- mocha (~> 0.9.4)
47
- mongoid (~> 2.3)
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.7.1
1
+ 0.9.0
@@ -18,10 +18,16 @@ irb.context.evaluate("require 'irb/completion'", 0)
18
18
  irb.context.evaluate(%@
19
19
  include Mongoid
20
20
 
21
- Mongoid.configure do |config|
22
- config.master = Mongo::Connection.new.db("test")
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.config.database
30
+ $db = Mongoid.session("default")
25
31
 
26
32
  @, 0)
27
33
 
@@ -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.master.eval("db.system.js.save({_id: '#{name}', value: #{code}})")
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
@@ -25,7 +25,7 @@ module MongoidExt
25
25
  marshaled = Marshal.dump(v)
26
26
  enc_value = ::Encryptor.encrypt(marshaled, :key => key).unpack('H*')[0]
27
27
 
28
- write_attribute(name, enc_value)
28
+ attributes[name.to_s] = enc_value
29
29
  end
30
30
  end
31
31
  end
@@ -47,16 +47,21 @@ module MongoidExt
47
47
  gridfs.put(io, options)
48
48
 
49
49
  if file = self.get
50
- file.send(:get_md5)
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
- gridfs.get(grid_filename)
58
- rescue Mongo::GridFileNotFound
59
- return nil
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
- self.get.read(size) rescue nil
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 delete
84
- @io = nil
85
- gridfs.delete(grid_filename)
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 method_missing(name, *args, &block)
89
- f = self.get
90
- if f && f.respond_to?(name)
91
- f.send(name, *args, &block)
101
+ def each(&block)
102
+ if self.get
103
+ self.get.each(&block)
92
104
  else
93
- super(name, *args, &block)
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.fetch(id, nil)
47
+ file = self[id]
48
48
 
49
49
  if file.nil?
50
50
  file = self[id] = MongoidExt::File.new
51
- elsif file.class == ::Hash || file.class == BSON::OrderedHash
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 serialize(v)
84
- v
83
+ def self.mongoize(v)
84
+ Hash[v]
85
85
  end
86
86
 
87
- def deserialize(v)
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)
@@ -58,10 +58,8 @@ class FileServer
58
58
  end
59
59
 
60
60
  def each
61
- f = @file.get
62
- while part = f.read(8192)
61
+ @file.each do |part|
63
62
  yield part
64
- break if part.empty?
65
63
  end
66
64
  end
67
65
  end
@@ -11,7 +11,7 @@ module MongoidExt
11
11
  extend ClassMethods
12
12
 
13
13
  field :_keywords, :type => Set, :default => Set.new
14
- index :_keywords
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
- results = self.db.nolock_eval("function(collection, q, config) { return filter(collection, q, config); }", self.collection_name, conds, {:words => parsed_query[:words].to_a, :stemmed => parsed_query[:stemmed].to_a, :limit => limit, :min_score => min_score, :select => select })
59
-
60
- pagination = MongoidExt::Filter::ResultSet.new(results["total_entries"], parsed_query, conds, page, limit)
61
-
62
- pagination.subject = results['results'].map do |result|
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
@@ -50,7 +50,7 @@ module MongoidExt
50
50
 
51
51
  private
52
52
  def _apply_modifier(modifier, conditions, update)
53
- collection.update(conditions, {modifier => update}, :multi => true)
53
+ collection.find(conditions).update_all({modifier => update})
54
54
  end
55
55
  end
56
56
 
@@ -75,7 +75,8 @@ module MongoidExt #:nodoc:
75
75
  cattr_accessor :parent_class
76
76
  self.parent_class = parent_klass
77
77
 
78
- self.collection_name = "#{self.parent_class.collection_name}.trash"
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
 
@@ -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 :_random
10
- index :_random_times
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
@@ -15,7 +15,8 @@ module MongoidExt
15
15
  extend ClassMethods
16
16
  extend Finder
17
17
 
18
- field :slug, :type => String, :index => true
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
- return false if self[self.class.slug_key].blank?
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