Skip to content

Commit

Permalink
Merge pull request #752 from drapergem/rails-5
Browse files Browse the repository at this point in the history
Rails 5 compatibility
  • Loading branch information
seanlinsley authored Jul 10, 2016
2 parents 00067f2 + 92dd584 commit b2f9f08
Show file tree
Hide file tree
Showing 30 changed files with 181 additions and 173 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.gem
*.rvmrc
.rvmrc
.ruby-version
.ruby-gemset
.bundle
Gemfile.lock
pkg/*
Expand Down
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@ services:
- mongodb

rvm:
- 2.1.5
- 2.2.1

env:
- "RAILS_VERSION=4.0"
- "RAILS_VERSION=4.1"
- "RAILS_VERSION=4.2"
- 2.2.5
- 2.3.1
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Draper Changelog

## 3.0.0 - 2016-07-XX

* Added support for Rails 5, dropped 4.0 - 4.2
* Ruby >= 2.2 is required, matching Rails 5
* Dropped support for ActiveModelSerializers 0.8

## 2.1.0 - 2015-03-26

* Cleared most issues and merged a few PRs
Expand Down
9 changes: 4 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ platforms :ruby do
end

platforms :jruby do
gem "minitest", ">= 3.0"
gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.beta2"
gem "minitest"
gem "activerecord-jdbcsqlite3-adapter"
end

version = ENV["RAILS_VERSION"] || "4.1"

eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
gem "rails", "~> 5.0"
gem "mongoid", github: "mongodb/mongoid"
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Draper: View Models for Rails

[![TravisCI Build Status](https://travis-ci.org/drapergem/draper.svg?branch=master)](http://travis-ci.org/drapergem/draper)
[![Code Climate](https://codeclimate.com/github/drapergem/draper.png)](https://codeclimate.com/github/drapergem/draper)
[![Inline docs](http://inch-ci.org/github/drapergem/draper.png?branch=master)](http://inch-ci.org/github/drapergem/draper)
[![Code Climate](https://codeclimate.com/github/drapergem/draper.svg)](https://codeclimate.com/github/drapergem/draper)
[![Inline docs](http://inch-ci.org/github/drapergem/draper.svg?branch=master)](http://inch-ci.org/github/drapergem/draper)

Draper adds an object-oriented layer of presentation logic to your Rails
application.
Expand Down Expand Up @@ -586,15 +586,15 @@ end

This is only necessary when proxying class methods.

Once this association between the decorator and the model is set up, you can call `SomeModel.decorator_class` to access class methods defined in the decorator. If necessary, you can check if your model is decorated with `SomeModel.decorator_class?`.
Once this association between the decorator and the model is set up, you can call
`SomeModel.decorator_class` to access class methods defined in the decorator.
If necessary, you can check if your model is decorated with `SomeModel.decorator_class?`.

### Making Models Decoratable

Models get their `decorate` method from the `Draper::Decoratable` module, which
is included in `ActiveRecord::Base` and `Mongoid::Document` by default. If
you're [using another
ORM](https://github.com/drapergem/draper/wiki/Using-other-ORMs) (including
versions of Mongoid prior to 3.0), or want to decorate plain old Ruby objects,
you're using another ORM, or want to decorate plain old Ruby objects,
you can include this module manually.

## Contributors
Expand All @@ -603,7 +603,11 @@ Draper was conceived by Jeff Casimir and heavily refined by Steve Klabnik and a
great community of open source
[contributors](https://github.com/drapergem/draper/contributors).

### Core Team
### Current maintainers

* Sean Linsley

### Historical maintainers

* Jeff Casimir ([email protected])
* Steve Klabnik ([email protected])
Expand Down
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ desc "Run all specs"
task "spec" => "spec:all"

namespace "spec" do
task "all" => ["draper", "generators", "integration"]
# TODOD: add back "generators"
task "all" => ["draper", "integration"]

def spec_task(name)
desc "Run #{name} specs"
Expand Down Expand Up @@ -64,6 +65,6 @@ namespace "db" do
run_in_dummy_app "rm -f db/*.sqlite3"
run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed"
run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed"
run_in_dummy_app "RAILS_ENV=test rake db:schema:load"
run_in_dummy_app "RAILS_ENV=test rake db:environment:set db:schema:load"
end
end
17 changes: 10 additions & 7 deletions draper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'activesupport', '>= 3.0'
s.add_dependency 'actionpack', '>= 3.0'
s.required_ruby_version = '>= 2.2.2'

s.add_dependency 'activesupport', '~> 5.0'
s.add_dependency 'actionpack', '~> 5.0'
s.add_dependency 'request_store', '~> 1.0'
s.add_dependency 'activemodel', '>= 3.0'
s.add_dependency 'activemodel', '~> 5.0'
s.add_dependency 'activemodel-serializers-xml', '~> 1.0'

s.add_development_dependency 'ammeter'
s.add_development_dependency 'rake', '>= 0.9.2'
s.add_development_dependency 'rspec-rails', '~> 3.2'
s.add_development_dependency 'minitest-rails', '>= 1.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'minitest-rails'
s.add_development_dependency 'capybara'
s.add_development_dependency 'active_model_serializers'
s.add_development_dependency 'active_model_serializers', '~> 0.10'
end
3 changes: 0 additions & 3 deletions gemfiles/4.0.gemfile

This file was deleted.

3 changes: 0 additions & 3 deletions gemfiles/4.1.gemfile

This file was deleted.

3 changes: 0 additions & 3 deletions gemfiles/4.2.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion lib/draper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.setup_action_controller(base)
extend Draper::HelperSupport
extend Draper::DecoratesAssigned

before_filter :activate_draper
before_action :activate_draper
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/draper/decoratable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ module ClassMethods
# @param [Hash] options
# see {Decorator.decorate_collection}.
def decorate(options = {})
collection = Rails::VERSION::MAJOR >= 4 ? all : scoped
decorator_class.decorate_collection(collection, options.reverse_merge(with: nil))
decorator_class.decorate_collection(all, options.reverse_merge(with: nil))
end

def decorator_class?
Expand Down
12 changes: 0 additions & 12 deletions lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,6 @@ def instance_of?(klass)
super || object.instance_of?(klass)
end

if RUBY_VERSION < "2.0"
# nasty hack to stop 1.9.x using the delegated `to_s` in `inspect`
alias_method :_to_s, :to_s

def inspect
ivars = instance_variables.map do |name|
"#{name}=#{instance_variable_get(name).inspect}"
end
_to_s.insert(-2, " #{ivars.join(", ")}")
end
end

delegate :to_s

# In case object is nil
Expand Down
8 changes: 0 additions & 8 deletions lib/draper/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ class Railtie < Rails::Railtie
end
end

initializer "draper.setup_active_model_serializers" do |app|
ActiveSupport.on_load :active_model_serializers do
if defined?(ActiveModel::ArraySerializerSupport)
Draper::CollectionDecorator.send :include, ActiveModel::ArraySerializerSupport
end
end
end

initializer "draper.minitest-rails_integration" do |app|
ActiveSupport.on_load :minitest do
require "draper/test/minitest_integration"
Expand Down
17 changes: 2 additions & 15 deletions lib/draper/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
require 'rake/testtask'

test_task = if Rails.version.to_f < 3.2
require 'rails/test_unit/railtie'
Rake::TestTask
else
require 'rails/test_unit/sub_test_task'
Rails::SubTestTask
end
require 'rails/test_unit/railtie'

namespace :test do
test_task.new(:decorators => "test:prepare") do |t|
Rake::TestTask.new(:decorators => "test:prepare") do |t|
t.libs << "test"
t.pattern = "test/decorators/**/*_test.rb"
end
end

if Rails.version.to_f < 4.2 && Rake::Task.task_defined?('test:run')
Rake::Task['test:run'].enhance do
Rake::Task['test:decorators'].invoke
end
end
6 changes: 1 addition & 5 deletions lib/draper/test/rspec_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ module DecoratorExampleGroup
end

RSpec.configure do |config|
if RSpec::Core::Version::STRING.starts_with?("3")
config.include DecoratorExampleGroup, file_path: %r{spec/decorators}, type: :decorator
else
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
end
config.include DecoratorExampleGroup, file_path: %r{spec/decorators}, type: :decorator

[:decorator, :controller, :mailer].each do |type|
config.before(:each, type: type) { Draper::ViewContext.clear! }
Expand Down
12 changes: 4 additions & 8 deletions lib/draper/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ module Behavior
end
end

if defined?(ActionController::TestCase)
class ActionController::TestCase
include Draper::TestCase::ViewContextTeardown
end
if defined? ActionController::TestCase
ActionController::TestCase.include Draper::TestCase::ViewContextTeardown
end

if defined?(ActionMailer::TestCase)
class ActionMailer::TestCase
include Draper::TestCase::ViewContextTeardown
end
if defined? ActionMailer::TestCase
ActionMailer::TestCase.include Draper::TestCase::ViewContextTeardown
end
2 changes: 1 addition & 1 deletion lib/draper/view_context/build_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def call

def controller
(Draper::ViewContext.controller || ApplicationController.new).tap do |controller|
controller.request ||= ActionController::TestRequest.new if defined?(ActionController::TestRequest)
controller.request ||= ActionController::TestRequest.create
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/draper/decoratable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,17 @@ 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 scoping_method => scoped
Product.stub all: 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 scoping_method => []
Product.stub all: []

Product.decorator_class.should_receive(:decorate_collection).with([], options)
Product.decorate(options)
Expand Down
14 changes: 8 additions & 6 deletions spec/dummy/app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
<dt>Helpers from the controller:</dt>
<dd id="goodnight_moon"><%= post.goodnight_moon %></dd>

<dt>Path with decorator:</dt>
<dd id="path_with_decorator"><%= post_path(post) %></dd>
<% unless defined? mailer %>
<dt>Path with decorator:</dt>
<dd id="path_with_decorator"><%= post_url(post) %></dd>

<dt>Path with model:</dt>
<dd id="path_with_model"><%= post.path_with_model %></dd>
<dt>Path with model:</dt>
<dd id="path_with_model"><%= post.path_with_model %></dd>

<dt>Path with id:</dt>
<dd id="path_with_id"><%= post.path_with_id %></dd>
<dt>Path with id:</dt>
<dd id="path_with_id"><%= post.path_with_id %></dd>
<% end %>

<dt>URL with decorator:</dt>
<dd id="url_with_decorator"><%= post_url(post) %></dd>
Expand Down
Loading

0 comments on commit b2f9f08

Please sign in to comment.