Skip to content

Commit

Permalink
#update allowing using :id instead of :_id
Browse files Browse the repository at this point in the history
  • Loading branch information
al6x committed Dec 25, 2011
1 parent 935d57b commit 00941b3
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 38 deletions.
10 changes: 5 additions & 5 deletions docs/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!)}
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/composite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/modifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions docs/site/associations.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h3>Basics</h3>
</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>
Expand Down Expand Up @@ -167,7 +167,7 @@ <h3>Basics</h3>
<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>
Expand All @@ -193,7 +193,7 @@ <h3>Basics</h3>
</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>
Expand Down Expand Up @@ -321,10 +321,10 @@ <h3>Caching comments count.</h3>
<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>
Expand Down
4 changes: 2 additions & 2 deletions docs/site/composite.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion docs/site/modifiers.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 1 addition & 3 deletions lib/mongo/model/conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/model/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/model/identity_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/model/integration/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo/model/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/model/query_mixin.rb
Original file line number Diff line number Diff line change
@@ -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?

Expand Down
2 changes: 1 addition & 1 deletion spec/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/crud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/misc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)}

Expand All @@ -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'
Expand Down

0 comments on commit 00941b3

Please sign in to comment.