Commit ec23ba88 authored by Adam Hegyi's avatar Adam Hegyi

Merge branch 'fix-migration-mentionable-concerns' into 'master'

Fix module namespacing for mentionable concerns

See merge request gitlab-org/gitlab!26438
parents 665cc055 d6a5f930
...@@ -4,95 +4,97 @@ module Gitlab ...@@ -4,95 +4,97 @@ module Gitlab
module BackgroundMigration module BackgroundMigration
module UserMentions module UserMentions
module Models module Models
# == IsolatedMentionable concern module Concerns
# # == IsolatedMentionable concern
# Shortcutted for isolation version of Mentionable to be used in mentions migrations #
# # Shortcutted for isolation version of Mentionable to be used in mentions migrations
module IsolatedMentionable #
extend ::ActiveSupport::Concern module IsolatedMentionable
extend ::ActiveSupport::Concern
class_methods do
# Indicate which attributes of the Mentionable to search for GFM references. class_methods do
def attr_mentionable(attr, options = {}) # Indicate which attributes of the Mentionable to search for GFM references.
attr = attr.to_s def attr_mentionable(attr, options = {})
mentionable_attrs << [attr, options] attr = attr.to_s
mentionable_attrs << [attr, options]
end
end end
end
included do included do
# Accessor for attributes marked mentionable. # Accessor for attributes marked mentionable.
cattr_accessor :mentionable_attrs, instance_accessor: false do cattr_accessor :mentionable_attrs, instance_accessor: false do
[] []
end end
if self < Participable if self < Participable
participant -> (user, ext) { all_references(user, extractor: ext) } participant -> (user, ext) { all_references(user, extractor: ext) }
end
end end
end
def all_references(current_user = nil, extractor: nil) def all_references(current_user = nil, extractor: nil)
# Use custom extractor if it's passed in the function parameters. # Use custom extractor if it's passed in the function parameters.
if extractor if extractor
extractors[current_user] = extractor extractors[current_user] = extractor
else else
extractor = extractors[current_user] ||= ::Gitlab::ReferenceExtractor.new(project, current_user) extractor = extractors[current_user] ||= ::Gitlab::ReferenceExtractor.new(project, current_user)
extractor.reset_memoized_values extractor.reset_memoized_values
end end
self.class.mentionable_attrs.each do |attr, options| self.class.mentionable_attrs.each do |attr, options|
text = __send__(attr) # rubocop:disable GitlabSecurity/PublicSend text = __send__(attr) # rubocop:disable GitlabSecurity/PublicSend
options = options.merge( options = options.merge(
cache_key: [self, attr], cache_key: [self, attr],
author: author, author: author,
skip_project_check: skip_project_check? skip_project_check: skip_project_check?
).merge(mentionable_params) ).merge(mentionable_params)
cached_html = self.try(:updated_cached_html_for, attr.to_sym) cached_html = self.try(:updated_cached_html_for, attr.to_sym)
options[:rendered] = cached_html if cached_html options[:rendered] = cached_html if cached_html
extractor.analyze(text, options) extractor.analyze(text, options)
end end
extractor extractor
end end
def extractors def extractors
@extractors ||= {} @extractors ||= {}
end end
def skip_project_check? def skip_project_check?
false false
end end
def build_mention_values(resource_foreign_key) def build_mention_values(resource_foreign_key)
refs = all_references(author) refs = all_references(author)
mentioned_users_ids = array_to_sql(refs.mentioned_users.pluck(:id)) mentioned_users_ids = array_to_sql(refs.mentioned_users.pluck(:id))
mentioned_projects_ids = array_to_sql(refs.mentioned_projects.pluck(:id)) mentioned_projects_ids = array_to_sql(refs.mentioned_projects.pluck(:id))
mentioned_groups_ids = array_to_sql(refs.mentioned_groups.pluck(:id)) mentioned_groups_ids = array_to_sql(refs.mentioned_groups.pluck(:id))
return if mentioned_users_ids.blank? && mentioned_projects_ids.blank? && mentioned_groups_ids.blank? return if mentioned_users_ids.blank? && mentioned_projects_ids.blank? && mentioned_groups_ids.blank?
{ {
"#{resource_foreign_key}": user_mention_resource_id, "#{resource_foreign_key}": user_mention_resource_id,
note_id: user_mention_note_id, note_id: user_mention_note_id,
mentioned_users_ids: mentioned_users_ids, mentioned_users_ids: mentioned_users_ids,
mentioned_projects_ids: mentioned_projects_ids, mentioned_projects_ids: mentioned_projects_ids,
mentioned_groups_ids: mentioned_groups_ids mentioned_groups_ids: mentioned_groups_ids
} }
end end
def array_to_sql(ids_array) def array_to_sql(ids_array)
return unless ids_array.present? return unless ids_array.present?
'{' + ids_array.join(", ") + '}' '{' + ids_array.join(", ") + '}'
end end
private private
def mentionable_params def mentionable_params
{} {}
end
end end
end end
end end
......
...@@ -4,17 +4,19 @@ module Gitlab ...@@ -4,17 +4,19 @@ module Gitlab
module BackgroundMigration module BackgroundMigration
module UserMentions module UserMentions
module Models module Models
# Extract common no_quote_columns method used in determining the columns that do not need module Concerns
# to be quoted for corresponding models # Extract common no_quote_columns method used in determining the columns that do not need
module MentionableMigrationMethods # to be quoted for corresponding models
extend ::ActiveSupport::Concern module MentionableMigrationMethods
extend ::ActiveSupport::Concern
class_methods do class_methods do
def no_quote_columns def no_quote_columns
[ [
:note_id, :note_id,
user_mention_model.resource_foreign_key user_mention_model.resource_foreign_key
] ]
end
end end
end end
end end
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
module Models module Models
module DesignManagement module DesignManagement
class Design < ActiveRecord::Base class Design < ActiveRecord::Base
include MentionableMigrationMethods include Concerns::MentionableMigrationMethods
def self.user_mention_model def self.user_mention_model
Gitlab::BackgroundMigration::UserMentions::Models::DesignUserMention Gitlab::BackgroundMigration::UserMentions::Models::DesignUserMention
......
...@@ -6,9 +6,9 @@ module Gitlab ...@@ -6,9 +6,9 @@ module Gitlab
module UserMentions module UserMentions
module Models module Models
class Epic < ActiveRecord::Base class Epic < ActiveRecord::Base
include IsolatedMentionable include Concerns::IsolatedMentionable
include Concerns::MentionableMigrationMethods
include CacheMarkdownField include CacheMarkdownField
include MentionableMigrationMethods
attr_mentionable :title, pipeline: :single_line attr_mentionable :title, pipeline: :single_line
attr_mentionable :description attr_mentionable :description
......
...@@ -6,7 +6,7 @@ module Gitlab ...@@ -6,7 +6,7 @@ module Gitlab
module UserMentions module UserMentions
module Models module Models
class Note < ActiveRecord::Base class Note < ActiveRecord::Base
include IsolatedMentionable include Concerns::IsolatedMentionable
include CacheMarkdownField include CacheMarkdownField
self.table_name = 'notes' self.table_name = 'notes'
......
...@@ -6,9 +6,9 @@ module Gitlab ...@@ -6,9 +6,9 @@ module Gitlab
module UserMentions module UserMentions
module Models module Models
class Snippet < ActiveRecord::Base class Snippet < ActiveRecord::Base
include IsolatedMentionable include Concerns::IsolatedMentionable
include Concerns::MentionableMigrationMethods
include CacheMarkdownField include CacheMarkdownField
include MentionableMigrationMethods
attr_mentionable :title, pipeline: :single_line attr_mentionable :title, pipeline: :single_line
attr_mentionable :description attr_mentionable :description
......
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