Skip to content
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

Switch to Module#prepend in ActiveRecord::Relation patch #1

Merged
merged 1 commit into from Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/standby/active_record/relation.rb
@@ -1,19 +1,18 @@
module ExecQueriesWithStandbyTarget
# Supports queries like User.on_standby.to_a
def exec_queries
if standby_target
Standby.on_standby(standby_target) { super }
else
super
end
end
end

module ActiveRecord
class Relation
attr_accessor :standby_target

# Supports queries like User.on_standby.to_a
alias_method :exec_queries_without_standby, :exec_queries

def exec_queries
if standby_target
Standby.on_standby(standby_target) { exec_queries_without_standby }
else
exec_queries_without_standby
end
end


# Supports queries like User.on_standby.count
alias_method :calculate_without_standby, :calculate

Expand All @@ -26,3 +25,5 @@ def calculate(*args)
end
end
end

ActiveRecord::Relation.prepend(ExecQueriesWithStandbyTarget)
6 changes: 6 additions & 0 deletions spec/slavery_spec.rb
Expand Up @@ -104,4 +104,10 @@ class ActiveRecord::Relation
expect(User.on_standby(:two).where(nil).to_a.size).to be 0
expect(User.on_standby.where(nil).to_a.size).to be 1
end

it 'does not interfere with setting inverses' do
user = User.first
user.update(name: 'a different name')
expect(user.items.first.user.name).to eq('a different name')
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -15,10 +15,11 @@
# Prepare databases
class User < ActiveRecord::Base
has_many :items
attr_accessor :name
end

class Item < ActiveRecord::Base
belongs_to :user
belongs_to :user, inverse_of: :items
end

class Seeder
Expand Down
1 change: 1 addition & 0 deletions standby.gemspec
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^exe/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 2.0'

gem.add_runtime_dependency 'activerecord', '>= 3.0.0'

Expand Down