Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decorating Mongoid::Criteria #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gem 'rails', '~> 3.2.0'
gem 'rspec-rails'
gem 'capybara', '>= 2'
gem 'sqlite3'
gem 'mongoid'
1 change: 1 addition & 0 deletions lib/active_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_decorator/version'
require 'active_decorator/decorator'
require 'active_decorator/railtie'
require 'active_decorator/orm/mongoid'
2 changes: 2 additions & 0 deletions lib/active_decorator/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def to_a_with_decorator
end
alias_method_chain :to_a, :decorator
end
elsif defined?(Mongoid) && obj.is_a?(Mongoid::Criteria)
obj.extend ORM::Mongoid
else
d = decorator_for obj.class
return obj unless d
Expand Down
31 changes: 31 additions & 0 deletions lib/active_decorator/orm/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module ActiveDecorator
module ORM
module Mongoid
def first
decorate super
end

def last
decorate super
end

def each(&block)
super do |document|
yield decorate(document)
end
end

def map(&block)
super do |document|
yield decorate(document)
end
end

private

def decorate(object)
ActiveDecorator::Decorator.instance.decorate(object)
end
end
end
end
25 changes: 25 additions & 0 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe ActiveDecorator::Decorator do
subject do
ActiveDecorator::Decorator.instance
end

describe '#decorate' do
context 'Mongoid::Criteria' do
let(:criteria) {Mongoid::Criteria.new(MongoidDummy)}

it 'is extended with ORM::Mongoid' do
criteria.should_receive(:extend).with(ActiveDecorator::ORM::Mongoid)
subject.decorate(criteria)
end
end
end
end

class MongoidDummy
include Mongoid::Document
end

module MongoidDummyDecorator
end
40 changes: 40 additions & 0 deletions spec/orm/mongoid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

describe ActiveDecorator::ORM::Mongoid do
subject do
MongoidDummy.create
MongoidDummy.all.extend(ActiveDecorator::ORM::Mongoid)
end

it 'decorates #first' do
subject.first.thing.should be_true
end

it 'decorates #last' do
subject.last.thing.should be_true
end

it 'decorates #each' do
subject.each do |document|
document.thing.should be_true
end
end

it 'decorates #map' do
subject.map(&:thing).should == [true]
end
end

class MongoidDummy
include Mongoid::Document

def thing
false
end
end

module MongoidDummyDecorator
def thing
true
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# needs to load the app before loading rspec/rails => capybara
require 'fake_app/fake_app'
require 'rspec/rails'
require 'mongoid'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Expand All @@ -18,5 +19,10 @@
Book.delete_all
Author.delete_all
Movie.delete_all
Mongoid::Config.purge!
end
end

Mongoid.configure do |config|
config.connect_to('active_decorator_test')
end