Skip to content

Relations: #decorate queries DB every time #944

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

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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Draper Changelog

## Unreleased

### Fixes
* Fix decoration of AR relations [#944](https://github.com/drapergem/draper/pull/944)

## 4.0.4 - 2025-01-28

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/draper/decoratable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Decoratable
extend ActiveSupport::Concern
include Draper::Decoratable::Equality

autoload :CollectionProxy, 'draper/decoratable/collection_proxy'
autoload :Relation, 'draper/decoratable/relation'

included do
prepend Draper::Compatibility::Broadcastable if defined? Turbo::Broadcastable
Expand Down
15 changes: 0 additions & 15 deletions lib/draper/decoratable/collection_proxy.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/draper/decoratable/relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Draper
module Decoratable
module Relation
# Decorates a relation of objects. Used at the end of a scope chain.
#
# @example
# Product.popular.decorate
# @param [Hash] options
# see {Decorator.decorate_collection}.
def decorate(options = {})
decorator_class.decorate_collection(self, options.reverse_merge(with: nil))
end
end
end
end
2 changes: 1 addition & 1 deletion lib/draper/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Railtie < Rails::Railtie
ActiveSupport.on_load :active_record do
include Draper::Decoratable

ActiveRecord::Associations::CollectionProxy.include Draper::Decoratable::CollectionProxy
ActiveRecord::Relation.include Draper::Decoratable::Relation
end

ActiveSupport.on_load :mongoid do
Expand Down
34 changes: 33 additions & 1 deletion spec/dummy/spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@
end
end if defined? Turbo::Broadcastable

describe 'relations' do
context 'when decorated' do
subject { relation.decorate }

let(:relation) { described_class.where('1=1') }
let(:persisted) { relation.create! [{}] * rand(0..2) }

before { persisted } # should exist

it 'returns a decorated collection' do
is_expected.to match_array persisted
is_expected.to be_all &:decorated?
end

it 'uses cached records' do
expect(relation).not_to be_loaded

relation.load

expect { subject.to_a }.to execute.exactly(0).queries
end

it 'caches records' do
expect(relation).not_to be_loaded

relation.decorate.to_a

expect { subject.to_a; relation.load }.to execute.exactly(0).queries
end
end
end

describe 'associations' do
context 'when decorated' do
subject { associated.decorate }
Expand All @@ -47,7 +79,7 @@
it 'caches records' do
expect(associated).not_to be_loaded

associated.decorate
associated.decorate.to_a

expect { subject.to_a; associated.load }.to execute.exactly(0).queries
end
Expand Down
Loading