Skip to content
Browse files

Merge pull request #486 from haines/scoped_deprecation

Prevent Rails 4.0 deprecation warning for `scoped`
  • Loading branch information...
2 parents 7d63298 + 9873c33 commit 2d5756b3216d92e7cac7166cdabd5b1a32fb97d0 @steveklabnik steveklabnik committed Mar 3, 2013
View
5 .travis.yml
@@ -1,7 +1,10 @@
language: ruby
before_install:
- - gem install bundler --pre
+ - gem install bundler
+
+services:
+ - mongodb
rvm:
- 1.9.3
View
3 lib/draper/decoratable.rb
@@ -53,7 +53,8 @@ module ClassMethods
# @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.
View
6 spec/draper/decoratable_spec.rb
@@ -108,17 +108,19 @@ 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
end
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)
View
2 spec/dummy/app/decorators/mongoid_post_decorator.rb
@@ -0,0 +1,2 @@
+class MongoidPostDecorator < Draper::Decorator
+end
View
4 spec/dummy/spec/decorators/post_decorator_spec.rb
@@ -46,8 +46,8 @@
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
View
6 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
View
12 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
View
24 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
View
1 spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'bundler/setup'
require 'draper'
+require 'rails/version'
require 'action_controller'
require 'action_controller/test_case'

0 comments on commit 2d5756b

Please sign in to comment.
Something went wrong with that request. Please try again.