-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Associations:
#decorate
queries DB every time
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target` (be it already loaded or not) and loads the association again every time it's called. That's why unsaved records get missing from the decorated collection. Resolves #646, #706, #827.
- Loading branch information
1 parent
7093384
commit c7e37c1
Showing
12 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Draper | ||
module Decoratable | ||
module CollectionProxy | ||
# Decorates a collection of objects. Used at the end of a scope chain. | ||
# | ||
# @example | ||
# company.products.popular.decorate | ||
# @param [Hash] options | ||
# see {Decorator.decorate_collection}. | ||
def decorate(options = {}) | ||
decorator_class.decorate_collection(load_target, options.reverse_merge(with: nil)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CommentDecorator < Draper::Decorator | ||
delegate_all | ||
|
||
# Define presentation-specific methods here. Helpers are accessed through | ||
# `helpers` (aka `h`). You can override attributes, for example: | ||
# | ||
# def created_at | ||
# helpers.content_tag :span, class: 'time' do | ||
# object.created_at.strftime("%a %m/%d/%y") | ||
# end | ||
# end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :post | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateComments < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :comments do |t| | ||
t.references :post, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
# encoding: UTF-8 | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# This file is the source Rails uses to define your schema when running `bin/rails | ||
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to | ||
# be faster and is potentially less error prone than running all of your | ||
# migrations from scratch. Old migrations may fail to apply correctly if those | ||
# migrations use external dependencies or application code. | ||
# | ||
# It's strongly recommended to check this file into your version control system. | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 20121019115657) do | ||
ActiveRecord::Schema.define(version: 2024_09_07_041839) do | ||
|
||
create_table "posts", force: true do |t| | ||
create_table "comments", force: :cascade do |t| | ||
t.integer "post_id" | ||
t.datetime "created_at", precision: 6, null: false | ||
t.datetime "updated_at", precision: 6, null: false | ||
t.index ["post_id"], name: "index_comments_on_post_id" | ||
end | ||
|
||
create_table "posts", force: :cascade do |t| | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
end | ||
|
||
add_foreign_key "comments", "posts" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
ENV['RAILS_ENV'] ||= 'test' | ||
require File.expand_path('../../config/environment', __FILE__) | ||
require 'rspec/rails' | ||
require 'rspec/activerecord/expectations' | ||
|
||
RSpec.configure do |config| | ||
config.expect_with(:rspec) {|c| c.syntax = :expect} | ||
config.order = :random | ||
|
||
include RSpec::ActiveRecord::Expectations | ||
end |