Skip to content

Commit

Permalink
Now we can remove a bunch of conditionals for legacy AR support
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Jun 28, 2019
1 parent cf6e4e1 commit 86ed83e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 65 deletions.
25 changes: 3 additions & 22 deletions lib/active_decorator/monkey/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ def target
end
end

# @see https://github.com/rails/rails/commit/03855e790de2224519f55382e3c32118be31eeff
if Rails.version.to_f < 4.1
module CollectionAssociation
private
def first_or_last(*)
ActiveDecorator::Decorator.instance.decorate_association(owner, super)
end
end
elsif Rails.version.to_f < 5.1
if Rails.version.to_f < 5.1
module CollectionAssociation
private
def first_nth_or_last(*)
Expand All @@ -29,19 +21,8 @@ def first_nth_or_last(*)
end

module CollectionProxy
if Rails.version.to_f >= 4.0
def take(*)
ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
end
else
# To propagate the decorated mark from association to relation
def scoped
ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
end

def find(*)
ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
end
def take(*)
ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
end

if Rails.version.to_f >= 5.1
Expand Down
4 changes: 1 addition & 3 deletions lib/active_decorator/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class Railtie < ::Rails::Railtie
ActiveRecord::Associations::CollectionAssociation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionAssociation
end

if Rails.version.to_f >= 4.0
ActiveRecord::AssociationRelation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::AssociationRelation
end
ActiveRecord::AssociationRelation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::AssociationRelation

ActiveRecord::Associations::CollectionProxy.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionProxy
ActiveRecord::Associations::CollectionAssociation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionAssociation
Expand Down
14 changes: 3 additions & 11 deletions lib/active_decorator/view_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ module Filter
extend ActiveSupport::Concern

included do
if Rails::VERSION::MAJOR >= 4
around_action do |controller, blk|
ActiveDecorator::ViewContext.run_with(controller.view_context) do
blk.call
end
end
else
around_filter do |controller, blk|
ActiveDecorator::ViewContext.run_with(controller.view_context) do
blk.call
end
around_action do |controller, blk|
ActiveDecorator::ViewContext.run_with(controller.view_context) do
blk.call
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/fake_app/app/views/authors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= @author.capitalized_name %>
<%= @author.books.first.upcased_title %>
<%= @author.books.last.upcased_title %>
<% if Rails.version.to_f >= 4.0 && p = @author.publishers.take %><%= p.upcased_name %><% end %>
<% if p = @author.publishers.take %><%= p.upcased_name %><% end %>
<% if Rails.version.to_f >= 5.1 && p = @author.publishers.second_to_last %><%= p.reversed_name %><% end %>
<% if p = @author.profile %><%= p.address %><% end %>
<% if h = @author.profile_history %><%= h.update_date %><% end %>
Expand Down
23 changes: 6 additions & 17 deletions test/fake_app/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,13 @@ class ApplicationController < ActionController::Base
end
class AuthorsController < ApplicationController
def index
if Author.respond_to?(:scoped)
# ActiveRecord 3.x
if params[:variable_type] == 'array'
@authors = Author.all
elsif params[:variable_type] == 'proxy'
@authors = RelationProxy.new(Author.scoped)
else
@authors = Author.scoped
end
# ActiveRecord 4.x
if params[:variable_type] == 'array'
@authors = Author.all.to_a
elsif params[:variable_type] == 'proxy'
@authors = RelationProxy.new(Author.all)
else
# ActiveRecord 4.x
if params[:variable_type] == 'array'
@authors = Author.all.to_a
elsif params[:variable_type] == 'proxy'
@authors = RelationProxy.new(Author.all)
else
@authors = Author.all
end
@authors = Author.all
end
end

Expand Down
4 changes: 1 addition & 3 deletions test/features/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class AssociationIntegrationTest < ActionDispatch::IntegrationTest
visit "/authors/#{@matz.id}"
assert page.has_content? 'the world of code'.upcase
assert page.has_content? 'the ruby programming language'.upcase
if Rails.version.to_f >= 4.0
assert page.has_content? 'nikkei linux'.upcase
end
assert page.has_content? 'nikkei linux'.upcase
if Rails.version.to_f >= 5.1
assert page.has_content? 'nikkei linux'.reverse
end
Expand Down
12 changes: 4 additions & 8 deletions test/models/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class AssociationTest < Test::Unit::TestCase
assert @books.find(@id).is_a? ActiveDecorator::Decorated
end

if ActiveRecord::VERSION::MAJOR >= 4
test 'take' do
assert @books.take.is_a? ActiveDecorator::Decorated
end
test 'take' do
assert @books.take.is_a? ActiveDecorator::Decorated
end

sub_test_case 'when method chained' do
Expand All @@ -70,10 +68,8 @@ class AssociationTest < Test::Unit::TestCase
assert @books.find(@id).is_a? ActiveDecorator::Decorated
end

if ActiveRecord::VERSION::MAJOR >= 4
test 'take' do
assert @books.take.is_a? ActiveDecorator::Decorated
end
test 'take' do
assert @books.take.is_a? ActiveDecorator::Decorated
end
end
end

0 comments on commit 86ed83e

Please sign in to comment.