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.
@@ -86,7 +86,7 @@ module MongoidExt #:nodoc:
86
86
  end
87
87
 
88
88
  def self.compact!(date = 1.month.ago)
89
- self.delete_all(:created_at.lte => date)
89
+ self.delete_all(:created_at => {:$lte => date.to_time})
90
90
  end
91
91
 
92
92
  private
@@ -14,4 +14,17 @@ module Mongo
14
14
  doc['retval']
15
15
  end
16
16
  end
17
- end
17
+ end
18
+
19
+ module Mongoid
20
+ module Document
21
+ def self.included(base)
22
+ models << base.to_s
23
+ super
24
+ end
25
+
26
+ def self.models
27
+ @models ||= []
28
+ end
29
+ end
30
+ end
@@ -1,12 +1,10 @@
1
1
  module MongoidExt
2
2
  module Storage
3
- def self.included(model)
4
- model.class_eval do
5
- extend ClassMethods
3
+ extend ActiveSupport::Concern
6
4
 
7
- validate :add_mm_storage_errors
8
- file_list :file_list
9
- end
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 mm_storage_errors
33
- @mm_storage_errors ||= {}
30
+ def storage_errors
31
+ @storage_errors ||= {}
34
32
  end
35
33
 
36
- def add_mm_storage_errors
37
- mm_storage_errors.each do |k, msgs|
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
- doc.save(:validate => false)
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]).get(name.to_s).put(name.to_s, file)
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
- mm_storage_errors.merge!(errors.errors)
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
- other.each do |k,v|
6
- self[k] = v
6
+ super()
7
+
8
+ if other
9
+ other.each do |k,v|
10
+ self[k] = v
11
+ end
7
12
  end
8
- self["_id"] ||= BSON::ObjectId.new.to_s
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
- self[name.to_s] ||= opts[:default].kind_of?(Proc) ? opts[:default].call : opts[:default]
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
- self["_id"]
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
- class OpenStruct
4
- def self.set(value)
5
- value.nil? ? nil : value.to_hash
6
- end
3
+ module MongoidExt
4
+ class OpenStruct < ::OpenStruct
5
+ include Mongoid::Fields::Serializable
7
6
 
8
- def self.get(value)
9
- value.nil? ? nil : OpenStruct.new(value || {})
10
- end
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
- def to_hash
13
- send(:table)
15
+ def to_hash
16
+ send(:table)
17
+ end
14
18
  end
15
19
  end
@@ -1,15 +1,17 @@
1
1
  class Timestamp
2
- def self.get(value)
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 self.set(value)
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 self.set(value)
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 self.get(value)
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
- module InstanceMethods
21
- def rollback!(pos = nil)
22
- pos = self.versions_count-1 if pos.nil?
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
- if version
26
- version.data.each do |key, value|
27
- self.send("#{key}=", value)
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
- @rolling_back = true
36
- r = save!
37
- @rolling_back = false
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
- def load_version(pos = nil)
42
- pos = self.versions_count-1 if pos.nil?
43
- version = self.version_at(pos)
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
- if version
46
- version.data.each do |key, value|
47
- self.send("#{key}=", value)
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
- def diff(key, pos1, pos2, format = :html)
53
- version1 = self.version_at(pos1)
54
- version2 = self.version_at(pos2)
50
+ def diff(key, pos1, pos2, format = :html)
51
+ version1 = self.version_at(pos1)
52
+ version2 = self.version_at(pos2)
55
53
 
56
- Differ.diff(version1.content(key), version2.content(key)).format_as(format).html_safe
57
- end
54
+ Differ.diff(version1.content(key), version2.content(key)).format_as(format).html_safe
55
+ end
58
56
 
59
- def diff_by_word(key, pos1, pos2, format = :html)
60
- version1 = self.version_at(pos1)
61
- version2 = self.version_at(pos2)
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
- Differ.diff_by_word(version1.content(key), version2.content(key)).format_as(format).html_safe
64
- end
61
+ Differ.diff_by_word(version1.content(key), version2.content(key)).format_as(format).html_safe
62
+ end
65
63
 
66
- def diff_by_line(key, pos1, pos2, format = :html)
67
- version1 = self.version_at(pos1)
68
- version2 = self.version_at(pos2)
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
- Differ.diff_by_line(version1.content(key), version2.content(key)).format_as(format).html_safe
71
- end
68
+ Differ.diff_by_line(version1.content(key), version2.content(key)).format_as(format).html_safe
69
+ end
72
70
 
73
- def diff_by_char(key, pos1, pos2, format = :html)
74
- version1 = self.version_at(pos1)
75
- version2 = self.version_at(pos2)
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
- Differ.diff_by_char(version1.content(key), version2.content(key)).format_as(format).html_safe
78
- end
75
+ Differ.diff_by_char(version1.content(key), version2.content(key)).format_as(format).html_safe
76
+ end
79
77
 
80
- def current_version
81
- 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)
82
- end
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
- def version_at(pos)
85
- case pos.to_s
86
- when "current"
87
- current_version
88
- when "first"
89
- version_klass.find(self.version_ids.first)
90
- when "last"
91
- version_klass.find(self.version_ids.last)
92
- else
93
- if version_id = self.version_ids[pos]
94
- version_klass.find(self.version_ids[pos])
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
- def versions
100
- version_klass.where(:target_id => self.id)
101
- end
97
+ def versions
98
+ version_klass.where(:target_id => self.id)
99
+ end
102
100
 
103
- def version_klass
104
- self.class.version_klass
105
- end
101
+ def version_klass
102
+ self.class.version_klass
106
103
  end
107
104
 
108
105
  module ClassMethods
@@ -46,12 +46,12 @@ module MongoidExt
46
46
 
47
47
  def add_vote!(value, voter_id, &block)
48
48
  if embedded?
49
- updates = {self._position+".votes_count" => 1,
50
- self._position+".votes_average" => value.to_i}
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._position+".votes_up"] = 1
52
+ updates[self.atomic_position+".votes_up"] = 1
53
53
  elsif value == -1
54
- updates[self._position+".votes_down"] = 1
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._position+".votes_count" => -1,
77
- self._position+".votes_average" => -value.to_i}
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._position+".votes_up"] = -1
79
+ updates[self.atomic_position+".votes_up"] = -1
80
80
  elsif value == -1
81
- updates[self._position+".votes_down"] = -1
81
+ updates[self.atomic_position+".votes_down"] = -1
82
82
  end
83
83
 
84
84
  self._parent.increment(updates)
@@ -4,15 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{mongoid_ext}
8
- s.version = "0.6.2"
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 = %q{2011-07-15}
13
- s.default_executable = %q{mongoid_console}
14
- s.description = %q{mongoid plugins}
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 = %q{http://github.com/dcu/mongoid_ext}
76
+ s.homepage = "http://github.com/dcu/mongoid_ext"
78
77
  s.require_paths = ["lib"]
79
- s.rubygems_version = %q{1.3.7}
80
- s.summary = %q{mongoid plugins}
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"])