diff --git a/.travis.yml b/.travis.yml index 196eba8..21aba45 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,10 @@ language: ruby before_install: - - gem install bundler --pre + - gem install bundler + +services: + - mongodb rvm: - 1.9.3 diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index da590c5..de74e39 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -53,7 +53,8 @@ module Draper # @param [Hash] options # see {Decorator.decorate_collection}. def decorate(options = {}) - decorator_class(options[:namespace]).decorate_collection(scoped, options.reverse_merge(with: nil)) + collection = Rails::VERSION::MAJOR >= 4 ? all : scoped + decorator_class(options[:namespace]).decorate_collection(collection, options.reverse_merge(with: nil)) end # Infers the decorator class to be used by {Decoratable#decorate} (e.g. diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 2c8d463..67d0730 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -108,9 +108,11 @@ module Draper end describe ".decorate" do + let(:scoping_method) { Rails::VERSION::MAJOR >= 4 ? :all : :scoped } + it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] - Product.stub scoped: scoped + Product.stub scoping_method => scoped Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) expect(Product.decorate).to be :decorated_collection @@ -118,7 +120,7 @@ module Draper it "accepts options" do options = {with: ProductDecorator, context: {some: "context"}} - Product.stub scoped: [] + Product.stub scoping_method => [] Product.decorator_class.should_receive(:decorate_collection).with([], options) Product.decorate(options) diff --git a/spec/dummy/app/decorators/mongoid_post_decorator.rb b/spec/dummy/app/decorators/mongoid_post_decorator.rb new file mode 100644 index 0000000..61596e4 --- /dev/null +++ b/spec/dummy/app/decorators/mongoid_post_decorator.rb @@ -0,0 +1,2 @@ +class MongoidPostDecorator < Draper::Decorator +end diff --git a/spec/dummy/spec/decorators/post_decorator_spec.rb b/spec/dummy/spec/decorators/post_decorator_spec.rb index e90011c..453d2d5 100755 --- a/spec/dummy/spec/decorators/post_decorator_spec.rb +++ b/spec/dummy/spec/decorators/post_decorator_spec.rb @@ -46,8 +46,8 @@ describe PostDecorator do end it "serializes to XML" do - # Rails < 3.2 does not use `serializable_hash` in `to_xml` - pending if Rails.version.to_f < 3.2 + pending("Rails < 3.2 does not use `serializable_hash` in `to_xml`") if Rails.version.to_f < 3.2 + xml = Capybara.string(decorator.to_xml) expect(xml).to have_css "post > updated-at", text: "overridden" end diff --git a/spec/dummy/spec/models/mongoid_post_spec.rb b/spec/dummy/spec/models/mongoid_post_spec.rb index a5a35d0..1707bd1 100644 --- a/spec/dummy/spec/models/mongoid_post_spec.rb +++ b/spec/dummy/spec/models/mongoid_post_spec.rb @@ -1,10 +1,8 @@ require 'spec_helper' +require 'shared_examples/decoratable' if defined?(Mongoid) describe MongoidPost do - it "is decoratable" do - expect(MongoidPost).to respond_to :decorator_class - expect(MongoidPost.new).to respond_to :decorate - end + it_behaves_like "a decoratable model" end end diff --git a/spec/dummy/spec/models/post_spec.rb b/spec/dummy/spec/models/post_spec.rb index 4e7ef44..8bdd192 100644 --- a/spec/dummy/spec/models/post_spec.rb +++ b/spec/dummy/spec/models/post_spec.rb @@ -1,14 +1,6 @@ require 'spec_helper' +require 'shared_examples/decoratable' describe Post do - describe "#==" do - it "is true for other instances' decorators" do - Post.create - one = Post.first - other = Post.first - - expect(one).not_to be other - expect(one == other.decorate).to be_true - end - end + it_behaves_like "a decoratable model" end diff --git a/spec/dummy/spec/shared_examples/decoratable.rb b/spec/dummy/spec/shared_examples/decoratable.rb new file mode 100644 index 0000000..a20b058 --- /dev/null +++ b/spec/dummy/spec/shared_examples/decoratable.rb @@ -0,0 +1,24 @@ +shared_examples_for "a decoratable model" do + describe ".decorate" do + it "applies a collection decorator to a scope" do + described_class.create + decorated = described_class.limit(1).decorate + + expect(decorated).to have(1).items + expect(decorated).to be_decorated + end + end + + describe "#==" do + it "is true for other instances' decorators" do + pending "Mongoid < 3.1 overrides `#==`" if defined?(Mongoid) && Mongoid::VERSION.to_f < 3.1 && described_class < Mongoid::Document + + described_class.create + one = described_class.first + other = described_class.first + + expect(one).not_to be other + expect(one == other.decorate).to be_true + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4fa0fd1..db6510f 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'bundler/setup' require 'draper' +require 'rails/version' require 'action_controller' require 'action_controller/test_case'