Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#update allowing using :id instead of :_id

  • Loading branch information...
commit 00941b36d6382130a26616419bce0086509e87c7 1 parent 935d57b
@alexeypetrushin authored
View
10 docs/associations.rb
@@ -33,7 +33,7 @@ class Post
# later to select comments belongign to this post (there's no
# database call at this point).
def comments
- Comment.where post_id: _id
+ Comment.where post_id: id
end
# If the post will be deleted, all comments also should be deleted.
after_delete{|post| post.comments.each(&:delete!)}
@@ -49,14 +49,14 @@ class Comment
attr_accessor :text
- # Every comment has `post_id` attribute with `_id` of corresponding post.
+ # Every comment has `post_id` attribute with `id` of corresponding post.
attr_accessor :post_id
# We need to ensure that comment always belongs to some post.
validates_presence_of :post_id
# Adding method allowing to assign post to comment.
def post= post
- self.post_id = post._id
+ self.post_id = post.id
_cache[:post] = post
end
# Retrieving the post this comment belongs to.
@@ -110,10 +110,10 @@ def comments_count; @comments_count ||= 0 end
# do it in more efficient way using [modifiers][modifiers].
class Comment
after_create do |comment|
- Post.update({_id: comment.post_id}, {_inc: {comments_count: 1}})
+ Post.update({id: comment.post_id}, {_inc: {comments_count: 1}})
end
after_delete do |comment|
- Post.update({_id: comment.post_id}, {_inc: {comments_count: -1}})
+ Post.update({id: comment.post_id}, {_inc: {comments_count: -1}})
end
end
View
4 docs/composite.rb
@@ -3,8 +3,8 @@
# Models are just ordinary Ruby Objects, so You can combine and mix it as You wish.
# The only differences are
#
-# - main object has the `_id` attribute.
-# - child objects doesn't have `_id`, but have `_parent` - reference to the main object.
+# - main object has the `id` attribute.
+# - child objects doesn't have `id`, but have `_parent` - reference to the main object.
#
# [Callbacks][callbacks], [validations][validations] and [conversions][conversions]
# works on embedded objects the same way as on the main.
View
2  docs/modifiers.rb
@@ -28,7 +28,7 @@ class Unit
p tassadar.life # => 40
# There's also helper on the model class.
-Unit.update({_id: tassadar._id}, {_inc: {life: -20}})
+Unit.update({id: tassadar.id}, {_inc: {life: -20}})
tassadar.reload
p tassadar.life # => 20
View
10 docs/site/associations.html
@@ -119,7 +119,7 @@
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">comments</span>
- <span class="no">Comment</span><span class="o">.</span><span class="n">where</span> <span class="n">post_id</span><span class="p">:</span> <span class="n">_id</span>
+ <span class="no">Comment</span><span class="o">.</span><span class="n">where</span> <span class="n">post_id</span><span class="p">:</span> <span class="n">id</span>
<span class="k">end</span></pre></div>
</td>
</tr>
@@ -167,7 +167,7 @@
<div class="pilwrap">
<a class="pilcrow" href="#section-10">&#182;</a>
</div>
- <p>Every comment has <code>post_id</code> attribute with <code>_id</code> of corresponding post.</p>
+ <p>Every comment has <code>post_id</code> attribute with <code>id</code> of corresponding post.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kp">attr_accessor</span> <span class="ss">:post_id</span></pre></div>
@@ -193,7 +193,7 @@
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">post</span><span class="o">=</span> <span class="n">post</span>
- <span class="nb">self</span><span class="o">.</span><span class="n">post_id</span> <span class="o">=</span> <span class="n">post</span><span class="o">.</span><span class="n">_id</span>
+ <span class="nb">self</span><span class="o">.</span><span class="n">post_id</span> <span class="o">=</span> <span class="n">post</span><span class="o">.</span><span class="n">id</span>
<span class="n">_cache</span><span class="o">[</span><span class="ss">:post</span><span class="o">]</span> <span class="o">=</span> <span class="n">post</span>
<span class="k">end</span></pre></div>
</td>
@@ -321,10 +321,10 @@
<td class=code>
<div class='highlight'><pre><span class="k">class</span> <span class="nc">Comment</span>
<span class="n">after_create</span> <span class="k">do</span> <span class="o">|</span><span class="n">comment</span><span class="o">|</span>
- <span class="no">Post</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">_id</span><span class="p">:</span> <span class="n">comment</span><span class="o">.</span><span class="n">post_id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">comments_count</span><span class="p">:</span> <span class="mi">1</span><span class="p">}})</span>
+ <span class="no">Post</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">id</span><span class="p">:</span> <span class="n">comment</span><span class="o">.</span><span class="n">post_id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">comments_count</span><span class="p">:</span> <span class="mi">1</span><span class="p">}})</span>
<span class="k">end</span>
<span class="n">after_delete</span> <span class="k">do</span> <span class="o">|</span><span class="n">comment</span><span class="o">|</span>
- <span class="no">Post</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">_id</span><span class="p">:</span> <span class="n">comment</span><span class="o">.</span><span class="n">post_id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">comments_count</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">}})</span>
+ <span class="no">Post</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">id</span><span class="p">:</span> <span class="n">comment</span><span class="o">.</span><span class="n">post_id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">comments_count</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">}})</span>
<span class="k">end</span>
<span class="k">end</span></pre></div>
</td>
View
4 docs/site/composite.html
@@ -46,8 +46,8 @@
The only differences are</p>
<ul>
-<li>main object has the <code>_id</code> attribute.</li>
-<li>child objects doesn&rsquo;t have <code>_id</code>, but have <code>_parent</code> &ndash; reference to the main object.</li>
+<li>main object has the <code>id</code> attribute.</li>
+<li>child objects doesn&rsquo;t have <code>id</code>, but have <code>_parent</code> &ndash; reference to the main object.</li>
</ul>
<p><a href="callbacks.html">Callbacks</a>, <a href="validations.html">validations</a> and <a href="conversions.html">conversions</a>
View
2  docs/site/modifiers.html
@@ -112,7 +112,7 @@
<p>There&rsquo;s also helper on the model class.</p>
</td>
<td class=code>
- <div class='highlight'><pre><span class="no">Unit</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">_id</span><span class="p">:</span> <span class="n">tassadar</span><span class="o">.</span><span class="n">_id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">life</span><span class="p">:</span> <span class="o">-</span><span class="mi">20</span><span class="p">}})</span>
+ <div class='highlight'><pre><span class="no">Unit</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">id</span><span class="p">:</span> <span class="n">tassadar</span><span class="o">.</span><span class="n">id</span><span class="p">},</span> <span class="p">{</span><span class="n">_inc</span><span class="p">:</span> <span class="p">{</span><span class="n">life</span><span class="p">:</span> <span class="o">-</span><span class="mi">20</span><span class="p">}})</span>
<span class="n">tassadar</span><span class="o">.</span><span class="n">reload</span>
<span class="nb">p</span> <span class="n">tassadar</span><span class="o">.</span><span class="n">life</span> <span class="c1"># =&gt; 20</span></pre></div>
</td>
View
4 lib/mongo/model/conversion.rb
@@ -61,9 +61,7 @@ def to_rson options = {}
result[:errors] = errors
end
- result[:id] = _id if _id and (options[:id] != false)
- result[:_saved] = _saved if _saved and (options[:_saved] != false)
- result[:param] = to_param if _saved and respond_to?(:to_param) and (options[:param] != false)
+ result[:id] = id if id and (options[:id] != false)
result
end
View
2  lib/mongo/model/crud.rb
@@ -46,7 +46,7 @@ def delete! *args
def update doc, options = {}
with_collection options do |collection, options|
- collection.update({_id: _id}, doc, options)
+ collection.update({id: id}, doc, options)
end
end
View
4 lib/mongo/model/identity_map.rb
@@ -6,7 +6,7 @@ module Mongo::Model::IdentityMap
def original
unless _cache[:original_cached]
_cache[:original_cached] = true
- _cache[:original] = _id && self.class.get_from_identity_map(_id)
+ _cache[:original] = id && self.class.get_from_identity_map(id)
end
_cache[:original]
end
@@ -23,7 +23,7 @@ def get_from_identity_map id
def from_mongo doc
model = super doc
- model.class.identity_map[model._id] = doc if model._id
+ model.class.identity_map[model.id] = doc if model.id
model
end
end
View
4 lib/mongo/model/integration/rails.rb
@@ -14,10 +14,10 @@ def full_messages
module Mongo::Model::Rails
def to_model; self end
- def persisted?; !!_id end
+ def persisted?; _saved end
def to_key
- persisted? ? [_id] : nil
+ persisted? ? [id] : nil
end
def new_record?; new? end
View
2  lib/mongo/model/integration/validatable/uniqueness_validator.rb
@@ -19,7 +19,7 @@ def valid?(instance)
end
# Make sure we're not including the current document in the query.
- conditions[:_id] = {_ne: instance._id} if instance._id
+ conditions[:_id] = {_ne: instance.id} if instance.id
!klass.exists?(conditions)
end
View
6 lib/mongo/model/misc.rb
@@ -18,11 +18,11 @@ def dom_id
end
def to_param
- _id.try :to_s
+ id.try :to_s
end
def reload
- obj = self.class.by_id!(_id || raise("can't reload new document (#{self})!"))
+ obj = self.class.by_id!(id || raise("can't reload new document (#{self})!"))
instance_variables.each{|n| remove_instance_variable n}
obj.instance_variables.each do |n|
instance_variable_set n, obj.instance_variable_get(n)
@@ -33,7 +33,7 @@ def reload
def original
unless _cache[:original_cached]
_cache[:original_cached] = true
- _cache[:original] = _id && self.class.by_id(_id)
+ _cache[:original] = id && self.class.by_id(id)
end
_cache[:original]
end
View
2  lib/mongo/model/query_mixin.rb
@@ -1,6 +1,6 @@
module Mongo::Model::QueryMixin
def exists? options = {}
- self.class.count({_id: _id}, options) > 0
+ self.class.count({id: id}, options) > 0
end
alias_method :exist?, :exists?
View
2  spec/associations_spec.rb
@@ -13,7 +13,7 @@ class Post
attr_accessor :text
def comments
- Comment.query({post_id: _id}, {sort: [[:text, -1]]})
+ Comment.query({post_id: id}, {sort: [[:text, -1]]})
end
end
View
8 spec/crud_spec.rb
@@ -47,7 +47,7 @@ def == o; [self.class, name, info] == [o.class, o.name, o.info] end
# Create.
@unit.save.should be_true
- @unit._id.should_not be_nil
+ @unit.id.should_not be_nil
# Read.
Unit.count.should == 1
@@ -69,7 +69,7 @@ def == o; [self.class, name, info] == [o.class, o.name, o.info] end
it 'should be able to save model to another collection' do
# Create.
@unit.save(collection: db.heroes).should be_true
- @unit._id.should_not be_nil
+ @unit.id.should_not be_nil
# Read.
Unit.count.should == 0
@@ -111,7 +111,7 @@ def == o; [self.class, name, info] == [o.class, o.name, o.info] end
it 'should accept modifiers' do
unit = Unit.create! name: 'Zeratul'
- Unit.update({_id: unit._id}, _set: {name: 'Tassadar'})
+ Unit.update({id: unit.id}, _set: {name: 'Tassadar'})
unit.reload
unit.name.should == 'Tassadar'
@@ -152,7 +152,7 @@ def == o; [self.class, self.name] == [o.class, o.name] end
it 'should perform CRUD' do
# Create.
@unit.save.should be_true
- @unit._id.should_not be_nil
+ @unit.id.should_not be_nil
# Read.
Unit.count.should == 1
View
2  spec/misc_spec.rb
@@ -85,7 +85,7 @@ class Unit
unit = Unit.first
unit.name = "Tassadar"
- Unit.should_receive(:first).with(_id: unit._id).and_return{db.units.first(_id: unit._id)}
+ Unit.should_receive(:first).with(_id: unit.id).and_return{db.units.first(id: unit.id)}
unit.original.name.should == "Zeratul"
end
View
2  spec/query_spec.rb
@@ -43,7 +43,7 @@ class Unit
u = Unit.build(name: 'Zeratul')
u.save!
Unit.first_by_name('Zeratul').name.should == 'Zeratul'
- Unit.by_id!(u._id).name.should == 'Zeratul'
+ Unit.by_id!(u.id).name.should == 'Zeratul'
end
it 'should be integrated with build, create and create!' do
View
4 todo.md
@@ -11,7 +11,7 @@
def comments
- Comment.query({item_id: _id}, {sort: [[created_at:, -1]]})
+ Comment.query({item_id: id}, {sort: [[created_at:, -1]]})
end
after_delete{comments.each(&:delete!)}
@@ -34,7 +34,7 @@
# relations
def spaces
- Models::Space.query account_id: _id
+ Models::Space.query account_id: id
end
after_delete{|m| m.spaces.each &:delete!}
has_many :spaces, dependent: :delete, foreign_key: :account_id, class_name: 'Models::Space'
Please sign in to comment.
Something went wrong with that request. Please try again.