-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
3 changed files
with
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "active_support/concern" | ||
|
||
module ActiveRecord | ||
module Sharding | ||
module Facade | ||
extend ActiveSupport::Concern | ||
|
||
include Model | ||
include Sequencer | ||
|
||
included do | ||
before_put do |attributes| | ||
attributes[:id] ||= next_sequence_id | ||
end | ||
|
||
before_save on: :create do | ||
self.id ||= self.class.next_sequence_id | ||
end | ||
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,47 @@ | ||
describe ActiveRecord::Sharding::Facade do | ||
let!(:model) do | ||
Class.new(ActiveRecord::Base) do | ||
def self.name | ||
"User" | ||
end | ||
|
||
include ActiveRecord::Sharding::Facade | ||
|
||
use_sharding :user, :modulo | ||
define_sharding_key :id | ||
use_sequencer :user | ||
end | ||
end | ||
|
||
describe "including modules" do | ||
it "includes ActiveRecord::Sharding::Model and ActiveRecord::Sharding::Sequencer" do | ||
expect(model).to be_include ActiveRecord::Sharding::Model | ||
expect(model).to be_include ActiveRecord::Sharding::Sequencer | ||
end | ||
end | ||
|
||
shared_examples_for "successfully" do | ||
it "inserts a record" do | ||
expect(alice.persisted?).to be true | ||
expect(alice.id).to eq model.current_sequence_id | ||
expect(alice.name).to eq "Alice" | ||
expect(alice.class.name).to match /User::ShardFor/ | ||
end | ||
end | ||
|
||
describe ".put!" do | ||
let(:alice) { model.put!(name: "Alice") } | ||
|
||
it_behaves_like "successfully" | ||
end | ||
|
||
describe ".new and #save" do | ||
let(:alice) do | ||
alice = model.shard_for(1).new(name: "Alice") | ||
alice.save | ||
alice | ||
end | ||
|
||
it_behaves_like "successfully" | ||
end | ||
end |