-
Notifications
You must be signed in to change notification settings - Fork 7
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
Assign next_sequence_id automatically before put/save #28
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
describe ActiveRecord::Sharding::Facade do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/BlockLength: Block has too many lines. [37/25] |
||
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/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/AmbiguousRegexpLiteral: Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the / if it should be a division. |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.