mongo_mapper 0.10.0 → 0.10.1

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.
data/lib/mongo_mapper.rb CHANGED
@@ -62,6 +62,7 @@ module MongoMapper
62
62
  autoload :Collection, 'mongo_mapper/plugins/associations/collection'
63
63
  autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
64
64
  autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
65
+ autoload :SingleAssociation, 'mongo_mapper/plugins/associations/single_association'
65
66
  autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
66
67
  autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
67
68
  autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
@@ -15,7 +15,7 @@ module MongoMapper
15
15
  end
16
16
 
17
17
  module InstanceMethods
18
- def assign(attrs={})
18
+ def attributes=(attrs={})
19
19
  super(filter_inaccessible_attrs(attrs))
20
20
  end
21
21
 
@@ -29,7 +29,7 @@ module MongoMapper
29
29
 
30
30
  protected
31
31
  def filter_inaccessible_attrs(attrs)
32
- return attrs if accessible_attributes.blank? || attrs.blank?
32
+ return attrs if !accessible_attributes? || attrs.blank?
33
33
  attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
34
34
  end
35
35
  end
@@ -2,8 +2,7 @@
2
2
  module MongoMapper
3
3
  module Plugins
4
4
  module Associations
5
- class BelongsToAssociation < Base
6
-
5
+ class BelongsToAssociation < SingleAssociation
7
6
  def embeddable?
8
7
  false
9
8
  end
@@ -14,41 +13,7 @@ module MongoMapper
14
13
 
15
14
  def setup(model)
16
15
  model.key foreign_key, ObjectId unless model.key?(foreign_key)
17
-
18
- model.associations_module.module_eval <<-end_eval
19
- def #{name}
20
- proxy = get_proxy(associations[#{name.inspect}])
21
- proxy.nil? ? nil : proxy
22
- end
23
-
24
- def #{name}=(value)
25
- association = associations[#{name.inspect}]
26
- proxy = get_proxy(association)
27
-
28
- if proxy.nil? || proxy.target != value
29
- proxy = build_proxy(association)
30
- end
31
-
32
- proxy.replace(value)
33
- value
34
- end
35
-
36
- def #{name}?
37
- get_proxy(associations[#{name.inspect}]).present?
38
- end
39
-
40
- def build_#{name}(attrs={})
41
- get_proxy(associations[#{name.inspect}]).build(attrs)
42
- end
43
-
44
- def create_#{name}(attrs={})
45
- get_proxy(associations[#{name.inspect}]).create(attrs)
46
- end
47
-
48
- def create_#{name}!(attrs={})
49
- get_proxy(associations[#{name.inspect}]).create!(attrs)
50
- end
51
- end_eval
16
+ super
52
17
  end
53
18
 
54
19
  def autosave?
@@ -103,8 +103,8 @@ module MongoMapper
103
103
  def query(options={})
104
104
  klass.
105
105
  query(association.query_options).
106
- update(options).
107
- update(criteria)
106
+ amend(options).
107
+ amend(criteria)
108
108
  end
109
109
 
110
110
  def criteria
@@ -74,7 +74,7 @@ module MongoMapper
74
74
  def query(options={})
75
75
  klass.
76
76
  query(association.query_options).
77
- update(options).update(criteria)
77
+ amend(options).amend(criteria)
78
78
  end
79
79
 
80
80
  def method_missing(method, *args, &block)
@@ -2,7 +2,7 @@
2
2
  module MongoMapper
3
3
  module Plugins
4
4
  module Associations
5
- class OneAssociation < BelongsToAssociation
5
+ class OneAssociation < SingleAssociation
6
6
  def embeddable?
7
7
  klass.embeddable?
8
8
  end
@@ -17,17 +17,17 @@ module MongoMapper
17
17
  OneProxy
18
18
  end
19
19
  end
20
-
20
+
21
21
  def setup(model)
22
22
  super
23
-
23
+
24
24
  association = self
25
25
  options = self.options
26
26
 
27
27
  model.before_destroy do
28
28
  if !association.embeddable?
29
29
  proxy = self.get_proxy(association)
30
-
30
+
31
31
  unless proxy.nil?
32
32
  case options[:dependent]
33
33
  when :destroy then proxy.destroy
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class SingleAssociation < Base
6
+ def setup(model)
7
+ model.associations_module.module_eval <<-end_eval
8
+ def #{name}
9
+ proxy = get_proxy(associations[#{name.inspect}])
10
+ proxy.nil? ? nil : proxy
11
+ end
12
+
13
+ def #{name}=(value)
14
+ association = associations[#{name.inspect}]
15
+ proxy = get_proxy(association)
16
+
17
+ if proxy.nil? || proxy.target != value
18
+ proxy = build_proxy(association)
19
+ end
20
+
21
+ proxy.replace(value)
22
+ value
23
+ end
24
+
25
+ def #{name}?
26
+ get_proxy(associations[#{name.inspect}]).present?
27
+ end
28
+
29
+ def build_#{name}(attrs={})
30
+ get_proxy(associations[#{name.inspect}]).build(attrs)
31
+ end
32
+
33
+ def create_#{name}(attrs={})
34
+ get_proxy(associations[#{name.inspect}]).create(attrs)
35
+ end
36
+
37
+ def create_#{name}!(attrs={})
38
+ get_proxy(associations[#{name.inspect}]).create!(attrs)
39
+ end
40
+ end_eval
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -25,7 +25,7 @@ module MongoMapper
25
25
  get_proxy(association).reset
26
26
  end
27
27
  instance_variables.each { |ivar| instance_variable_set(ivar, nil) }
28
- self.attributes = doc
28
+ load_from_database(doc)
29
29
  self
30
30
  else
31
31
  raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
@@ -34,14 +34,14 @@ module MongoMapper
34
34
 
35
35
  module IdentityMapQueryMethods
36
36
  def all(opts={})
37
- query = clone.update(opts)
37
+ query = clone.amend(opts)
38
38
  super.tap do |docs|
39
39
  model.remove_documents_from_map(docs) if query.fields?
40
40
  end
41
41
  end
42
42
 
43
43
  def find_one(opts={})
44
- query = clone.update(opts)
44
+ query = clone.amend(opts)
45
45
 
46
46
  if model.identity_map_on? && query.simple? && model.identity_map[query[:_id]]
47
47
  model.identity_map[query[:_id]]
@@ -5,9 +5,8 @@ module MongoMapper
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  module ClassMethods
8
- def ensure_index(spec, options={})
9
- collection.create_index(spec, options)
10
- end
8
+ extend Forwardable
9
+ def_delegators :collection, :ensure_index, :create_index, :drop_index, :drop_indexes
11
10
  end
12
11
  end
13
12
  end
@@ -111,7 +111,10 @@ module MongoMapper
111
111
  end
112
112
 
113
113
  def create_indexes_for(key)
114
- ensure_index key.name if key.options[:index] && !key.embeddable?
114
+ if key.options[:index] && !key.embeddable?
115
+ warn "[DEPRECATION] :index option when defining key #{key.name.inspect} is deprecated. Put indexes in `db/indexes.rb`"
116
+ ensure_index key.name
117
+ end
115
118
  end
116
119
 
117
120
  def create_validations_for(key)
@@ -159,7 +162,7 @@ module MongoMapper
159
162
  module InstanceMethods
160
163
  def initialize(attrs={})
161
164
  @_new = true
162
- assign(attrs)
165
+ self.attributes = attrs
163
166
  end
164
167
 
165
168
  def initialize_from_database(attrs={})
@@ -205,16 +208,17 @@ module MongoMapper
205
208
  alias :to_mongo :attributes
206
209
 
207
210
  def assign(attrs={})
211
+ warn "[DEPRECATION] #assign is deprecated, use #attributes="
208
212
  self.attributes = attrs
209
213
  end
210
214
 
211
215
  def update_attributes(attrs={})
212
- assign(attrs)
216
+ self.attributes = attrs
213
217
  save
214
218
  end
215
219
 
216
220
  def update_attributes!(attrs={})
217
- assign(attrs)
221
+ self.attributes = attrs
218
222
  save!
219
223
  end
220
224
 
@@ -24,7 +24,7 @@ module MongoMapper
24
24
  end
25
25
 
26
26
  module InstanceMethods
27
- def assign(attrs={})
27
+ def attributes=(attrs={})
28
28
  super(filter_protected_attrs(attrs))
29
29
  end
30
30
 
@@ -64,7 +64,7 @@ module MongoMapper
64
64
  query = Plucky::Query.new(collection, :transformer => transformer)
65
65
  query.extend(Decorator)
66
66
  query.object_ids(object_id_keys)
67
- query.update(options)
67
+ query.amend(options)
68
68
  query.model(self)
69
69
  query
70
70
  end
@@ -80,7 +80,7 @@ module MongoMapper
80
80
  end
81
81
 
82
82
  def find_some(ids, options={})
83
- query = query(options).update(:_id => ids.flatten.compact.uniq)
83
+ query = query(options).amend(:_id => ids.flatten.compact.uniq)
84
84
  find_many(query.to_hash).compact
85
85
  end
86
86
 
@@ -1,13 +1,12 @@
1
1
  namespace :db do
2
-
3
- if not Rake::Task.task_defined?("db:drop")
2
+ unless Rake::Task.task_defined?("db:drop")
4
3
  desc 'Drops all the collections for the database for the current Rails.env'
5
4
  task :drop => :environment do
6
5
  MongoMapper.database.collections.select {|c| c.name !~ /system/ }.each(&:drop)
7
6
  end
8
7
  end
9
8
 
10
- if not Rake::Task.task_defined?("db:seed")
9
+ unless Rake::Task.task_defined?("db:seed")
11
10
  # if another ORM has defined db:seed, don't run it twice.
12
11
  desc 'Load the seed data from db/seeds.rb'
13
12
  task :seed => :environment do
@@ -16,29 +15,29 @@ namespace :db do
16
15
  end
17
16
  end
18
17
 
19
- if not Rake::Task.task_defined?("db:setup")
18
+ unless Rake::Task.task_defined?("db:setup")
20
19
  desc 'Create the database, and initialize with the seed data'
21
20
  task :setup => [ 'db:create', 'db:seed' ]
22
21
  end
23
22
 
24
- if not Rake::Task.task_defined?("db:reseed")
23
+ unless Rake::Task.task_defined?("db:reseed")
25
24
  desc 'Delete data and seed'
26
25
  task :reseed => [ 'db:drop', 'db:seed' ]
27
26
  end
28
27
 
29
- if not Rake::Task.task_defined?("db:create")
28
+ unless Rake::Task.task_defined?("db:create")
30
29
  task :create => :environment do
31
30
  # noop
32
31
  end
33
32
  end
34
33
 
35
- if not Rake::Task.task_defined?("db:migrate")
34
+ unless Rake::Task.task_defined?("db:migrate")
36
35
  task :migrate => :environment do
37
36
  # noop
38
37
  end
39
38
  end
40
39
 
41
- if not Rake::Task.task_defined?("db:schema:load")
40
+ unless Rake::Task.task_defined?("db:schema:load")
42
41
  namespace :schema do
43
42
  task :load do
44
43
  # noop
@@ -46,7 +45,7 @@ namespace :db do
46
45
  end
47
46
  end
48
47
 
49
- if not Rake::Task.task_defined?("db:test:prepare")
48
+ unless Rake::Task.task_defined?("db:test:prepare")
50
49
  namespace :test do
51
50
  task :prepare => :environment do
52
51
  MongoMapper.connect('test')
@@ -55,6 +54,12 @@ namespace :db do
55
54
  end
56
55
  end
57
56
  end
57
+
58
+ desc 'Load the seed data from db/seeds.rb'
59
+ task :index => :environment do
60
+ indexes = File.join(Rails.root, 'db', 'indexes.rb')
61
+ load(indexes) if File.exist?(indexes)
62
+ end
58
63
  end
59
64
 
60
65
  task 'test:prepare' => 'db:test:prepare'
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
- Version = '0.10.0'
3
+ Version = '0.10.1'
4
4
  end
@@ -17,6 +17,12 @@ class OneAsProxyTest < Test::Unit::TestCase
17
17
  nil.should === @post_class.new.author
18
18
  end
19
19
 
20
+ should "not define any keys" do
21
+ count = @post_class.keys.length
22
+ @post_class.one :author, :class => @author_class
23
+ @post_class.keys.length.should == count
24
+ end
25
+
20
26
  should "allow assignment of associated document using a hash" do
21
27
  @post_class.one :author, :as => :authorable, :class => @author_class
22
28
 
@@ -58,6 +58,16 @@ class AccessibleTest < Test::Unit::TestCase
58
58
  doc.name.should == 'John'
59
59
  end
60
60
 
61
+ should "not ignore inaccessible attributes on #reload" do
62
+ doc = @doc_class.new(:name => 'John')
63
+ doc.admin = true
64
+ doc.save!
65
+
66
+ doc.reload
67
+ doc.admin.should be_true
68
+ doc.name.should == 'John'
69
+ end
70
+
61
71
  should "ignore inaccessible attribute on #update_attributes" do
62
72
  @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
63
73
  @doc.name.should == 'Ren Hoek'
@@ -70,6 +80,12 @@ class AccessibleTest < Test::Unit::TestCase
70
80
  @doc.admin.should be_false
71
81
  end
72
82
 
83
+ should "ignore inaccessible attribute on #attributes=" do
84
+ @doc.attributes = {:name => 'Ren Hoek', :admin => true}
85
+ @doc.name.should == 'Ren Hoek'
86
+ @doc.admin.should be_false
87
+ end
88
+
73
89
  should "be indifferent to whether the accessible keys are strings or symbols" do
74
90
  @doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
75
91
  @doc.name.should == 'Stimpson J. Cat'
@@ -79,6 +95,15 @@ class AccessibleTest < Test::Unit::TestCase
79
95
  should "accept nil as constructor's argument without raising exception" do
80
96
  lambda { @doc_class.new(nil) }.should_not raise_error
81
97
  end
98
+
99
+ should "ignore all attributes if called with no args" do
100
+ @doc_class = Doc do
101
+ key :name
102
+ attr_accessible
103
+ end
104
+
105
+ @doc_class.new(:name => 'Steve Sloan').name.should be_nil
106
+ end
82
107
  end
83
108
 
84
109
  context "Single collection inherited accessible attributes" do
@@ -12,6 +12,14 @@ class IndexingTest < Test::Unit::TestCase
12
12
  end
13
13
  teardown { drop_indexes(@document) }
14
14
 
15
+ [:create_index, :ensure_index, :drop_index, :drop_indexes].each do |method|
16
+ should "delegate #{method} to collection" do
17
+ @document.stubs(:collection).returns(mock(:name => :foo))
18
+ @document.collection.expects(method).with(:arg)
19
+ @document.send(method, :arg)
20
+ end
21
+ end
22
+
15
23
  should "allow creating index for a key" do
16
24
  @document.ensure_index :first_name
17
25
  @document.should have_index('first_name_1')
@@ -35,7 +43,7 @@ class IndexingTest < Test::Unit::TestCase
35
43
  end
36
44
 
37
45
  should "work with :index shortcut when defining key" do
38
- @document.key :father, String, :index => true
46
+ silence_stderr { @document.key :father, String, :index => true }
39
47
  @document.should have_index('father_1')
40
48
  end
41
49
  end
@@ -64,6 +64,16 @@ class ProtectedTest < Test::Unit::TestCase
64
64
  doc.name.should == 'John'
65
65
  end
66
66
 
67
+ should "not ignore protected attributes on #reload" do
68
+ doc = @doc_class.new(:name => 'John')
69
+ doc.admin = true
70
+ doc.save!
71
+
72
+ doc.reload
73
+ doc.admin.should be_true
74
+ doc.name.should == 'John'
75
+ end
76
+
67
77
  should "ignore protected attribute on #update_attributes" do
68
78
  @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
69
79
  @doc.name.should == 'Ren Hoek'
@@ -76,6 +86,12 @@ class ProtectedTest < Test::Unit::TestCase
76
86
  @doc.admin.should be_false
77
87
  end
78
88
 
89
+ should "ignore protecteds attribute on #attributes=" do
90
+ @doc.attributes = {:name => 'Stimpson J. Cat', :admin => true}
91
+ @doc.name.should == 'Stimpson J. Cat'
92
+ @doc.admin.should be_false
93
+ end
94
+
79
95
  should "be indifferent to whether the protected keys are strings or symbols" do
80
96
  @doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
81
97
  @doc.name.should == 'Stimpson J. Cat'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 53
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 10
9
- - 0
10
- version: 0.10.0
9
+ - 1
10
+ version: 0.10.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nunemaker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-02 00:00:00 -04:00
18
+ date: 2011-11-07 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -55,12 +55,12 @@ dependencies:
55
55
  requirements:
56
56
  - - ~>
57
57
  - !ruby/object:Gem::Version
58
- hash: 3
58
+ hash: 15
59
59
  segments:
60
60
  - 0
61
- - 3
62
- - 8
63
- version: 0.3.8
61
+ - 4
62
+ - 0
63
+ version: 0.4.0
64
64
  prerelease: false
65
65
  type: :runtime
66
66
  requirement: *id003
@@ -128,6 +128,7 @@ files:
128
128
  - lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb
129
129
  - lib/mongo_mapper/plugins/associations/one_proxy.rb
130
130
  - lib/mongo_mapper/plugins/associations/proxy.rb
131
+ - lib/mongo_mapper/plugins/associations/single_association.rb
131
132
  - lib/mongo_mapper/plugins/associations.rb
132
133
  - lib/mongo_mapper/plugins/caching.rb
133
134
  - lib/mongo_mapper/plugins/callbacks.rb