diff options
Diffstat (limited to 'app/models/wiki.rb')
-rw-r--r-- | app/models/wiki.rb | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/app/models/wiki.rb b/app/models/wiki.rb index 9462f7401c4..e329a094319 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -4,6 +4,7 @@ class Wiki extend ::Gitlab::Utils::Override include HasRepository include Gitlab::Utils::StrongMemoize + include GlobalID::Identification MARKUPS = { # rubocop:disable Style/MultilineIfModifier 'Markdown' => :markdown, @@ -28,14 +29,46 @@ class Wiki # an operation fails. attr_reader :error_message - def self.for_container(container, user = nil) - "#{container.class.name}Wiki".constantize.new(container, user) + # Support run_after_commit callbacks, since we don't have a DB record + # we delegate to the container. + delegate :run_after_commit, to: :container + + class << self + attr_accessor :container_class + + def for_container(container, user = nil) + "#{container.class.name}Wiki".constantize.new(container, user) + end + + # This is needed to support repository lookup through Gitlab::GlRepository::Identifier + def find_by_id(container_id) + container_class.find_by_id(container_id)&.wiki + end end def initialize(container, user = nil) + raise ArgumentError, "user must be a User, got #{user.class}" if user && !user.is_a?(User) + @container = container @user = user - raise ArgumentError, "user must be a User, got #{user.class}" if user && !user.is_a?(User) + end + + def ==(other) + other.is_a?(self.class) && container == other.container + end + + # This is needed in: + # - Storage::Hashed + # - Gitlab::GlRepository::RepoType#identifier_for_container + # + # We also need an `#id` to support `build_stubbed` in tests, where the + # value doesn't matter. + # + # NOTE: Wikis don't have a DB record, so this ID can be the same + # for two wikis in different containers and should not be expected to + # be unique. Use `to_global_id` instead if you need a unique ID. + def id + container.id end def path @@ -103,10 +136,10 @@ class Wiki limited = pages.size > limit pages = pages.first(limit) if limited - [WikiPage.group_by_directory(pages), limited] + [WikiDirectory.group_pages(pages), limited] end - # Finds a page within the repository based on a tile + # Finds a page within the repository based on a title # or slug. # # title - The human readable or parameterized title of @@ -183,7 +216,7 @@ class Wiki override :repository def repository - @repository ||= Gitlab::GlRepository::WIKI.repository_for(container) + @repository ||= Gitlab::GlRepository::WIKI.repository_for(self) end def repository_storage @@ -198,7 +231,6 @@ class Wiki def full_path container.full_path + '.wiki' end - alias_method :id, :full_path # @deprecated use full_path when you need it for an URL route or disk_path when you want to point to the filesystem alias_method :path_with_namespace, :full_path |