Skip to content

Commit

Permalink
Merge bd4ffd1 into 95e4858
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukiARIA committed Nov 28, 2017
2 parents 95e4858 + bd4ffd1 commit d409b5e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/active_record/sharding/facade.rb
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
1 change: 1 addition & 0 deletions lib/activerecord-sharding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "active_record/sharding/sequencer"
require "active_record/sharding/sequencer_repository"
require "active_record/sharding/sequencer_config"
require "active_record/sharding/facade"

module ActiveRecord
module Sharding
Expand Down
47 changes: 47 additions & 0 deletions spec/active_record/sharding/facade_spec.rb
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

0 comments on commit d409b5e

Please sign in to comment.