Skip to content

Commit 961b93a

Browse files
authored
Merge pull request #193 from kbrock/nitpicks
Nitpicks
2 parents 5a12ae4 + c35cc65 commit 961b93a

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

lib/active_record/virtual_attributes/virtual_fields.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def grouped_records
191191
# rubocop:enable Lint/AmbiguousOperatorPrecedence
192192

193193
# branched.rb 7.0
194+
# rubocop:disable Style/MultilineBlockChain
194195
def preloaders_for_reflection(reflection, reflection_records)
195196
reflection_records.group_by do |record|
196197
# begin virtual_attributes changes
@@ -212,6 +213,7 @@ def preloaders_for_reflection(reflection, reflection_records)
212213
end
213214
end
214215
end)
216+
# rubocop:enable Style/MultilineBlockChain
215217
end
216218
end
217219
end

lib/active_record/virtual_attributes/virtual_reflections.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def virtual_has_one(name, uses: nil, **options)
1414
add_virtual_reflection(reflection, name, uses)
1515
end
1616

17-
def virtual_has_many(name, uses: nil, source: nil, through: nil, **options)
17+
def virtual_has_many(name, uses: nil, source: name, through: nil, **options)
1818
define_method(:"#{name.to_s.singularize}_ids") do
1919
records = send(name)
2020
records.respond_to?(:ids) ? records.ids : records.collect(&:id)
2121
end
22-
define_delegate(name, source || name, :to => through, :allow_nil => true, :default => []) if through
22+
define_delegate(name, source, :to => through, :allow_nil => true, :default => []) if through
2323
reflection = ActiveRecord::Associations::Builder::HasMany.build(self, name, nil, options)
2424
add_virtual_reflection(reflection, name, uses)
2525
end

spec/db/models.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def book_with_most_bookmarks
116116
virtual_has_many :famous_co_authors, :uses => [:book_with_most_bookmarks, {:books => :co_authors}]
117117

118118
def self.create_with_books(count)
119-
create!(:name => "foo").tap { |author| author.create_books(count) }
119+
create!(:name => "foo", :blurb => "blah blah blah").tap { |author| author.create_books(count) }
120120
end
121121

122122
def create_books(count, create_attrs = {})
@@ -147,6 +147,9 @@ class Book < VirtualTotalTestBase
147147
virtual_delegate :name, :to => :author, :prefix => true, :type => :string
148148
# this tests delegates to named child attribute
149149
virtual_delegate :author_name2, :to => "author.name", :type => :string
150+
# delegate without a prefix
151+
virtual_delegate :blurb, :to => :author, :type => :string
152+
150153
# delegate to a polymorphic
151154
virtual_delegate :description, :to => :current_photo, :prefix => true, :type => :string, :allow_nil => true
152155

spec/db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
t.integer "teacher_id", :index => true
88
t.string "name"
99
t.string "nickname"
10+
t.string "blurb"
1011
end
1112

1213
create_table "books", :force => true do |t|

spec/spec_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
end
66
end
77

8-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
9-
8+
$LOAD_PATH.unshift File.expand_path("../../lib", __dir__)
109
require "bundler/setup"
1110
require "active_record/virtual_attributes"
1211
require "active_record/virtual_attributes/rspec"

spec/virtual_includes_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
let(:author_name) { "foo" }
77
let(:book_name) { "bar" }
8+
let(:blurb_text) { "blah blah blah" }
9+
810
# NOTE: each of the 1 authors has an array of books. so this value is [[Book, Book]]
911
let(:named_books) { [Book.where.not(:name => nil).order(:id).load] }
1012

@@ -41,8 +43,8 @@
4143
end
4244

4345
it "preloads virtual_attribute (:uses => :author, :uses => :author)" do
44-
expect(Book.includes(:author_name, :author_name2)).to preload_values(:author_name, author_name)
45-
expect(Book.includes(:author_name2 => {})).to preload_values(:author_name, author_name)
46+
expect(Book.includes(:author_name, :blurb)).to preload_values(:author_name, author_name)
47+
expect(Book.includes(:author_name => {})).to preload_values(:blurb, blurb_text)
4648
end
4749

4850
it "preloads virtual_attribute (:uses => :upper_author_name) (:uses => :author_name)" do
@@ -526,7 +528,7 @@
526528
end
527529

528530
it "handles hash form of delegates" do
529-
expect(Book.replace_virtual_fields([{:author_name => {}}, {:author_name2 => {}}])).to eq([:author, :author])
531+
expect(Book.replace_virtual_fields([{:author_name => {}}, {:blurb => {}}])).to eq([:author, :author])
530532
end
531533

532534
it "handles non-'includes' virtual_attributes" do

0 commit comments

Comments
 (0)