mongomodel 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module MongoModel
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
include ActiveModel::AttributeMethods
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
module ClassMethods
|
|
8
8
|
# Generates all the attribute related methods for defined properties
|
|
9
9
|
# accessors, mutators and query methods.
|
|
@@ -13,22 +13,22 @@ module MongoModel
|
|
|
13
13
|
super(properties.keys)
|
|
14
14
|
@attribute_methods_generated = true
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
def attribute_methods_generated?
|
|
18
18
|
@attribute_methods_generated ||= false
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
def undefine_attribute_methods(*args)
|
|
22
22
|
super
|
|
23
23
|
@attribute_methods_generated = false
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
def property(*args)
|
|
27
27
|
property = super
|
|
28
28
|
undefine_attribute_methods
|
|
29
29
|
property
|
|
30
30
|
end
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
protected
|
|
33
33
|
def instance_method_already_implemented?(method_name)
|
|
34
34
|
method_defined?(method_name) ||
|
|
@@ -36,7 +36,7 @@ module MongoModel
|
|
|
36
36
|
super
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
def method_missing(method, *args, &block)
|
|
41
41
|
unless self.class.attribute_methods_generated?
|
|
42
42
|
self.class.define_attribute_methods
|
|
@@ -50,19 +50,19 @@ module MongoModel
|
|
|
50
50
|
super
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
def respond_to?(*args)
|
|
55
55
|
self.class.define_attribute_methods unless self.class.attribute_methods_generated?
|
|
56
56
|
super
|
|
57
57
|
end
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
def clone_attribute_value(attribute_name)
|
|
60
60
|
value = read_attribute(attribute_name)
|
|
61
61
|
value.duplicable? ? value.clone : value
|
|
62
62
|
rescue TypeError, NoMethodError
|
|
63
63
|
value
|
|
64
64
|
end
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
protected
|
|
67
67
|
def attribute_method?(attr_name)
|
|
68
68
|
properties.has_key?(attr_name)
|
|
@@ -2,16 +2,16 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module BeforeTypeCast
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
included do
|
|
7
7
|
attribute_method_suffix "_before_type_cast"
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
# Returns an attribute value before typecasting.
|
|
11
11
|
def read_attribute_before_type_cast(name)
|
|
12
12
|
attributes.before_type_cast(name.to_sym)
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
# Returns a hash of attributes before typecasting.
|
|
16
16
|
def attributes_before_type_cast
|
|
17
17
|
attributes.keys.inject({}) do |result, key|
|
|
@@ -19,7 +19,7 @@ module MongoModel
|
|
|
19
19
|
result
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
private
|
|
24
24
|
def attribute_before_type_cast(attribute_name)
|
|
25
25
|
read_attribute_before_type_cast(attribute_name)
|
|
@@ -2,17 +2,62 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Dirty
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
6
|
-
include ActiveModel::
|
|
7
|
-
|
|
5
|
+
|
|
6
|
+
include ActiveModel::AttributeMethods
|
|
7
|
+
|
|
8
|
+
OPTION_NOT_GIVEN = Object.new
|
|
9
|
+
private_constant :OPTION_NOT_GIVEN
|
|
10
|
+
|
|
8
11
|
included do
|
|
12
|
+
attribute_method_suffix "_changed?", "_change", "_will_change!", "_was"
|
|
13
|
+
attribute_method_suffix "_previously_changed?", "_previous_change"
|
|
14
|
+
attribute_method_affix prefix: "restore_", suffix: "!"
|
|
15
|
+
|
|
9
16
|
before_save { @previously_changed = changes }
|
|
10
17
|
after_save { changed_attributes.clear }
|
|
11
18
|
end
|
|
12
|
-
|
|
19
|
+
|
|
20
|
+
def changed?
|
|
21
|
+
changed_attributes.present?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def changed
|
|
25
|
+
changed_attributes.keys
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def changes
|
|
29
|
+
ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def previous_changes
|
|
33
|
+
@previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def changed_attributes
|
|
37
|
+
@changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
|
|
41
|
+
!!changes_include?(attr) &&
|
|
42
|
+
(to == OPTION_NOT_GIVEN || to == __send__(attr)) &&
|
|
43
|
+
(from == OPTION_NOT_GIVEN || from == changed_attributes[attr])
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def attribute_was(attr)
|
|
47
|
+
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def attribute_previously_changed?(attr)
|
|
51
|
+
previous_changes.include?(attr)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def restore_attributes(attributes = changed)
|
|
55
|
+
attributes.each { |attr| restore_attribute! attr }
|
|
56
|
+
end
|
|
57
|
+
|
|
13
58
|
def write_attribute(key, value)
|
|
14
59
|
attr = key.to_sym
|
|
15
|
-
|
|
60
|
+
|
|
16
61
|
# The attribute already has an unsaved change.
|
|
17
62
|
if changed_attributes.include?(attr)
|
|
18
63
|
old = changed_attributes[attr]
|
|
@@ -21,21 +66,58 @@ module MongoModel
|
|
|
21
66
|
old = clone_attribute_value(attr)
|
|
22
67
|
changed_attributes[attr] = old unless value == old
|
|
23
68
|
end
|
|
24
|
-
|
|
69
|
+
|
|
25
70
|
super
|
|
26
71
|
end
|
|
27
|
-
|
|
72
|
+
|
|
28
73
|
# Returns the attributes as they were before any changes were made to the document.
|
|
29
74
|
def original_attributes
|
|
30
75
|
{}.with_indifferent_access.merge(attributes).merge(changed_attributes)
|
|
31
76
|
end
|
|
32
|
-
|
|
33
|
-
protected
|
|
34
|
-
def changed_attributes
|
|
35
|
-
@changed_attributes ||= {}.with_indifferent_access
|
|
36
|
-
end
|
|
37
|
-
|
|
77
|
+
|
|
38
78
|
private
|
|
79
|
+
def changes_include?(attr_name)
|
|
80
|
+
attributes_changed_by_setter.include?(attr_name)
|
|
81
|
+
end
|
|
82
|
+
alias attribute_changed_by_setter? changes_include?
|
|
83
|
+
|
|
84
|
+
def attribute_change(attr)
|
|
85
|
+
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def attribute_previous_change(attr)
|
|
89
|
+
previous_changes[attr] if attribute_previously_changed?(attr)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def attribute_will_change!(attr)
|
|
93
|
+
return if attribute_changed?(attr)
|
|
94
|
+
|
|
95
|
+
begin
|
|
96
|
+
value = __send__(attr)
|
|
97
|
+
value = value.duplicable? ? value.clone : value
|
|
98
|
+
rescue TypeError, NoMethodError
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
set_attribute_was(attr, value)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def restore_attribute!(attr)
|
|
105
|
+
if attribute_changed?(attr)
|
|
106
|
+
__send__("#{attr}=", changed_attributes[attr])
|
|
107
|
+
clear_attribute_changes([attr])
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
alias_method :attributes_changed_by_setter, :changed_attributes
|
|
112
|
+
|
|
113
|
+
def set_attribute_was(attr, old_value)
|
|
114
|
+
attributes_changed_by_setter[attr] = old_value
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def clear_attribute_changes(attributes)
|
|
118
|
+
attributes_changed_by_setter.except!(*attributes)
|
|
119
|
+
end
|
|
120
|
+
|
|
39
121
|
def clone_attribute_value(attribute_name)
|
|
40
122
|
value = self[attribute_name.to_sym]
|
|
41
123
|
value.duplicable? ? value.clone : value
|
|
@@ -2,11 +2,11 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module MultiParameterAssignment
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
def assign_attributes(attrs, options={})#:nodoc:
|
|
7
7
|
super(transform_multiparameter_attributes(attrs))
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
private
|
|
11
11
|
# Converts multiparameter attributes into array format. For example, the parameters
|
|
12
12
|
# { "start_date(1i)" => "2010", "start_date(2i)" => "9", "start_date(3i)" => "4" }
|
|
@@ -15,20 +15,20 @@ module MongoModel
|
|
|
15
15
|
def transform_multiparameter_attributes(attrs)
|
|
16
16
|
attrs.merge(extract_multiparameter_attributes(attrs))
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def extract_multiparameter_attributes(attrs)
|
|
20
20
|
multiparameter_attributes = Hash.new { |h, k| h[k] = [] }
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
attrs.each do |k, v|
|
|
23
23
|
if k.to_s =~ /(.*)\((\d+)([if])?\)/
|
|
24
24
|
multiparameter_attributes[$1][$2.to_i - 1] = type_cast_attribute_value($3, v)
|
|
25
25
|
attrs.delete(k)
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
multiparameter_attributes
|
|
30
30
|
end
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
def type_cast_attribute_value(type, value)
|
|
33
33
|
case type
|
|
34
34
|
when 'i', 'f'
|
|
@@ -2,35 +2,35 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Nested
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
included do
|
|
7
7
|
class_attribute :nested_attributes_options, :instance_writer => false
|
|
8
8
|
self.nested_attributes_options = {}
|
|
9
9
|
end
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
module ClassMethods
|
|
12
12
|
def accepts_nested_attributes_for(*attr_names)
|
|
13
13
|
options = attr_names.extract_options!
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
attr_names.each do |attr_name|
|
|
16
16
|
type = property_type(attr_name)
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
nested_attributes_options = self.nested_attributes_options.dup
|
|
19
19
|
nested_attributes_options[attr_name.to_sym] = options
|
|
20
20
|
self.nested_attributes_options = nested_attributes_options
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
class_eval <<-EORUBY, __FILE__, __LINE__ + 1
|
|
23
23
|
if method_defined?(:#{attr_name}_attributes=)
|
|
24
24
|
remove_method(:#{attr_name}_attributes=)
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
def #{attr_name}_attributes=(attributes)
|
|
28
28
|
assign_nested_attributes_for_#{type}(:#{attr_name}, attributes)
|
|
29
29
|
end
|
|
30
30
|
EORUBY
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
private
|
|
35
35
|
def property_type(attr_name)
|
|
36
36
|
if property = properties[attr_name]
|
|
@@ -40,7 +40,7 @@ module MongoModel
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
|
-
|
|
43
|
+
|
|
44
44
|
private
|
|
45
45
|
def assign_nested_attributes_for_property(property, attributes)
|
|
46
46
|
if obj = send(property)
|
|
@@ -49,18 +49,18 @@ module MongoModel
|
|
|
49
49
|
send("#{property}=", attributes)
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
def assign_nested_attributes_for_collection(property, attributes_collection)
|
|
54
54
|
attributes_collection = convert_to_array(attributes_collection)
|
|
55
55
|
options = self.nested_attributes_options[property]
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
if options[:limit] && attributes_collection.size > options[:limit]
|
|
58
58
|
raise TooManyDocuments, "Maximum #{options[:limit]} documents are allowed. Got #{attributes_collection.size} documents instead."
|
|
59
59
|
end
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
collection = send(property)
|
|
62
62
|
collection.clear if options[:clear]
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
attributes_collection.each_with_index do |attributes, index|
|
|
65
65
|
if collection[index]
|
|
66
66
|
collection[index].attributes = attributes
|
|
@@ -69,7 +69,7 @@ module MongoModel
|
|
|
69
69
|
end
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
def assign_nested_attributes_for_association(association, attributes)
|
|
74
74
|
if obj = send(association)
|
|
75
75
|
obj.attributes = attributes
|
|
@@ -77,23 +77,23 @@ module MongoModel
|
|
|
77
77
|
send("build_#{association}", attributes)
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
def assign_nested_attributes_for_association_collection(association, attributes_collection)
|
|
82
82
|
attributes_collection = convert_to_array(attributes_collection)
|
|
83
83
|
options = self.nested_attributes_options[association]
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
if options[:limit] && attributes_collection.size > options[:limit]
|
|
86
86
|
raise TooManyDocuments, "Maximum #{options[:limit]} documents are allowed. Got #{attributes_collection.size} documents instead."
|
|
87
87
|
end
|
|
88
|
-
|
|
88
|
+
|
|
89
89
|
association = send(association)
|
|
90
90
|
association.clear if options[:clear]
|
|
91
|
-
|
|
91
|
+
|
|
92
92
|
attributes_collection.each do |attributes|
|
|
93
93
|
association.build(attributes)
|
|
94
94
|
end
|
|
95
95
|
end
|
|
96
|
-
|
|
96
|
+
|
|
97
97
|
def convert_to_array(params)
|
|
98
98
|
case params
|
|
99
99
|
when Hash
|
|
@@ -2,7 +2,7 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Protected
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
include ActiveModel::MassAssignmentSecurity
|
|
7
7
|
|
|
8
8
|
module ClassMethods
|
|
@@ -11,11 +11,11 @@ module MongoModel
|
|
|
11
11
|
|
|
12
12
|
attr_protected(name) if property.options[:protected]
|
|
13
13
|
attr_accessible(name) if property.options[:accessible]
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
property
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def assign_attributes(attrs, options={})
|
|
20
20
|
if options[:without_protection]
|
|
21
21
|
super
|
|
@@ -2,16 +2,16 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Query
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
included do
|
|
7
7
|
attribute_method_suffix "?"
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
# Returns true if the attribute is not blank (i.e. it has some value). Otherwise returns false.
|
|
11
11
|
def query_attribute(name)
|
|
12
12
|
attributes.has?(name.to_sym)
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
private
|
|
16
16
|
# Handle *? for method_missing.
|
|
17
17
|
def attribute?(attribute_name)
|
|
@@ -2,27 +2,27 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Read
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
# ActiveModel 3.2 defines the non-prefixed attribute name automatically
|
|
7
7
|
if ActiveModel::VERSION::STRING <= "3.2"
|
|
8
8
|
included do
|
|
9
9
|
attribute_method_suffix ""
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
# Returns the value of the attribute identified by +name+ after it has been typecast (for example,
|
|
14
14
|
# "2004-12-12" in a date property is cast to a date object, like Date.new(2004, 12, 12)).
|
|
15
15
|
def read_attribute(name)
|
|
16
16
|
attributes[name.to_sym]
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
# Returns the value of the attribute identified by <tt>name</tt> after it has been typecast (for example,
|
|
20
20
|
# "2004-12-12" in a date property is cast to a date object, like Date.new(2004, 12, 12)).
|
|
21
21
|
# (Alias for read_attribute).
|
|
22
22
|
def [](name)
|
|
23
23
|
read_attribute(name)
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
private
|
|
27
27
|
def attribute(attribute_name)
|
|
28
28
|
read_attribute(attribute_name)
|