stonegao-mongoid 2.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +50 -0
  3. data/Rakefile +51 -0
  4. data/lib/config/locales/bg.yml +44 -0
  5. data/lib/config/locales/de.yml +44 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +44 -0
  8. data/lib/config/locales/fr.yml +45 -0
  9. data/lib/config/locales/hu.yml +47 -0
  10. data/lib/config/locales/it.yml +42 -0
  11. data/lib/config/locales/kr.yml +68 -0
  12. data/lib/config/locales/nl.yml +42 -0
  13. data/lib/config/locales/pl.yml +42 -0
  14. data/lib/config/locales/pt-br.yml +43 -0
  15. data/lib/config/locales/pt.yml +43 -0
  16. data/lib/config/locales/ro.yml +49 -0
  17. data/lib/config/locales/sv.yml +43 -0
  18. data/lib/config/locales/zh-CN.yml +34 -0
  19. data/lib/mongoid/atomicity.rb +111 -0
  20. data/lib/mongoid/attributes.rb +251 -0
  21. data/lib/mongoid/callbacks.rb +13 -0
  22. data/lib/mongoid/collection.rb +137 -0
  23. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  24. data/lib/mongoid/collections/master.rb +29 -0
  25. data/lib/mongoid/collections/operations.rb +42 -0
  26. data/lib/mongoid/collections/slaves.rb +45 -0
  27. data/lib/mongoid/collections.rb +70 -0
  28. data/lib/mongoid/components.rb +45 -0
  29. data/lib/mongoid/config/database.rb +167 -0
  30. data/lib/mongoid/config/replset_database.rb +48 -0
  31. data/lib/mongoid/config.rb +343 -0
  32. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  33. data/lib/mongoid/contexts/enumerable.rb +226 -0
  34. data/lib/mongoid/contexts/ids.rb +25 -0
  35. data/lib/mongoid/contexts/mongo.rb +345 -0
  36. data/lib/mongoid/contexts/paging.rb +50 -0
  37. data/lib/mongoid/contexts.rb +21 -0
  38. data/lib/mongoid/copyable.rb +44 -0
  39. data/lib/mongoid/criteria.rb +325 -0
  40. data/lib/mongoid/criterion/complex.rb +34 -0
  41. data/lib/mongoid/criterion/creational.rb +34 -0
  42. data/lib/mongoid/criterion/exclusion.rb +67 -0
  43. data/lib/mongoid/criterion/inclusion.rb +134 -0
  44. data/lib/mongoid/criterion/inspection.rb +20 -0
  45. data/lib/mongoid/criterion/optional.rb +213 -0
  46. data/lib/mongoid/criterion/selector.rb +74 -0
  47. data/lib/mongoid/cursor.rb +81 -0
  48. data/lib/mongoid/default_scope.rb +28 -0
  49. data/lib/mongoid/dirty.rb +251 -0
  50. data/lib/mongoid/document.rb +256 -0
  51. data/lib/mongoid/errors/document_not_found.rb +29 -0
  52. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  53. data/lib/mongoid/errors/invalid_database.rb +20 -0
  54. data/lib/mongoid/errors/invalid_field.rb +19 -0
  55. data/lib/mongoid/errors/invalid_options.rb +16 -0
  56. data/lib/mongoid/errors/invalid_type.rb +26 -0
  57. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  58. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  59. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  60. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  61. data/lib/mongoid/errors/validations.rb +24 -0
  62. data/lib/mongoid/errors.rb +12 -0
  63. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  64. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  65. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  66. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  67. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  68. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  69. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  70. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  71. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  72. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  74. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  75. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  76. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  77. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  78. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  79. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  80. data/lib/mongoid/extensions/object_id/conversions.rb +57 -0
  81. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  84. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  85. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  86. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  87. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  88. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  89. data/lib/mongoid/extensions.rb +116 -0
  90. data/lib/mongoid/extras.rb +61 -0
  91. data/lib/mongoid/factory.rb +20 -0
  92. data/lib/mongoid/field.rb +95 -0
  93. data/lib/mongoid/fields.rb +138 -0
  94. data/lib/mongoid/finders.rb +173 -0
  95. data/lib/mongoid/hierarchy.rb +85 -0
  96. data/lib/mongoid/identity.rb +89 -0
  97. data/lib/mongoid/indexes.rb +38 -0
  98. data/lib/mongoid/inspection.rb +58 -0
  99. data/lib/mongoid/javascript/functions.yml +37 -0
  100. data/lib/mongoid/javascript.rb +21 -0
  101. data/lib/mongoid/json.rb +16 -0
  102. data/lib/mongoid/keys.rb +77 -0
  103. data/lib/mongoid/logger.rb +18 -0
  104. data/lib/mongoid/matchers/all.rb +11 -0
  105. data/lib/mongoid/matchers/default.rb +27 -0
  106. data/lib/mongoid/matchers/exists.rb +13 -0
  107. data/lib/mongoid/matchers/gt.rb +11 -0
  108. data/lib/mongoid/matchers/gte.rb +11 -0
  109. data/lib/mongoid/matchers/in.rb +11 -0
  110. data/lib/mongoid/matchers/lt.rb +11 -0
  111. data/lib/mongoid/matchers/lte.rb +11 -0
  112. data/lib/mongoid/matchers/ne.rb +11 -0
  113. data/lib/mongoid/matchers/nin.rb +11 -0
  114. data/lib/mongoid/matchers/size.rb +11 -0
  115. data/lib/mongoid/matchers.rb +55 -0
  116. data/lib/mongoid/modifiers/command.rb +18 -0
  117. data/lib/mongoid/modifiers/inc.rb +24 -0
  118. data/lib/mongoid/modifiers.rb +24 -0
  119. data/lib/mongoid/multi_database.rb +11 -0
  120. data/lib/mongoid/multi_parameter_attributes.rb +80 -0
  121. data/lib/mongoid/named_scope.rb +36 -0
  122. data/lib/mongoid/nested_attributes.rb +43 -0
  123. data/lib/mongoid/paranoia.rb +103 -0
  124. data/lib/mongoid/paths.rb +61 -0
  125. data/lib/mongoid/persistence/command.rb +59 -0
  126. data/lib/mongoid/persistence/insert.rb +53 -0
  127. data/lib/mongoid/persistence/insert_embedded.rb +42 -0
  128. data/lib/mongoid/persistence/remove.rb +44 -0
  129. data/lib/mongoid/persistence/remove_all.rb +40 -0
  130. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  131. data/lib/mongoid/persistence/update.rb +76 -0
  132. data/lib/mongoid/persistence.rb +237 -0
  133. data/lib/mongoid/railtie.rb +129 -0
  134. data/lib/mongoid/railties/database.rake +171 -0
  135. data/lib/mongoid/railties/document.rb +12 -0
  136. data/lib/mongoid/relations/accessors.rb +157 -0
  137. data/lib/mongoid/relations/auto_save.rb +34 -0
  138. data/lib/mongoid/relations/binding.rb +26 -0
  139. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  140. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  141. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  142. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  143. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  144. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
  145. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  146. data/lib/mongoid/relations/bindings.rb +9 -0
  147. data/lib/mongoid/relations/builder.rb +42 -0
  148. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  149. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  150. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  151. data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
  152. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  153. data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
  154. data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
  155. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  156. data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
  157. data/lib/mongoid/relations/builders.rb +79 -0
  158. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  159. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  160. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  161. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  162. data/lib/mongoid/relations/cascading.rb +55 -0
  163. data/lib/mongoid/relations/constraint.rb +45 -0
  164. data/lib/mongoid/relations/cyclic.rb +97 -0
  165. data/lib/mongoid/relations/embedded/in.rb +173 -0
  166. data/lib/mongoid/relations/embedded/many.rb +483 -0
  167. data/lib/mongoid/relations/embedded/one.rb +170 -0
  168. data/lib/mongoid/relations/macros.rb +306 -0
  169. data/lib/mongoid/relations/many.rb +171 -0
  170. data/lib/mongoid/relations/metadata.rb +533 -0
  171. data/lib/mongoid/relations/nested_builder.rb +68 -0
  172. data/lib/mongoid/relations/one.rb +47 -0
  173. data/lib/mongoid/relations/polymorphic.rb +54 -0
  174. data/lib/mongoid/relations/proxy.rb +128 -0
  175. data/lib/mongoid/relations/referenced/in.rb +216 -0
  176. data/lib/mongoid/relations/referenced/many.rb +443 -0
  177. data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
  178. data/lib/mongoid/relations/referenced/one.rb +206 -0
  179. data/lib/mongoid/relations/reflections.rb +45 -0
  180. data/lib/mongoid/relations.rb +105 -0
  181. data/lib/mongoid/safe.rb +23 -0
  182. data/lib/mongoid/safety.rb +207 -0
  183. data/lib/mongoid/scope.rb +31 -0
  184. data/lib/mongoid/serialization.rb +99 -0
  185. data/lib/mongoid/state.rb +66 -0
  186. data/lib/mongoid/timestamps.rb +38 -0
  187. data/lib/mongoid/validations/associated.rb +42 -0
  188. data/lib/mongoid/validations/uniqueness.rb +85 -0
  189. data/lib/mongoid/validations.rb +117 -0
  190. data/lib/mongoid/version.rb +4 -0
  191. data/lib/mongoid/versioning.rb +51 -0
  192. data/lib/mongoid.rb +139 -0
  193. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  194. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
  195. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  196. data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
  197. data/lib/rails/generators/mongoid_generator.rb +61 -0
  198. data/lib/rails/mongoid.rb +57 -0
  199. metadata +380 -0
@@ -0,0 +1,117 @@
1
+ # encoding: utf-8
2
+ require "mongoid/validations/associated"
3
+ require "mongoid/validations/uniqueness"
4
+
5
+ module Mongoid #:nodoc:
6
+
7
+ # This module provides additional validations that ActiveModel does not
8
+ # provide: validates_associated and validates_uniqueness_of.
9
+ module Validations
10
+ extend ActiveSupport::Concern
11
+ include ActiveModel::Validations
12
+
13
+ attr_accessor :validated
14
+
15
+ # Overrides the default ActiveModel behaviour since we need to handle
16
+ # validations of relations slightly different than just calling the
17
+ # getter.
18
+ #
19
+ # @example Read the value.
20
+ # person.read_attribute_for_validation(:addresses)
21
+ #
22
+ # @param [ Symbol ] attr The name of the field or relation.
23
+ #
24
+ # @return [ Object ] The value of the field or the relation.
25
+ #
26
+ # @since 2.0.0.rc.1
27
+ def read_attribute_for_validation(attr)
28
+ if relations[attr.to_s]
29
+ send(attr, false, :eager => true)
30
+ else
31
+ send(attr)
32
+ end
33
+ end
34
+
35
+ # Determine if the document is valid.
36
+ #
37
+ # @example Is the document valid?
38
+ # person.valid?
39
+ #
40
+ # @example Is the document valid in a context?
41
+ # person.valid?(:create)
42
+ #
43
+ # @param [ Symbol ] context The optional validation context.
44
+ #
45
+ # @return [ true, false ] True if valid, false if not.
46
+ #
47
+ # @since 2.0.0.rc.6
48
+ def valid?(context = nil)
49
+ super context ? context : (new? ? :create : :update)
50
+ end
51
+
52
+ # Used to prevent infinite loops in associated validations.
53
+ #
54
+ # @example Is the document validated?
55
+ # document.validated?
56
+ #
57
+ # @return [ true, false ] Has the document already been validated?
58
+ #
59
+ # @since 2.0.0.rc.2
60
+ def validated?
61
+ !!@validated
62
+ end
63
+
64
+ module ClassMethods #:nodoc:
65
+
66
+ # Validates whether or not an association is valid or not. Will correctly
67
+ # handle has one and has many associations.
68
+ #
69
+ # @example
70
+ #
71
+ # class Person
72
+ # include Mongoid::Document
73
+ # embeds_one :name
74
+ # embeds_many :addresses
75
+ #
76
+ # validates_associated :name, :addresses
77
+ # end
78
+ #
79
+ # @param [ Array ] *args The arguments to pass to the validator.
80
+ def validates_associated(*args)
81
+ validates_with(AssociatedValidator, _merge_attributes(args))
82
+ end
83
+
84
+ # Validates whether or not a field is unique against the documents in the
85
+ # database.
86
+ #
87
+ # @example
88
+ #
89
+ # class Person
90
+ # include Mongoid::Document
91
+ # field :title
92
+ #
93
+ # validates_uniqueness_of :title
94
+ # end
95
+ #
96
+ # @param [ Array ] *args The arguments to pass to the validator.
97
+ def validates_uniqueness_of(*args)
98
+ validates_with(UniquenessValidator, _merge_attributes(args))
99
+ end
100
+
101
+ protected
102
+
103
+ # Adds an associated validator for the relation if the validate option
104
+ # was not provided or set to true.
105
+ #
106
+ # @example Set up validation.
107
+ # Person.validate_relation(metadata)
108
+ #
109
+ # @param [ Metadata ] metadata The relation metadata.
110
+ #
111
+ # @since 2.0.0.rc.1
112
+ def validate_relation(metadata)
113
+ validates_associated(metadata.name) if metadata.validate?
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ VERSION = "2.0.0.rc.6"
4
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # Include this module to get automatic versioning of root level documents.
5
+ # This will add a version field to the +Document+ and a has_many association
6
+ # with all the versions contained in it.
7
+ module Versioning
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ field :version, :type => Integer, :default => 1
12
+ embeds_many :versions, :class_name => self.name, :validate => false
13
+ set_callback :save, :before, :revise
14
+
15
+ delegate :version_max, :to => "self.class"
16
+ end
17
+
18
+ # Create a new version of the +Document+. This will load the previous
19
+ # document from the database and set it as the next version before saving
20
+ # the current document. It then increments the version number. If a #max_versions
21
+ # limit is set in the model and it's exceeded, the oldest version gets discarded.
22
+ #
23
+ # @example Revise the document.
24
+ # person.revise
25
+ def revise
26
+ last_version = self.class.first(:conditions => { :_id => id, :version => version })
27
+ if last_version
28
+ versions.target << last_version.clone
29
+ versions.shift if version_max.present? && versions.length > version_max
30
+ self.version = (version || 1 ) + 1
31
+ @modifications["versions"] = [ nil, versions.to_hash ] if @modifications
32
+ end
33
+ end
34
+
35
+ module ClassMethods #:nodoc:
36
+ attr_accessor :version_max
37
+
38
+ # Sets the maximum number of versions to store.
39
+ #
40
+ # @example Set the maximum.
41
+ # Person.max_versions(5)
42
+ #
43
+ # @param [ Integer ] number The maximum number to store.
44
+ #
45
+ # @return [ Integer ] The max number of versions.
46
+ def max_versions(number)
47
+ self.version_max = number.to_i
48
+ end
49
+ end
50
+ end
51
+ end
data/lib/mongoid.rb ADDED
@@ -0,0 +1,139 @@
1
+ # encoding: utf-8
2
+
3
+ # Copyright (c) 2009 - 2011 Durran Jordan and friends.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ require "delegate"
24
+ require "time"
25
+ require "ostruct"
26
+ require "active_support/core_ext"
27
+ require 'active_support/json'
28
+ require "active_support/inflector"
29
+ require "active_support/time_with_zone"
30
+ require "active_model"
31
+ require "active_model/callbacks"
32
+ require "active_model/conversion"
33
+ require "active_model/errors"
34
+ require "active_model/mass_assignment_security"
35
+ require "active_model/naming"
36
+ require "active_model/serialization"
37
+ require "active_model/translation"
38
+ require "active_model/validator"
39
+ require "active_model/validations"
40
+ require "will_paginate/collection"
41
+ require "mongo"
42
+ require "mongoid/errors"
43
+ require "mongoid/extensions"
44
+ require "mongoid/safe"
45
+ require "mongoid/atomicity"
46
+ require "mongoid/attributes"
47
+ require "mongoid/callbacks"
48
+ require "mongoid/collection"
49
+ require "mongoid/collections"
50
+ require "mongoid/config"
51
+ require "mongoid/contexts"
52
+ require "mongoid/copyable"
53
+ require "mongoid/criteria"
54
+ require "mongoid/cursor"
55
+ require "mongoid/default_scope"
56
+ require "mongoid/dirty"
57
+ require "mongoid/extras"
58
+ require "mongoid/factory"
59
+ require "mongoid/field"
60
+ require "mongoid/fields"
61
+ require "mongoid/finders"
62
+ require "mongoid/hierarchy"
63
+ require "mongoid/identity"
64
+ require "mongoid/indexes"
65
+ require "mongoid/inspection"
66
+ require "mongoid/javascript"
67
+ require "mongoid/json"
68
+ require "mongoid/keys"
69
+ require "mongoid/logger"
70
+ require "mongoid/matchers"
71
+ require "mongoid/modifiers"
72
+ require "mongoid/multi_parameter_attributes"
73
+ require "mongoid/multi_database"
74
+ require "mongoid/named_scope"
75
+ require "mongoid/nested_attributes"
76
+ require "mongoid/paths"
77
+ require "mongoid/persistence"
78
+ require "mongoid/relations"
79
+ require "mongoid/safety"
80
+ require "mongoid/scope"
81
+ require "mongoid/serialization"
82
+ require "mongoid/state"
83
+ require "mongoid/timestamps"
84
+ require "mongoid/validations"
85
+ require "mongoid/versioning"
86
+ require "mongoid/components"
87
+ require "mongoid/paranoia"
88
+ require "mongoid/document"
89
+
90
+ # add railtie
91
+ if defined?(Rails)
92
+ require "mongoid/railtie"
93
+ end
94
+
95
+ # add english load path by default
96
+ I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
97
+
98
+ module Mongoid #:nodoc
99
+
100
+ MONGODB_VERSION = "1.6.0"
101
+
102
+ class << self
103
+
104
+ # Sets the Mongoid configuration options. Best used by passing a block.
105
+ #
106
+ # @example Set up configuration options.
107
+ #
108
+ # Mongoid.configure do |config|
109
+ # name = "mongoid_test"
110
+ # host = "localhost"
111
+ # config.allow_dynamic_fields = false
112
+ # config.master = Mongo::Connection.new.db(name)
113
+ # config.slaves = [
114
+ # Mongo::Connection.new(host, 27018, :slave_ok => true).db(name),
115
+ # Mongo::Connection.new(host, 27019, :slave_ok => true).db(name)
116
+ # ]
117
+ # end
118
+ #
119
+ # @return [ Config ] The configuration obejct.
120
+ def configure
121
+ config = Mongoid::Config
122
+ block_given? ? yield(config) : config
123
+ end
124
+ alias :config :configure
125
+ end
126
+
127
+ # Take all the public instance methods from the Config singleton and allow
128
+ # them to be accessed through the Mongoid module directly.
129
+ #
130
+ # @example Delegate the configuration methods.
131
+ # Mongoid.database = Mongo::Connection.new.db("test")
132
+ Mongoid::Config.public_instance_methods(false).each do |name|
133
+ (class << self; self; end).class_eval <<-EOT
134
+ def #{name}(*args)
135
+ configure.send("#{name}", *args)
136
+ end
137
+ EOT
138
+ end
139
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'rails/generators/mongoid_generator'
3
+
4
+ module Mongoid
5
+ module Generators
6
+ class ConfigGenerator < Rails::Generators::Base
7
+ desc "Creates a Mongoid configuration file at config/mongoid.yml"
8
+
9
+ argument :database_name, :type => :string, :optional => true
10
+
11
+ def self.source_root
12
+ @_mongoid_source_root ||= File.expand_path("../templates", __FILE__)
13
+ end
14
+
15
+ def app_name
16
+ Rails::Application.subclasses.first.parent.to_s.underscore
17
+ end
18
+
19
+ def create_config_file
20
+ template 'mongoid.yml', File.join('config', "mongoid.yml")
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ defaults: &defaults
2
+ host: localhost
3
+ # slaves:
4
+ # - host: slave1.local
5
+ # port: 27018
6
+ # - host: slave2.local
7
+ # port: 27019
8
+
9
+ development:
10
+ <<: *defaults
11
+ database: <%= database_name || app_name %>_development
12
+
13
+ test:
14
+ <<: *defaults
15
+ database: <%= database_name || app_name %>_test
16
+
17
+ # set these environment variables on your prod server
18
+ production:
19
+ host: <%%= ENV['MONGOID_HOST'] %>
20
+ port: <%%= ENV['MONGOID_PORT'] %>
21
+ username: <%%= ENV['MONGOID_USERNAME'] %>
22
+ password: <%%= ENV['MONGOID_PASSWORD'] %>
23
+ database: <%%= ENV['MONGOID_DATABASE'] %>
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require "rails/generators/mongoid_generator"
3
+
4
+ module Mongoid #:nodoc:
5
+ module Generators #:nodoc:
6
+ class ModelGenerator < Base #:nodoc:
7
+
8
+ desc "Creates a Mongoid model"
9
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
10
+
11
+ check_class_collision
12
+
13
+ class_option :timestamps, :type => :boolean
14
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
15
+ class_option :versioning, :type => :boolean, :default => false, :desc => "Enable mongoid versioning"
16
+
17
+ def create_model_file
18
+ template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
19
+ end
20
+
21
+ hook_for :test_framework
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent] %>
2
+ <% unless options[:parent] -%>
3
+ include Mongoid::Document
4
+ <% end -%>
5
+ <% if options[:timestamps] -%>
6
+ include Mongoid::Timestamps
7
+ <% end -%>
8
+ <% if options[:versioning] -%>
9
+ include Mongoid::Versioning
10
+ <% end -%>
11
+ <% attributes.reject{|attr| attr.reference?}.each do |attribute| -%>
12
+ field :<%= attribute.name %>, :type => <%= attribute.type_class %>
13
+ <% end -%>
14
+ <% attributes.select{|attr| attr.reference? }.each do |attribute| -%>
15
+ embedded_in :<%= attribute.name%>, :inverse_of => :<%= class_name.tableize %>
16
+ <% end -%>
17
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require "rails/generators/named_base"
3
+ require "rails/generators/active_model"
4
+
5
+ module Mongoid #:nodoc:
6
+ module Generators #:nodoc:
7
+ class Base < ::Rails::Generators::NamedBase #:nodoc:
8
+
9
+ def self.source_root
10
+ @_mongoid_source_root ||=
11
+ File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__)
12
+ end
13
+ end
14
+
15
+ class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
16
+ def self.all(klass)
17
+ "#{klass}.all"
18
+ end
19
+
20
+ def self.find(klass, params=nil)
21
+ "#{klass}.find(#{params})"
22
+ end
23
+
24
+ def self.build(klass, params=nil)
25
+ if params
26
+ "#{klass}.new(#{params})"
27
+ else
28
+ "#{klass}.new"
29
+ end
30
+ end
31
+
32
+ def save
33
+ "#{name}.save"
34
+ end
35
+
36
+ def update_attributes(params=nil)
37
+ "#{name}.update_attributes(#{params})"
38
+ end
39
+
40
+ def errors
41
+ "#{name}.errors"
42
+ end
43
+
44
+ def destroy
45
+ "#{name}.destroy"
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ module Rails
52
+ module Generators
53
+ class GeneratedAttribute #:nodoc:
54
+ def type_class
55
+ return "Time" if type.to_s == "datetime"
56
+ return "String" if type.to_s == "text"
57
+ return type.to_s.camelcase
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ module Rails #:nodoc:
3
+ module Mongoid #:nodoc:
4
+ extend self
5
+
6
+ # Use the application configuration to get every model and require it, so
7
+ # that indexing and inheritance work in both development and production
8
+ # with the same results.
9
+ #
10
+ # @example Load all the application models.
11
+ # Rails::Mongoid.load_models(app)
12
+ #
13
+ # @param [ Application ] app The rails application.
14
+ def load_models(app)
15
+ app.config.paths["app/models"].each do |path|
16
+ Dir.glob("#{path}/**/*.rb").sort.each do |file|
17
+ load_model(file.gsub("#{path}/" , "").gsub(".rb", ""))
18
+ end
19
+ end
20
+ end
21
+
22
+ # Recursive function to create all the indexes for the model, then
23
+ # potentially and subclass of the model since both are still root
24
+ # documents in the hierarchy.
25
+ #
26
+ # Note there is a tricky naming scheme going on here that needs to be
27
+ # revisisted. Module.descendants vs Class.descendents is way too
28
+ # confusing.
29
+ #
30
+ # @example Index the children.
31
+ # Rails::Mongoid.index_children(classes)
32
+ #
33
+ # @param [ Array<Class> ] children The child model classes.
34
+ def index_children(children)
35
+ children.each do |model|
36
+ Logger.new($stdout).info("Generating indexes for #{model}")
37
+ model.create_indexes
38
+ index_children(model.descendants)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ # I don't want to mock out kernel for unit testing purposes, so added this
45
+ # method as a convenience.
46
+ #
47
+ # @example Load the model.
48
+ # Mongoid.load_model("/mongoid/behaviour")
49
+ #
50
+ # @param [ String ] file The base filename.
51
+ #
52
+ # @since 2.0.0.rc.3
53
+ def load_model(file)
54
+ require_dependency(file)
55
+ end
56
+ end
57
+ end