summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2018-03-14 13:42:03 +0100
committerAndreas Brandl <abrandl@gitlab.com>2018-03-16 13:35:27 +0100
commit539bdf73be4a84b1f542801a28f016808d604fe5 (patch)
tree7c7a06625bdfd49a177709be5db0e155b3ef3ced
parentd9a953c847339c3d906be4540bdc2a3c44f13fca (diff)
downloadgitlab-ce-539bdf73be4a84b1f542801a28f016808d604fe5.tar.gz
Address review comments.
-rw-r--r--app/models/concerns/atomic_internal_id.rb27
-rw-r--r--app/models/internal_id.rb5
2 files changed, 15 insertions, 17 deletions
diff --git a/app/models/concerns/atomic_internal_id.rb b/app/models/concerns/atomic_internal_id.rb
index bba71eee8bd..6f2d60a7ee9 100644
--- a/app/models/concerns/atomic_internal_id.rb
+++ b/app/models/concerns/atomic_internal_id.rb
@@ -24,24 +24,21 @@
module AtomicInternalId
extend ActiveSupport::Concern
- included do
- class << self
- def has_internal_id(on, scope:, usage: nil, init:) # rubocop:disable Naming/PredicateName
- before_validation(on: :create) do
- 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
- 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
+ module ClassMethods
+ def has_internal_id(on, scope:, usage: nil, init:) # rubocop:disable Naming/PredicateName
+ before_validation(on: :create) do
+ 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
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
+
+ validates on, presence: true, numericality: true
end
end
diff --git a/app/models/internal_id.rb b/app/models/internal_id.rb
index 6419cdc5b67..f3630ed1ac5 100644
--- a/app/models/internal_id.rb
+++ b/app/models/internal_id.rb
@@ -67,16 +67,17 @@ class InternalId < ActiveRecord::Base
# 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
attr_reader :subject, :scope, :init, :scope_attrs, :usage
+
def initialize(subject, scope, usage, init)
@subject = subject
@scope = scope
@init = init
@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)
- 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