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.
@@ -41,22 +41,29 @@ module MongoidExt
41
41
 
42
42
  module ClassMethods
43
43
  def gridfs
44
- @gridfs ||= Mongo::Grid.new(self.db)
44
+ @gridfs ||= Mongoid::GridFS
45
45
  end
46
46
 
47
47
  def file_list(name)
48
48
  field name, :type => MongoidExt::FileList
49
+
49
50
  define_method(name) do
51
+ varname = "@file_list_#{name}"
52
+
53
+ return instance_variable_get(varname) if instance_variable_get(varname)
54
+
50
55
  list = self[name]
51
56
 
52
57
  if list.nil?
53
58
  list = self[name] = MongoidExt::FileList.new
54
- elsif list.class == BSON::OrderedHash || list.class == ::Hash
59
+ elsif list.kind_of?(::Hash)
55
60
  list = self[name] = MongoidExt::FileList.new(list)
56
61
  end
57
62
 
58
63
  list.parent_document = self
59
64
  list.list_name = name
65
+
66
+ instance_variable_set(varname, list)
60
67
  list
61
68
  end
62
69
 
@@ -66,7 +73,7 @@ module MongoidExt
66
73
 
67
74
  query = doc._updates
68
75
  if !query.blank?
69
- doc.collection.update({:_id => doc.id}, query)
76
+ doc.collection.find(:_id => doc.id).update(query, {:multi => true})
70
77
  end
71
78
  end
72
79
 
@@ -4,22 +4,53 @@ module MongoidExt
4
4
  klass.class_eval do
5
5
  extend ClassMethods
6
6
 
7
- field :tags, :type => Array, :index => true, :default => []
7
+ field :tags, :type => Array, :default => []
8
+ index({:tags => 1})
8
9
  end
9
10
  end
10
11
 
11
12
  module ClassMethods
12
13
  def tag_cloud(conditions = {}, limit = 30)
13
- self.db.nolock_eval("function(collection, q,l) { return tag_cloud(collection, q,l); }", self.collection_name, conditions, limit)
14
+ pipeline = []
15
+ if !conditions.blank?
16
+ match = {:$match => conditions }
17
+ pipeline << match
18
+ end
19
+
20
+ pipeline << {:$project => {:tags => 1}}
21
+ pipeline << {:$unwind => "$tags"}
22
+ pipeline << {:$group => {:_id => "$tags", :count => { :$sum => 1}}}
23
+ pipeline << {:$project => {:_id => 0, :name => '$_id', :count => 1}}
24
+ pipeline << {:$sort => {:count => -1}}
25
+ pipeline << {:$limit => limit}
26
+ self.collection.aggregate(pipeline)
14
27
  end
15
28
 
16
29
  # Model.find_with_tags("budget", "big").limit(4)
17
30
  def find_with_tags(*tags)
18
- self.all(:conditions => {:tags.in => tags})
31
+ self.where({:tags.in => tags})
19
32
  end
20
33
 
21
34
  def find_tags(regex, conditions = {}, limit = 30)
22
- self.db.nolock_eval("function(collection, a,b,c) { return find_tags(collection, a,b,c); }", self.collection_name, regex, conditions, limit)
35
+ pipeline = []
36
+ if regex.is_a? String
37
+ regex = /#{Regexp.escape(regex)}/
38
+ end
39
+ match = {:$match => {:tags => {:$in => [regex]}}}
40
+
41
+ if !conditions.blank?
42
+ match[:$match].merge! conditions
43
+ end
44
+ pipeline << match
45
+
46
+ pipeline << {:$project => {:tags => 1}}
47
+ pipeline << {:$unwind => "$tags"}
48
+ pipeline << {:$match => {:tags => regex}}
49
+ pipeline << {:$group => {:_id => "$tags", :count => { :$sum => 1}}}
50
+ pipeline << {:$project => {:_id => 0, :name => '$_id', :count => 1}}
51
+ pipeline << {:$sort => {:count => -1}}
52
+ pipeline << {:$limit => limit}
53
+ self.collection.aggregate(pipeline)
23
54
  end
24
55
  end
25
56
  end
@@ -1,5 +1,4 @@
1
1
  class EmbeddedHash < Hash
2
- include Mongoid::Fields::Serializable
3
2
  include ActiveModel::Validations
4
3
 
5
4
  def initialize(other = {})
@@ -40,12 +39,12 @@ class EmbeddedHash < Hash
40
39
  end
41
40
  alias :_id :id
42
41
 
43
- def serialize(v)
42
+ def self.mongoize(v)
44
43
  v
45
44
  end
46
45
 
47
- def deserialize(v)
48
- self.class.new(v)
46
+ def self.demongoize(v)
47
+ self.new(v)
49
48
  end
50
49
 
51
50
  # def method_missing(name, *args, &block)
@@ -54,7 +53,7 @@ class EmbeddedHash < Hash
54
53
 
55
54
  def assign_id
56
55
  if fetch("_id", nil).nil?
57
- self["_id"] = BSON::ObjectId.new.to_s
56
+ self["_id"] = Moped::BSON::ObjectId.new.to_s
58
57
  end
59
58
  end
60
59
  end
@@ -2,18 +2,22 @@ require 'ostruct'
2
2
 
3
3
  module MongoidExt
4
4
  class OpenStruct < ::OpenStruct
5
- include Mongoid::Fields::Serializable
6
-
7
- def serialize(value)
8
- value.nil? ? nil : value.to_hash
5
+ def mongoize
6
+ send(:table)
9
7
  end
10
8
 
11
- def deserialize(value)
12
- value.nil? ? nil : OpenStruct.new(value || {})
9
+ def self.demongoize(value)
10
+ value.nil? ? nil : OpenStruct.new(value)
13
11
  end
14
12
 
15
- def to_hash
16
- send(:table)
13
+ def self.mongoize(value)
14
+ if value.kind_of?(self)
15
+ value.mongoize
16
+ elsif value.kind_of?(Hash)
17
+ value
18
+ else
19
+ nil
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -1,7 +1,5 @@
1
1
  class Timestamp
2
- include Mongoid::Fields::Serializable
3
-
4
- def deserialize(value)
2
+ def self.demongoize(value)
5
3
  if value.nil? || value == ''
6
4
  nil
7
5
  else
@@ -9,7 +7,7 @@ class Timestamp
9
7
  end
10
8
  end
11
9
 
12
- def serialize(value)
10
+ def self.mongoize(value)
13
11
  value.to_i
14
12
  end
15
13
  end
@@ -1,6 +1,4 @@
1
1
  class Translation < String
2
- include Mongoid::Fields::Serializable
3
-
4
2
  attr_accessor :keys
5
3
 
6
4
  def initialize(*args)
@@ -35,13 +33,13 @@ class Translation < String
35
33
  tr
36
34
  end
37
35
 
38
- def serialize(value)
36
+ def self.mongoize(value)
39
37
  return value.keys if value.kind_of?(self)
40
38
 
41
39
  @keys
42
40
  end
43
41
 
44
- def deserialize(value)
42
+ def self.demongoize(value)
45
43
  return value if value.kind_of?(self)
46
44
 
47
45
  result = self.new
@@ -112,15 +112,16 @@ module Versioning
112
112
  cattr_accessor :parent_class
113
113
  self.parent_class = parent_klass
114
114
 
115
- self.collection_name = "#{self.parent_class.collection_name}.versions"
115
+ self.storage_options = {
116
+ :collection => "#{self.parent_class.collection_name}.versions"
117
+ }
116
118
 
117
- identity :type => String
118
119
  field :message, :type => String
119
120
  field :data, :type => Hash
120
121
 
121
- referenced_in :owner, :class_name => parent_klass.versionable_options[:user_class]
122
+ belongs_to :owner, :class_name => parent_klass.versionable_options[:user_class]
122
123
 
123
- referenced_in :target, :polymorphic => true
124
+ belongs_to :target, :polymorphic => true
124
125
 
125
126
  after_create :add_version
126
127
 
@@ -180,7 +181,7 @@ module Versioning
180
181
  end
181
182
 
182
183
  uuser_id = send(self.versionable_options[:owner_field]+"_was")||send(self.versionable_options[:owner_field])
183
- if !self.new? && !data.empty? && uuser_id
184
+ if !self.new_record? && !data.empty? && uuser_id
184
185
  max_versions = self.versionable_options[:max_versions].to_i
185
186
  if max_versions > 0 && self.version_ids.size >= max_versions
186
187
  old = self.version_ids.slice!(0, max_versions-1)
@@ -190,7 +191,7 @@ module Versioning
190
191
  self.class.set_callback(:save, :before, :save_version)
191
192
  end
192
193
 
193
- self.version_klass.create({
194
+ self.version_klass.create!({
194
195
  'data' => data,
195
196
  'owner_id' => uuser_id,
196
197
  'target' => self,
@@ -15,31 +15,36 @@ module MongoidExt
15
15
  if self[:votes] && !self[:votes].empty?
16
16
  self[:votes].include?(voter_id)
17
17
  else
18
- self.class.exists?(:conditions => {:_id => self.id, :"votes.#{voter_id}".exists => true})
18
+ self.class.where({:_id => self.id, :"votes.#{voter_id}".exists => true}).exists?
19
19
  end
20
20
  end
21
21
 
22
22
  def vote!(value, voter_id, &block)
23
+ value = value.to_i
24
+ voter_id = voter_id.to_s
25
+
23
26
  old_vote = self.votes[voter_id]
24
27
  if !old_vote
25
28
  self.votes[voter_id] = value
26
- self.save(:validate => false)
27
-
28
- add_vote!(value, voter_id, &block)
29
- return :created
29
+ if self.save
30
+ add_vote!(value, voter_id, &block)
31
+ return :created
32
+ end
30
33
  else
31
34
  if(old_vote != value)
32
35
  self.votes[voter_id] = value
33
- self.save
34
- self.remove_vote!(old_vote, voter_id, &block)
35
- self.add_vote!(value, voter_id, &block)
36
+ if self.save
37
+ self.remove_vote!(old_vote, voter_id, &block)
38
+ self.add_vote!(value, voter_id, &block)
36
39
 
37
- return :updated
40
+ return :updated
41
+ end
38
42
  else
39
43
  self.votes.delete(voter_id)
40
- self.save(:validate => false)
41
- remove_vote!(value, voter_id, &block)
42
- return :destroyed
44
+ if self.save
45
+ remove_vote!(value, voter_id, &block)
46
+ return :destroyed
47
+ end
43
48
  end
44
49
  end
45
50
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mongoid_ext"
8
- s.version = "0.7.1"
8
+ s.version = "0.9.0"
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 = "2012-07-27"
12
+ s.date = "2013-11-03"
13
13
  s.description = "mongoid plugins"
14
14
  s.email = "krawek@gmail.com"
15
15
  s.executables = ["mongoid_console"]
@@ -57,6 +57,7 @@ Gem::Specification.new do |s|
57
57
  "mongoid_ext.gemspec",
58
58
  "test/helper.rb",
59
59
  "test/models.rb",
60
+ "test/mongoid.yml",
60
61
  "test/support/custom_matchers.rb",
61
62
  "test/test_encryptor.rb",
62
63
  "test/test_filter.rb",
@@ -82,51 +83,57 @@ Gem::Specification.new do |s|
82
83
  s.specification_version = 3
83
84
 
84
85
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
85
- s.add_runtime_dependency(%q<mongoid>, ["~> 2.3"])
86
+ s.add_runtime_dependency(%q<mongoid>, [">= 0"])
86
87
  s.add_runtime_dependency(%q<uuidtools>, [">= 2.1.1"])
87
88
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
88
89
  s.add_runtime_dependency(%q<tzinfo>, [">= 0"])
89
90
  s.add_runtime_dependency(%q<differ>, [">= 0.1.2"])
90
91
  s.add_runtime_dependency(%q<encryptor>, ["~> 1.1.3"])
92
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
93
+ s.add_runtime_dependency(%q<mongoid-grid_fs>, [">= 0"])
91
94
  s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
92
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
93
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
95
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
94
96
  s.add_development_dependency(%q<shoulda>, ["~> 2.11.3"])
95
97
  s.add_development_dependency(%q<jnunemaker-matchy>, ["~> 0.4"])
96
98
  s.add_development_dependency(%q<shoulda>, ["~> 2.11.3"])
97
- s.add_development_dependency(%q<mocha>, ["~> 0.9.4"])
99
+ s.add_development_dependency(%q<mocha>, ["~> 0.11.4"])
98
100
  s.add_development_dependency(%q<timecop>, [">= 0"])
101
+ s.add_development_dependency(%q<pry>, [">= 0"])
99
102
  else
100
- s.add_dependency(%q<mongoid>, ["~> 2.3"])
103
+ s.add_dependency(%q<mongoid>, [">= 0"])
101
104
  s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
102
105
  s.add_dependency(%q<i18n>, [">= 0"])
103
106
  s.add_dependency(%q<tzinfo>, [">= 0"])
104
107
  s.add_dependency(%q<differ>, [">= 0.1.2"])
105
108
  s.add_dependency(%q<encryptor>, ["~> 1.1.3"])
109
+ s.add_dependency(%q<rack>, [">= 0"])
110
+ s.add_dependency(%q<mongoid-grid_fs>, [">= 0"])
106
111
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
107
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
108
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
112
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
109
113
  s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
110
114
  s.add_dependency(%q<jnunemaker-matchy>, ["~> 0.4"])
111
115
  s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
112
- s.add_dependency(%q<mocha>, ["~> 0.9.4"])
116
+ s.add_dependency(%q<mocha>, ["~> 0.11.4"])
113
117
  s.add_dependency(%q<timecop>, [">= 0"])
118
+ s.add_dependency(%q<pry>, [">= 0"])
114
119
  end
115
120
  else
116
- s.add_dependency(%q<mongoid>, ["~> 2.3"])
121
+ s.add_dependency(%q<mongoid>, [">= 0"])
117
122
  s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
118
123
  s.add_dependency(%q<i18n>, [">= 0"])
119
124
  s.add_dependency(%q<tzinfo>, [">= 0"])
120
125
  s.add_dependency(%q<differ>, [">= 0.1.2"])
121
126
  s.add_dependency(%q<encryptor>, ["~> 1.1.3"])
127
+ s.add_dependency(%q<rack>, [">= 0"])
128
+ s.add_dependency(%q<mongoid-grid_fs>, [">= 0"])
122
129
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
123
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
124
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
130
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
125
131
  s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
126
132
  s.add_dependency(%q<jnunemaker-matchy>, ["~> 0.4"])
127
133
  s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
128
- s.add_dependency(%q<mocha>, ["~> 0.9.4"])
134
+ s.add_dependency(%q<mocha>, ["~> 0.11.4"])
129
135
  s.add_dependency(%q<timecop>, [">= 0"])
136
+ s.add_dependency(%q<pry>, [">= 0"])
130
137
  end
131
138
  end
132
139
 
@@ -7,6 +7,7 @@ require 'shoulda'
7
7
  require 'timecop'
8
8
  require 'mocha'
9
9
  require 'pp'
10
+ require 'pry'
10
11
 
11
12
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
13
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -14,16 +15,14 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
15
  require 'support/custom_matchers'
15
16
  require 'mongoid_ext'
16
17
 
17
- Mongoid.configure do |config|
18
- config.master = Mongo::Connection.new.db("test")
19
- end
20
-
18
+ Mongoid.load!("test/mongoid.yml", "test")
21
19
  require 'models'
22
20
 
21
+
23
22
  class Test::Unit::TestCase
24
23
  include CustomMatchers
25
24
  end
26
25
 
27
26
  MongoidExt.init
28
27
 
29
- $VERBOSE=nil
28
+ $VERBOSE=nil
@@ -69,7 +69,7 @@ class BlogPost # for Slug and Filter
69
69
  field :tags, :type => Array
70
70
  field :date, :type => Time
71
71
 
72
- referenced_in :updated_by, :class_name => "User"
72
+ belongs_to :updated_by, :class_name => "User"
73
73
 
74
74
  versionable_keys :title, :body, :tags, :owner_field => "updated_by_id", :max_versions => 2
75
75
 
@@ -0,0 +1,19 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ hosts:
5
+ - localhost:27017
6
+ database: mongoidext-test
7
+ options:
8
+ safe: true
9
+ options:
10
+ allow_dynamic_fields: false
11
+ identity_map_enabled: true
12
+ include_root_in_json: false
13
+ include_type_for_serialization: true
14
+ preload_models: false
15
+ scope_overwrite_exception: true
16
+ raise_not_found_error: false
17
+ skip_version_check: false
18
+ use_activesupport_time_zone: true
19
+ use_utc: true
@@ -24,7 +24,7 @@ class StorageTest < Test::Unit::TestCase
24
24
  should "not fail with nil values" do
25
25
  @cc.data = nil
26
26
  @cc.save
27
- @cc.reload
27
+ @cc = CreditCard.find(@cc.id)
28
28
  @cc.data.should == nil
29
29
  end
30
30
  end