Permalink
Browse files

Merge branch 'nunesmaster'

* nunesmaster: (40 commits)
  Allow MongoMapper.setup to accept a symbol for the environment name so Sinatra's settings.environment value can be used.
  another fix in inspect
  fix issue #373
  Fix and test for displaying embedded documents
  Release 0.11.0
  Added timestamps! to EmbeddedDocument
  Document change to update_attribute
  Fix accessible/protected tests after change to update_attribute
  ActiveSupport::Concern's automatic inclusion of the InstanceMethods module is deprecated
  Fix add_to_set/push_uniq test typos
  Fuzzy match development dependency requirements on the minor version
  rename `connection_options` to `options` and create a test
  use mongodb connection options from yaml config file
  rename test
  move reflect_on_association to Rails module
  Make update_attribute behave like in ActiveRecord
  ActiveRecord compatible association reflection
  added :context option to validates_associated
  Added namespacing to model generator
  Add OneEmbeddedPolymorphicProxy (the last missing polymorphic proxy)
  ...
  • Loading branch information...
2 parents 5d2a5e8 + c2614d7 commit 321a552b72093e8a8dd2b8d96d3337f939b86fa4 @bryckbost bryckbost committed Feb 25, 2012
Showing with 1,352 additions and 623 deletions.
  1. +27 −0 ChangeLog
  2. +9 −8 Gemfile
  3. +4 −1 UPGRADES
  4. +2 −5 examples/plugins.rb
  5. +2 −0 lib/mongo_mapper.rb
  6. +5 −1 lib/mongo_mapper/connection.rb
  7. +1 −0 lib/mongo_mapper/embedded_document.rb
  8. +5 −6 lib/mongo_mapper/extensions/object.rb
  9. +14 −16 lib/mongo_mapper/plugins/accessible.rb
  10. +21 −23 lib/mongo_mapper/plugins/associations.rb
  11. +1 −1 lib/mongo_mapper/plugins/associations/base.rb
  12. +5 −36 lib/mongo_mapper/plugins/associations/belongs_to_association.rb
  13. +2 −2 lib/mongo_mapper/plugins/associations/in_array_proxy.rb
  14. +0 −4 lib/mongo_mapper/plugins/associations/many_association.rb
  15. +1 −1 lib/mongo_mapper/plugins/associations/many_documents_proxy.rb
  16. +5 −5 lib/mongo_mapper/plugins/associations/one_association.rb
  17. +30 −0 lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb
  18. +45 −0 lib/mongo_mapper/plugins/associations/single_association.rb
  19. +11 −13 lib/mongo_mapper/plugins/caching.rb
  20. +12 −14 lib/mongo_mapper/plugins/callbacks.rb
  21. +11 −13 lib/mongo_mapper/plugins/clone.rb
  22. +36 −38 lib/mongo_mapper/plugins/dirty.rb
  23. +20 −22 lib/mongo_mapper/plugins/document.rb
  24. +11 −13 lib/mongo_mapper/plugins/embedded_callbacks.rb
  25. +22 −24 lib/mongo_mapper/plugins/embedded_document.rb
  26. +6 −8 lib/mongo_mapper/plugins/equality.rb
  27. +13 −15 lib/mongo_mapper/plugins/identity_map.rb
  28. +2 −3 lib/mongo_mapper/plugins/indexes.rb
  29. +6 −8 lib/mongo_mapper/plugins/inspect.rb
  30. +113 −110 lib/mongo_mapper/plugins/keys.rb
  31. +2 −4 lib/mongo_mapper/plugins/logger.rb
  32. +32 −34 lib/mongo_mapper/plugins/modifiers.rb
  33. +5 −7 lib/mongo_mapper/plugins/persistence.rb
  34. +14 −16 lib/mongo_mapper/plugins/protected.rb
  35. +31 −33 lib/mongo_mapper/plugins/querying.rb
  36. +28 −22 lib/mongo_mapper/plugins/rails.rb
  37. +33 −0 lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb
  38. +3 −5 lib/mongo_mapper/plugins/safe.rb
  39. +3 −5 lib/mongo_mapper/plugins/sci.rb
  40. +49 −52 lib/mongo_mapper/plugins/serialization.rb
  41. +4 −6 lib/mongo_mapper/plugins/timestamps.rb
  42. +8 −10 lib/mongo_mapper/plugins/validations.rb
  43. +1 −2 lib/mongo_mapper/railtie.rb
  44. +14 −9 lib/mongo_mapper/railtie/database.rake
  45. +1 −1 lib/mongo_mapper/version.rb
  46. +1 −1 lib/rails/generators/mongo_mapper/model/model_generator.rb
  47. +2 −0 lib/rails/generators/mongo_mapper/model/templates/model.rb
  48. +1 −1 mongo_mapper.gemspec
  49. +63 −0 test/functional/associations/test_many_embedded_polymorphic_proxy.rb
  50. +6 −0 test/functional/associations/test_one_as_proxy.rb
  51. +208 −0 test/functional/associations/test_one_embedded_polymorphic_proxy.rb
  52. +30 −0 test/functional/test_accessible.rb
  53. +12 −0 test/functional/test_embedded_document.rb
  54. +9 −1 test/functional/test_indexes.rb
  55. +19 −19 test/functional/test_modifiers.rb
  56. +26 −0 test/functional/test_protected.rb
  57. +15 −1 test/functional/test_querying.rb
  58. +33 −0 test/functional/test_validations.rb
  59. +14 −0 test/models.rb
  60. +4 −0 test/support/railtie.rb
  61. +2 −0 test/support/railtie/autoloaded.rb
  62. +3 −0 test/support/railtie/not_autoloaded.rb
  63. +3 −0 test/support/railtie/parent.rb
  64. +11 −0 test/unit/associations/test_one_association.rb
  65. +20 −0 test/unit/test_inspect.rb
  66. +19 −0 test/unit/test_mongo_mapper.rb
  67. +2 −4 test/unit/test_plugins.rb
  68. +118 −0 test/unit/test_rails_reflect_on_association.rb
  69. +61 −0 test/unit/test_railtie.rb
View
@@ -0,0 +1,27 @@
+0.11.0 - 2012-01-26
+
+ Enhancements:
+
+ * Adds support for has_one polymorphic embedded associations
+ * Adds namespacing to model generator
+ * Adds :context option to validates_associated
+
+ many :things
+ validates_associated :things, :context => :custom_context
+
+ * Adds ActiveRecord-compatible association reflection
+ * Adds support for setting mongo connection options in mongo.yml
+
+ production:
+ uri: <%= ENV['MONGOHQ_URL'] %>
+ options:
+ safe: true
+
+ * Adds #timestamps! to embedded documents
+
+ Bug Fixes
+
+ * #update_attribute now ignores attr_accessible and attr_protected
+ * Fix deprecation warnings in Rails 3.2
+
+ https://github.com/jnunemaker/mongomapper/compare/v0.10.1...v0.11.0
View
17 Gemfile
@@ -2,20 +2,21 @@ source :rubygems
gemspec
group(:development) do
- gem 'bson_ext', '~> 1.3.0'
+ gem 'bson_ext', '~> 1.5'
gem 'SystemTimer', :platform => :mri_18
gem 'ruby-debug', :platform => :mri_18
gem 'ruby-debug19', :platform => :mri_19, :require => 'ruby-debug'
gem 'perftools.rb', :platform => :mri, :require => 'perftools'
gem 'rake'
- gem 'tzinfo'
- gem 'json'
- gem 'log_buddy'
- gem 'jnunemaker-matchy', '~> 0.4.0', :require => 'matchy'
+ gem 'tzinfo', '~> 0.3'
+ gem 'json', '~> 1.6'
+ gem 'log_buddy', '~> 0.6'
+ gem 'jnunemaker-matchy', '~> 0.4', :require => 'matchy'
gem 'shoulda', '~> 2.11'
- gem 'timecop', '~> 0.3.1'
- gem 'mocha', '~> 0.9.8'
- gem 'rack-test'
+ gem 'timecop', '~> 0.3'
+ gem 'mocha', '~> 0.10'
+ gem 'rack-test', '~> 0.6'
+ gem 'rails', '~> 3.0'
end
View
@@ -1,4 +1,7 @@
-0.9 => 1.0
+0.10 => 0.11
+ * #update_attribute now ignores attr_accessible and attr_protected
+
+0.9 => 0.10
* Using String IDs are no longer supported. If you are declaring your own ID, ensure it is an ObjectId, and set the default
key :_id, ObjectId, :default => lambda { BSON::ObjectId.new }
* The :dependent association option now applies to both when the parent is destroyed and when the association is reassigned (one and many associations)
View
@@ -16,11 +16,8 @@ def foo
end
end
- # InstanceMethods module will automatically get included
- module InstanceMethods
- def foo
- 'Foo instance method!'
- end
+ def foo
+ 'Foo instance method!'
end
# Any configuration can be done in the #included block, which gets
View
@@ -62,6 +62,7 @@ module Associations
autoload :Collection, 'mongo_mapper/plugins/associations/collection'
autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
+ autoload :SingleAssociation, 'mongo_mapper/plugins/associations/single_association'
autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
@@ -74,6 +75,7 @@ module Associations
autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
autoload :OneAsProxy, 'mongo_mapper/plugins/associations/one_as_proxy'
autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/one_embedded_proxy'
+ autoload :OneEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy'
autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
end
end
@@ -44,7 +44,7 @@ def config
# @api private
def config_for_environment(environment)
- env = config[environment] || {}
+ env = config[environment.to_s] || {}
return env if env['uri'].blank?
uri = URI.parse(env['uri'])
@@ -62,6 +62,10 @@ def connect(environment, options={})
raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
env = config_for_environment(environment)
+ if env['options'].is_a? Hash
+ options = env['options'].symbolize_keys.merge(options)
+ end
+
MongoMapper.connection = if env['hosts']
Mongo::ReplSetConnection.new( *env['hosts'].push(options) )
else
@@ -19,6 +19,7 @@ module EmbeddedDocument
include Plugins::Rails
include Plugins::Sci
include Plugins::Serialization
+ include Plugins::Timestamps
include Plugins::Validations
include Plugins::EmbeddedCallbacks
@@ -2,6 +2,8 @@
module MongoMapper
module Extensions
module Object
+ extend ActiveSupport::Concern
+
module ClassMethods
def to_mongo(value)
value
@@ -12,16 +14,13 @@ def from_mongo(value)
end
end
- module InstanceMethods
- def to_mongo
- self.class.to_mongo(self)
- end
+ def to_mongo
+ self.class.to_mongo(self)
end
end
end
end
class Object
- extend MongoMapper::Extensions::Object::ClassMethods
- include MongoMapper::Extensions::Object::InstanceMethods
+ include MongoMapper::Extensions::Object
end
@@ -14,25 +14,23 @@ def attr_accessible(*attrs)
end
end
- module InstanceMethods
- def assign(attrs={})
- super(filter_inaccessible_attrs(attrs))
- end
-
- def update_attributes(attrs={})
- super(filter_inaccessible_attrs(attrs))
- end
+ def attributes=(attrs={})
+ super(filter_inaccessible_attrs(attrs))
+ end
- def update_attributes!(attrs={})
- super(filter_inaccessible_attrs(attrs))
- end
+ def update_attributes(attrs={})
+ super(filter_inaccessible_attrs(attrs))
+ end
- protected
- def filter_inaccessible_attrs(attrs)
- return attrs if accessible_attributes.blank? || attrs.blank?
- attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
- end
+ def update_attributes!(attrs={})
+ super(filter_inaccessible_attrs(attrs))
end
+
+ protected
+ def filter_inaccessible_attrs(attrs)
+ return attrs if !accessible_attributes? || attrs.blank?
+ attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
+ end
end
end
end
@@ -56,35 +56,33 @@ def create_association(association)
end
end
- module InstanceMethods
- def associations
- self.class.associations
- end
+ def associations
+ self.class.associations
+ end
- def embedded_associations
- associations.values.select { |assoc| assoc.embeddable? }
- end
+ def embedded_associations
+ associations.values.select { |assoc| assoc.embeddable? }
+ end
- def build_proxy(association)
- proxy = association.proxy_class.new(self, association)
- self.instance_variable_set(association.ivar, proxy)
+ def build_proxy(association)
+ proxy = association.proxy_class.new(self, association)
+ self.instance_variable_set(association.ivar, proxy)
- proxy
- end
+ proxy
+ end
- def get_proxy(association)
- unless proxy = self.instance_variable_get(association.ivar)
- proxy = build_proxy(association)
- end
- proxy
+ def get_proxy(association)
+ unless proxy = self.instance_variable_get(association.ivar)
+ proxy = build_proxy(association)
end
+ proxy
+ end
- def save_to_collection(options={})
- super if defined?(super)
- associations.each do |association_name, association|
- proxy = get_proxy(association)
- proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection) && association.autosave?
- end
+ def save_to_collection(options={})
+ super if defined?(super)
+ associations.each do |association_name, association|
+ proxy = get_proxy(association)
+ proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection) && association.autosave?
end
end
end
@@ -40,7 +40,7 @@ def embeddable?
end
def type_key_name
- "#{as}_type"
+ "_type"
end
def as
@@ -2,7 +2,10 @@
module MongoMapper
module Plugins
module Associations
- class BelongsToAssociation < Base
+ class BelongsToAssociation < SingleAssociation
+ def type_key_name
+ "#{as}_type"
+ end
def embeddable?
false
@@ -14,41 +17,7 @@ def proxy_class
def setup(model)
model.key foreign_key, ObjectId unless model.key?(foreign_key)
-
- model.associations_module.module_eval <<-end_eval
- def #{name}
- proxy = get_proxy(associations[#{name.inspect}])
- proxy.nil? ? nil : proxy
- end
-
- def #{name}=(value)
- association = associations[#{name.inspect}]
- proxy = get_proxy(association)
-
- if proxy.nil? || proxy.target != value
- proxy = build_proxy(association)
- end
-
- proxy.replace(value)
- value
- end
-
- def #{name}?
- get_proxy(associations[#{name.inspect}]).present?
- end
-
- def build_#{name}(attrs={})
- get_proxy(associations[#{name.inspect}]).build(attrs)
- end
-
- def create_#{name}(attrs={})
- get_proxy(associations[#{name.inspect}]).create(attrs)
- end
-
- def create_#{name}!(attrs={})
- get_proxy(associations[#{name.inspect}]).create!(attrs)
- end
- end_eval
+ super
end
def autosave?
@@ -103,8 +103,8 @@ def replace(docs)
def query(options={})
klass.
query(association.query_options).
- update(options).
- update(criteria)
+ amend(options).
+ amend(criteria)
end
def criteria
@@ -8,10 +8,6 @@ def class_name
@class_name ||= options[:class_name] || name.to_s.singularize.camelize
end
- def type_key_name
- "_type"
- end
-
# hate this, need to revisit
def proxy_class
@proxy_class ||= if klass.embeddable?
@@ -74,7 +74,7 @@ def save_to_collection(options={})
def query(options={})
klass.
query(association.query_options).
- update(options).update(criteria)
+ amend(options).amend(criteria)
end
def method_missing(method, *args, &block)
@@ -2,32 +2,32 @@
module MongoMapper
module Plugins
module Associations
- class OneAssociation < BelongsToAssociation
+ class OneAssociation < SingleAssociation
def embeddable?
klass.embeddable?
end
def proxy_class
@proxy_class ||=
if klass.embeddable?
- OneEmbeddedProxy
+ polymorphic? ? OneEmbeddedPolymorphicProxy : OneEmbeddedProxy
elsif as?
OneAsProxy
else
OneProxy
end
end
-
+
def setup(model)
super
-
+
association = self
options = self.options
model.before_destroy do
if !association.embeddable?
proxy = self.get_proxy(association)
-
+
unless proxy.nil?
case options[:dependent]
when :destroy then proxy.destroy
Oops, something went wrong.

0 comments on commit 321a552

Please sign in to comment.