Skip to content

Commit

Permalink
updated to work with Mongoid 2.0.2 and above
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Jun 24, 2011
1 parent 9221217 commit bf9b93d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 64 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color --format nested
8 changes: 4 additions & 4 deletions Gemfile
@@ -1,10 +1,10 @@
source :gemcutter

gem 'mongoid', '>=2.0.0.rc.7'
gem "mongoid_adjust", ">= 0.1.1"
gem 'bson_ext', '>=1.0.1'
gem 'mongoid', '>= 2.0.2'
gem "mongoid_adjust", '>= 0.1.1'
gem 'bson_ext', '>= 1.0.1'

group :test do
gem 'rspec', '>= 2.0.0.beta.15'
gem 'rspec', '>= 2.4.0'
gem 'mocha'
end
41 changes: 25 additions & 16 deletions README.markdown → README.textile
@@ -1,24 +1,29 @@
# Mongoid embedded helper #
h1. Mongoid embedded helper

The Mongoid EmbeddedHelper helps overcome the limitation that you can't perform direct queries on an embedded collection without accessing it from the root.
It simply introduces a generic function that performs the "local" query by accessing it from the nearest root ;)

## Update
h2. Update June 24, 2011

In the latest release (0.3.0) the #assimilate method has been removed in order to work with Mongoid 2.0.0.rc1
In the latest release (0.3.1) the gem has been tested to work with Mongoid 2.0.2 (run the specs!)

## Installation ##
h2. Install

<code>gem install mongoid_embedded_helper</code>
In Gemfile:

## Usage ##
@gem 'mongoid_embedded_helper'@

Run bundler

@$ bundle@

h2. Usage

Mongoid doesn't allow you to perform direct queries on an embedded collection. You have to access the embedded collection from the root in order to do this.
To overcome this limitation, use the EmbeddedHelper module to add a method <code>query_class</code> which will find the closest non-embedded node in the hierarchy and
then traverse down the path back to the object, so that you can perform the query on the embedded collection.

<pre>
require 'mongoid_embedded_helper'
<pre>require 'mongoid_embedded_helper'

class Item
include Mongoid::Document
Expand All @@ -35,11 +40,13 @@ item.query_class.where(:number.gt => 1).to_a

</pre>

## Adjust!
h2. Adjust!

An as added bonus, an extra adjust! method has been added to Document, Criteria and Array. This enables multi-mutation of attributes!
This functionality comes along from the _mongoid_adjust_ gem.

Here an example using a number

*Here an example using a number*
<pre>
it "should times all positions (greater than 1) by 2" do
result = @person.lists[0].items.where(:pos.gt => 1).adjust!(:pos => lambda {|e| e * 2})
Expand All @@ -49,7 +56,8 @@ end

Passing a number is a convenience shortcut for adding a number instead of having to use the Proc approach as shown below.

*Here an example using a lambda (or Proc):*
Here an example using a lambda (or Proc):

<pre>
it "should times all positions (greater than 1) by 2" do
result = @person.lists[0].items.where(:pos.gt => 1).adjust!(:pos => lambda {|e| e * 2})
Expand All @@ -59,23 +67,24 @@ end

The Proc approach can in simple cases be declared using a shortcut Symbol/String approach

*Here an example using a symbol :upcase to declare that the method #upcase should be used as the mutator:*
Here an example using a symbol :upcase to declare that the method #upcase should be used as the mutator:

<pre>
it "should upcase the name - using symbol arg" do
result = @person.adjust!(:name => :upcase)
result.name.should == 'Kristian'.upcase
end
</pre>

*And using a String 'upcase' instead of a symbol:*
And using a String 'upcase' instead of a symbol:

<pre>
it "should upcase the name - using symbol arg" do
result = @person.adjust!(:name => 'upcase')
result.name.should == 'Kristian'.upcase
end
</pre>

h2. Copyright

## Copyright ##

Copyright (c) <2010> <Kristian Mandrup>
Copyright (c) 2010 Kristian Mandrup
43 changes: 4 additions & 39 deletions Rakefile
Expand Up @@ -10,48 +10,13 @@ begin
gem.email = "kmandrup@gmail.com"
gem.homepage = "http://github.com/kristianmandrup/mongoid_embedded_helper"
gem.authors = ["Kristian Mandrup"]
gem.add_dependency "mongoid", ">= 2.0.0.beta.18"
gem.add_dependency "mongoid_adjust", ">= 0.1.1"
gem.add_dependency "bson_ext", ">= 1.0.8"
gem.add_dependency "mongoid", ">= 2.0.0.rc.7"
gem.add_dependency "mongoid_adjust", ">= 0.1.1"
gem.add_dependency "bson_ext", ">= 1.0.8"

gem.add_development_dependency "rspec", ">=2.0.1"
gem.add_development_dependency "rspec", ">= 2.4.0"
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end
#
# require 'rake/testtask'
# Rake::TestTask.new(:test) do |test|
# test.libs << 'lib' << 'test'
# test.pattern = 'test/**/test_*.rb'
# test.verbose = true
# end
#
# begin
# require 'rcov/rcovtask'
# Rcov::RcovTask.new do |test|
# test.libs << 'test'
# test.pattern = 'test/**/test_*.rb'
# test.verbose = true
# end
# rescue LoadError
# task :rcov do
# abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
# end
# end
#
# task :test => :check_dependencies
#
# task :default => :test
#
# require 'rake/rdoctask'
# Rake::RDocTask.new do |rdoc|
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
#
# rdoc.rdoc_dir = 'rdoc'
# rdoc.title = "mongoid_acts_as_tree #{version}"
# rdoc.rdoc_files.include('README*')
# rdoc.rdoc_files.include('lib/**/*.rb')
# end
#
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.0
0.3.1
6 changes: 2 additions & 4 deletions lib/mongoid/embedded_helper.rb
Expand Up @@ -3,15 +3,13 @@
module Mongoid
module EmbeddedHelper
def self.included(model)
model.class_eval do

model.class_eval do
alias_method :old_parentize, :parentize
def parentize(document)
old_parentize document
send(:after_parentize) if respond_to?(:after_parentize)
# run_callbacks(:after_parentize)
end

end
end
end

Expand Down

0 comments on commit bf9b93d

Please sign in to comment.