-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b8b0f3
commit b67f807
Showing
4 changed files
with
121 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Slavable | ||
def switch(*method_names) | ||
options = method_names.pop | ||
|
||
unless options.is_a?(Hash) | ||
raise ArgumentError, 'Unable to detect "to" option, usage: "switch :method, :other, ..., to: :connection_name"' | ||
end | ||
|
||
method_names.each do |method| | ||
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 | ||
with_name = "#{aliased_method}_with_connection#{punctuation}" | ||
without_name = "#{aliased_method}_without_connection#{punctuation}" | ||
connection = options.with_indifferent_access.fetch(:to) | ||
|
||
class_eval <<-eoruby, __FILE__, __LINE__ + 1 | ||
def #{with_name}(*args) | ||
::ActiveRecord::Base.within(:#{connection}) { #{without_name}(*args) } | ||
end | ||
eoruby | ||
|
||
alias_method without_name, method | ||
alias_method method, with_name | ||
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,52 @@ | ||
require 'spec_helper' | ||
|
||
class Some | ||
extend Slavable | ||
|
||
def some_method | ||
Bar.create | ||
end | ||
|
||
def method_wtih_args(name) | ||
Foo.create(name: name) | ||
end | ||
|
||
def self.class_method(name) | ||
Foo.create(name: name) | ||
end | ||
|
||
switch :some_method, :method_wtih_args, to: :other | ||
|
||
class << self | ||
extend Slavable | ||
|
||
switch :class_method, to: :other | ||
end | ||
end | ||
|
||
describe Slaver do | ||
it 'switches some_method to other connection' do | ||
s = Some.new | ||
|
||
s.some_method | ||
|
||
expect(Bar.count).to eq 0 | ||
expect(Bar.on(:other).count).to eq 1 | ||
end | ||
|
||
it 'switches method_with_args to other connection' do | ||
s = Some.new | ||
|
||
s.method_wtih_args('test') | ||
|
||
expect(Foo.where(name: 'test').count).to eq 0 | ||
expect(Foo.on(:other).where(name: 'test').count).to eq 1 | ||
end | ||
|
||
it 'switches class_method to other connection' do | ||
Some.class_method('test') | ||
|
||
expect(Foo.where(name: 'test').count).to eq 0 | ||
expect(Foo.on(:other).where(name: 'test').count).to eq 1 | ||
end | ||
end |