mongomodel 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
@@ -3,12 +3,12 @@ module MongoModel
|
|
3
3
|
# named created_at/created_on or updated_at/updated_on.
|
4
4
|
module Timestamps #:nodoc:
|
5
5
|
extend ActiveSupport::Concern
|
6
|
-
|
6
|
+
|
7
7
|
included do
|
8
8
|
before_save :set_update_timestamps
|
9
9
|
before_create :set_create_timestamps
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
module ClassMethods
|
13
13
|
# Defines timestamp properties created_at and updated_at.
|
14
14
|
# When the document is created or updated, these properties will be respectively updated.
|
@@ -17,13 +17,13 @@ module MongoModel
|
|
17
17
|
property :updated_at, Time
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
private
|
22
22
|
def set_update_timestamps
|
23
23
|
write_attribute(:updated_at, Time.now) if properties.include?(:updated_at)
|
24
24
|
write_attribute(:updated_on, Time.now) if properties.include?(:updated_on)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def set_create_timestamps
|
28
28
|
write_attribute(:created_at, Time.now) if properties.include?(:created_at) && !query_attribute(:created_at)
|
29
29
|
write_attribute(:created_on, Time.now) if properties.include?(:created_on) && !query_attribute(:created_on)
|
@@ -1,23 +1,23 @@
|
|
1
1
|
module MongoModel
|
2
2
|
module Validations
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
4
|
+
|
5
5
|
include ActiveModel::Validations
|
6
|
-
|
6
|
+
|
7
7
|
module ClassMethods
|
8
8
|
def property(name, *args, &block) #:nodoc:
|
9
9
|
property = super
|
10
|
-
|
10
|
+
|
11
11
|
if property.validate?
|
12
12
|
validates_associated(name) if property.embeddable?
|
13
13
|
validates_presence_of(name) if property.options[:required]
|
14
14
|
validates_format_of(name, property.options[:format]) if property.options[:format]
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
property
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def valid?(context=nil)
|
22
22
|
context ||= new_record? ? :create : :update
|
23
23
|
super
|
data/lib/mongomodel/document.rb
CHANGED
@@ -8,20 +8,20 @@ module MongoModel
|
|
8
8
|
def ==(other)
|
9
9
|
self.class == other.class && id == other.id
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
include DocumentExtensions::Persistence
|
13
13
|
include DocumentExtensions::OptimisticLocking
|
14
14
|
include DocumentExtensions::CollectionModifiers
|
15
|
-
|
15
|
+
|
16
16
|
extend DocumentExtensions::DynamicFinders
|
17
17
|
include DocumentExtensions::Indexes
|
18
|
-
|
18
|
+
|
19
19
|
include DocumentExtensions::Scopes
|
20
20
|
include DocumentExtensions::Validations
|
21
|
-
|
22
|
-
|
21
|
+
prepend DocumentExtensions::Callbacks
|
22
|
+
|
23
23
|
self.abstract_class = true
|
24
|
-
|
24
|
+
|
25
25
|
class_attribute :per_page
|
26
26
|
self.per_page = 20
|
27
27
|
end
|
@@ -2,43 +2,37 @@ module MongoModel
|
|
2
2
|
module DocumentExtensions
|
3
3
|
module Callbacks
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
alias_method_chain method, :callbacks
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def instantiate_with_callbacks(*args) #:nodoc:
|
13
|
-
instantiate_without_callbacks(*args)
|
5
|
+
|
6
|
+
def instantiate(*) #:nodoc:
|
7
|
+
super
|
14
8
|
run_callbacks_with_embedded(:find)
|
15
9
|
end
|
16
|
-
private :
|
10
|
+
private :instantiate
|
17
11
|
|
18
|
-
def
|
12
|
+
def create_or_update #:nodoc:
|
19
13
|
run_callbacks_with_embedded(:save) do
|
20
|
-
|
14
|
+
super
|
21
15
|
end
|
22
16
|
end
|
23
|
-
private :
|
17
|
+
private :create_or_update
|
24
18
|
|
25
|
-
def
|
19
|
+
def create #:nodoc:
|
26
20
|
run_callbacks_with_embedded(:create) do
|
27
|
-
|
21
|
+
super
|
28
22
|
end
|
29
23
|
end
|
30
|
-
private :
|
24
|
+
private :create
|
31
25
|
|
32
|
-
def
|
26
|
+
def update(*) #:nodoc:
|
33
27
|
run_callbacks_with_embedded(:update) do
|
34
|
-
|
28
|
+
super
|
35
29
|
end
|
36
30
|
end
|
37
|
-
private :
|
31
|
+
private :update
|
38
32
|
|
39
|
-
def
|
33
|
+
def destroy #:nodoc:
|
40
34
|
run_callbacks_with_embedded(:destroy) do
|
41
|
-
|
35
|
+
super
|
42
36
|
end
|
43
37
|
end
|
44
38
|
end
|
@@ -2,21 +2,21 @@ module MongoModel
|
|
2
2
|
module DocumentExtensions
|
3
3
|
module CollectionModifiers
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
METHODS = [ :increment!, :increase!, :set!, :unset!, :push!, :push_all!, :add_to_set!, :pull!, :pull_all!, :pop!, :shift!, :rename! ]
|
7
|
-
|
7
|
+
|
8
8
|
# Define methods manually rather than use Module#delegate as it raises false deprecation warnings.
|
9
9
|
METHODS.each do |modifier|
|
10
10
|
define_method(modifier) do |*args|
|
11
11
|
instance_scope.send(modifier, *args)
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
private
|
16
16
|
def instance_scope
|
17
17
|
self.class.where(:id => id)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
module ClassMethods
|
21
21
|
# Post.increment!(:hits => 1, :available => -1)
|
22
22
|
# This method is also aliased as increase!
|
@@ -45,7 +45,7 @@ module MongoModel
|
|
45
45
|
def push_all!(args)
|
46
46
|
collection_modifier_update('$pushAll', args)
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
# Post.add_to_set!(:tags => 'xxx')
|
50
50
|
def add_to_set!(args)
|
51
51
|
collection_modifier_update('$addToSet', args)
|
@@ -4,26 +4,26 @@ module MongoModel
|
|
4
4
|
module DocumentExtensions
|
5
5
|
module Indexes
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
7
|
+
|
8
8
|
included do
|
9
9
|
index :_type
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
module ClassMethods
|
13
13
|
def property(name, *args, &block) #:nodoc:
|
14
14
|
property = super
|
15
15
|
index(name) if property.options[:index]
|
16
16
|
property
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def indexes
|
20
20
|
@_indexes ||= []
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def indexes=(indexes)
|
24
24
|
@_indexes = indexes
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def index(*args)
|
28
28
|
index = Index.new(*args)
|
29
29
|
self.indexes << index
|
@@ -42,7 +42,7 @@ module MongoModel
|
|
42
42
|
|
43
43
|
@_indexes_initialized = true
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def inherited(subclass)
|
47
47
|
super
|
48
48
|
subclass.indexes = indexes.dup
|
@@ -50,41 +50,41 @@ module MongoModel
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
class Index
|
55
55
|
def initialize(*keys)
|
56
56
|
options = keys.extract_options!
|
57
|
-
|
57
|
+
|
58
58
|
@name = options.delete(:name)
|
59
59
|
@unique = options.delete(:unique)
|
60
60
|
@min = options.delete(:min)
|
61
61
|
@max = options.delete(:max)
|
62
|
-
|
62
|
+
|
63
63
|
keys.each do |key|
|
64
64
|
self.keys[key.to_sym] = :ascending
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
options.each do |key, order|
|
68
68
|
self.keys[key.to_sym] = order
|
69
69
|
end
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
def keys
|
73
73
|
@keys ||= ActiveSupport::OrderedHash.new
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
def unique?
|
77
77
|
@unique
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
def geo2d?
|
81
81
|
@geo2d ||= keys.size == 1 && keys.values.first == :geo2d
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
def to_args
|
85
85
|
args = []
|
86
86
|
options = {}
|
87
|
-
|
87
|
+
|
88
88
|
if geo2d?
|
89
89
|
args << [[keys.keys.first, Mongo::GEO2D]]
|
90
90
|
elsif keys.size == 1 && keys.values.first == :ascending
|
@@ -92,19 +92,19 @@ module MongoModel
|
|
92
92
|
else
|
93
93
|
args << keys.map { |k, o| [k, o == :ascending ? Mongo::ASCENDING : Mongo::DESCENDING] }.sort_by { |k| k.first.to_s }
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
if geo2d? && @min && @max
|
97
97
|
options[:min] = @min
|
98
98
|
options[:max] = @max
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
options[:unique] = true if unique?
|
102
102
|
options[:name] = @name if @name
|
103
|
-
|
103
|
+
|
104
104
|
args << options if options.any?
|
105
105
|
args
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
def ==(other)
|
109
109
|
other.is_a?(Index) && to_args == other.to_args
|
110
110
|
end
|
@@ -2,12 +2,12 @@ module MongoModel
|
|
2
2
|
module DocumentExtensions
|
3
3
|
module OptimisticLocking
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
module ClassMethods
|
7
7
|
def locking_enabled?
|
8
8
|
properties.include?(:_lock_version)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def lock_optimistically=(value)
|
12
12
|
if value == true
|
13
13
|
property :_lock_version, Integer, :default => 0, :internal => true, :protected => true
|
@@ -17,24 +17,24 @@ module MongoModel
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def locking_enabled?
|
22
22
|
self.class.locking_enabled?
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
private
|
26
26
|
def increment_lock_version
|
27
27
|
self._lock_version += 1
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def save_to_collection
|
31
31
|
if locking_enabled? && _lock_version > 1
|
32
32
|
begin
|
33
33
|
collection.update({ '_id' => id.to_mongo, '_lock_version' => _lock_version-1 }, to_mongo)
|
34
34
|
success = database.get_last_error['updatedExisting']
|
35
|
-
|
35
|
+
|
36
36
|
self._lock_version -= 1 unless success
|
37
|
-
|
37
|
+
|
38
38
|
success
|
39
39
|
rescue Mongo::OperationFailure => e
|
40
40
|
false
|
@@ -2,24 +2,24 @@ module MongoModel
|
|
2
2
|
module DocumentExtensions
|
3
3
|
module Persistence
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
included do
|
7
7
|
undef_method :id if method_defined?(:id)
|
8
8
|
property :id, MongoModel::Reference, :as => '_id', :default => lambda { |doc| doc.generate_id }
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
# Reload the document from the database. If the document
|
12
12
|
# hasn't been saved, this method will raise an error.
|
13
13
|
def reload
|
14
14
|
reloaded = self.class.unscoped.find(id)
|
15
|
-
|
15
|
+
|
16
16
|
attributes.clear
|
17
17
|
attributes.load!(reloaded.attributes.to_mongo)
|
18
|
-
|
18
|
+
|
19
19
|
associations.values.each do |association|
|
20
20
|
association.proxy.reset
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
self
|
24
24
|
end
|
25
25
|
|
@@ -32,7 +32,7 @@ module MongoModel
|
|
32
32
|
def save!(*)
|
33
33
|
create_or_update || raise(DocumentNotSaved)
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def delete
|
37
37
|
self.class.unscoped.delete(id)
|
38
38
|
set_destroyed(true)
|
@@ -43,7 +43,7 @@ module MongoModel
|
|
43
43
|
def destroy
|
44
44
|
delete
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
# Updates all the attributes from the passed-in Hash and saves the document.
|
48
48
|
# If the object is invalid, the saving will fail and false will be returned.
|
49
49
|
#
|
@@ -55,35 +55,35 @@ module MongoModel
|
|
55
55
|
self.assign_attributes(attributes, options)
|
56
56
|
save
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
# Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
|
60
60
|
# of +save+, so an exception is raised if the docuemnt is invalid.
|
61
61
|
def update_attributes!(attributes, options={})
|
62
62
|
self.assign_attributes(attributes, options)
|
63
63
|
save!
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
# Updates a single attribute and saves the document without going through the normal validation procedure.
|
67
67
|
# This is especially useful for boolean flags on existing documents.
|
68
68
|
def update_attribute(name, value)
|
69
69
|
send("#{name}=", value)
|
70
70
|
save(:validate => false)
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def collection
|
74
74
|
self.class.collection
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
def database
|
78
78
|
self.class.database
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
# Generate a new BSON::ObjectId for the record.
|
82
82
|
# Override in subclasses for custom ID generation.
|
83
83
|
def generate_id
|
84
84
|
::BSON::ObjectId.new.to_s
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
module ClassMethods
|
88
88
|
def create(attributes={}, &block)
|
89
89
|
if attributes.is_a?(Array)
|
@@ -100,20 +100,20 @@ module MongoModel
|
|
100
100
|
instance.send(:instantiate) if instance
|
101
101
|
instance
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
def collection_name
|
105
105
|
@_collection_name || inferred_collection_name
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
def collection_name=(name)
|
109
109
|
@_collection = nil
|
110
110
|
@_collection_name = name
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
def use_type_selector?
|
114
114
|
!superclass.abstract_class?
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
def type_selector
|
118
118
|
[self.to_s] + descendants.map { |m| m.to_s }
|
119
119
|
end
|
@@ -121,19 +121,19 @@ module MongoModel
|
|
121
121
|
def collection
|
122
122
|
@_collection ||= InstrumentedCollection.new(database.collection(collection_name))
|
123
123
|
end
|
124
|
-
|
124
|
+
|
125
125
|
def database
|
126
126
|
MongoModel.database
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
def save_safely?
|
130
130
|
defined?(@_save_safely) ? @_save_safely : true
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
def save_safely=(val)
|
134
134
|
@_save_safely = val
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
protected
|
138
138
|
def inferred_collection_name
|
139
139
|
if superclass.abstract_class?
|
@@ -143,7 +143,7 @@ module MongoModel
|
|
143
143
|
end
|
144
144
|
end
|
145
145
|
end
|
146
|
-
|
146
|
+
|
147
147
|
private
|
148
148
|
def create_or_update
|
149
149
|
result = new_record? ? create : update
|
@@ -157,7 +157,7 @@ module MongoModel
|
|
157
157
|
def update
|
158
158
|
save_to_collection
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
def save_to_collection
|
162
162
|
collection.save(to_mongo, :w => self.class.save_safely? ? 1 : 0)
|
163
163
|
set_new_record(false)
|