Commit 539bdf73 authored by Andreas Brandl's avatar Andreas Brandl

Address review comments.

parent d9a953c8
...@@ -24,24 +24,21 @@ ...@@ -24,24 +24,21 @@
module AtomicInternalId module AtomicInternalId
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do module ClassMethods
class << self def has_internal_id(on, scope:, usage: nil, init:) # rubocop:disable Naming/PredicateName
def has_internal_id(on, scope:, usage: nil, init:) # rubocop:disable Naming/PredicateName before_validation(on: :create) do
before_validation(on: :create) do if self.public_send(on).blank? # rubocop:disable GitlabSecurity/PublicSend
if self.public_send(on).blank? # rubocop:disable GitlabSecurity/PublicSend scope_attrs = [scope].flatten.compact.each_with_object({}) do |e, h|
h[e] = self.public_send(e) # rubocop:disable GitlabSecurity/PublicSend
scope_attrs = [scope].flatten.compact.each_with_object({}) do |e, h|
h[e] = self.public_send(e) # rubocop:disable GitlabSecurity/PublicSend
end
usage = (usage || self.class.name.tableize).to_sym
new_iid = InternalId.generate_next(self, scope_attrs, usage, init)
self.public_send("#{on}=", new_iid) # rubocop:disable GitlabSecurity/PublicSend
end end
end
validates on, presence: true, numericality: true usage = (usage || self.class.table_name).to_sym
new_iid = InternalId.generate_next(self, scope_attrs, usage, init)
self.public_send("#{on}=", new_iid) # rubocop:disable GitlabSecurity/PublicSend
end
end end
validates on, presence: true, numericality: true
end end
end end
......
...@@ -67,16 +67,17 @@ class InternalId < ActiveRecord::Base ...@@ -67,16 +67,17 @@ class InternalId < ActiveRecord::Base
# usage: Symbol to define the usage of the internal id, see InternalId.usages # usage: Symbol to define the usage of the internal id, see InternalId.usages
# init: Block that gets called to initialize InternalId record if not present # init: Block that gets called to initialize InternalId record if not present
attr_reader :subject, :scope, :init, :scope_attrs, :usage attr_reader :subject, :scope, :init, :scope_attrs, :usage
def initialize(subject, scope, usage, init) def initialize(subject, scope, usage, init)
@subject = subject @subject = subject
@scope = scope @scope = scope
@init = init @init = init
@usage = usage @usage = usage
raise 'scope is not well-defined, need at least one column for scope (given: 0)' if scope.empty? raise ArgumentError, 'scope is not well-defined, need at least one column for scope (given: 0)' if scope.empty?
unless InternalId.usages.keys.include?(usage.to_s) unless InternalId.usages.keys.include?(usage.to_s)
raise "Usage '#{usage}' is unknown. Supported values are #{InternalId.usages.keys} from InternalId.usages" raise ArgumentError, "Usage '#{usage}' is unknown. Supported values are #{InternalId.usages.keys} from InternalId.usages"
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment