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

Assign next_sequence_id automatically before put/save #28

Conversation

YuukiARIA
Copy link
Contributor

When using activerecord-sharding, it is necessary to add following code snippet to our active record model classes.

  before_put do |attributes|
    attributes[:id] ||= next_sequence_id
  end

  # this part is not written on README.md, but is needed for Model.shard_for(...).new(...).save.
  before_save on: :create do
    self.id ||= self.class.next_sequence_id
  end

before_put is from ActiveRecord::Sharding::Model, next_sequence_id is from ActiveRecord::Sharding::Sequencer.

Now, when I use activerecord-sharding, I always include both extensions into my models.
So, I propose to define a unified module, ActiveRecord::Sharding::Facade.

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/
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

@@ -0,0 +1,47 @@
describe ActiveRecord::Sharding::Facade do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockLength: Block has too many lines. [37/25]
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

@@ -0,0 +1,22 @@
require "active_support/concern"
Copy link
Collaborator

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.

@hirocaster
Copy link
Owner

Thank you propose.

This module use before_save on: :create for user's implements hooks by Rails.

My module policy is not hacked Rails.
but, In many cases, I think that write such a code.

so, Exported gem.
https://github.com/hirocaster/activerecord-sharding-facade

It gem user is must consider this when implement hooks for models.

@hirocaster hirocaster closed this Nov 28, 2017
@YuukiARIA
Copy link
Contributor Author

I understand and thank you for your good idea!

@YuukiARIA YuukiARIA deleted the assign-next-sequence-id-automatically branch November 28, 2017 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants