summaryrefslogtreecommitdiff
path: root/spec/factories/wiki_pages.rb
blob: 8eb7a12a9282d45e70786cac5a00ddb7397d2323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

require 'ostruct'

FactoryBot.define do
  factory :wiki_page do
    transient do
      title { generate(:wiki_page_title) }
      content { 'Content for wiki page' }
      format { 'markdown' }
      project { create(:project) }
      attrs do
        {
          title: title,
          content: content,
          format: format
        }
      end
    end

    page { OpenStruct.new(url_path: 'some-name') }
    wiki { build(:project_wiki, project: project) }

    initialize_with { new(wiki, page) }

    before(:create) do |page, evaluator|
      page.attributes = evaluator.attrs
    end

    to_create do |page|
      page.create
    end

    trait :with_real_page do
      project { create(:project, :repository) }

      page do
        wiki.create_page(title, content)
        page_title, page_dir = wiki.page_title_and_dir(title)
        wiki.wiki.page(title: page_title, dir: page_dir, version: nil)
      end
    end
  end

  factory :wiki_page_meta, class: 'WikiPage::Meta' do
    title { generate(:wiki_page_title) }
    project { create(:project) }

    trait :for_wiki_page do
      transient do
        wiki_page { create(:wiki_page, project: project) }
      end

      project { @overrides[:wiki_page]&.project || create(:project) }
      title { wiki_page.title }

      initialize_with do
        raise 'Metadata only available for valid pages' unless wiki_page.valid?

        WikiPage::Meta.find_or_create(wiki_page.slug, wiki_page)
      end
    end
  end

  factory :wiki_page_slug, class: 'WikiPage::Slug' do
    wiki_page_meta { create(:wiki_page_meta) }
    slug { generate(:sluggified_title) }
    canonical { false }

    trait :canonical do
      canonical { true }
    end
  end

  sequence(:wiki_page_title) { |n| "Page #{n}" }
  sequence(:sluggified_title) { |n| "slug-#{n}" }
end