Class: Thredded::SeedDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/thredded/seed_database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messageboard ⇒ Object (readonly)

Returns the value of attribute messageboard.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def messageboard
  @messageboard
end

#posts ⇒ Object (readonly)

Returns the value of attribute posts.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def posts
  @posts
end

#private_topics ⇒ Object (readonly)

Returns the value of attribute private_topics.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def private_topics
  @private_topics
end

#topics ⇒ Object (readonly)

Returns the value of attribute topics.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def topics
  @topics
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def user
  @user
end

#users ⇒ Object (readonly)

Returns the value of attribute users.



14
15
16
# File 'lib/thredded/seed_database.rb', line 14

def users
  @users
end

Class Method Details

.run(users: 5, topics: 26, posts: (0..20)) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/thredded/seed_database.rb', line 16

def self.run(users: 5, topics: 26, posts: (0..20))
  s = new
  s.create_messageboard
  s.create_first_user
  s.create_users(count: users)
  s.create_topics(count: topics)
  s.create_posts(count: posts)
  s.create_private_posts(count: posts)
end

Instance Method Details

#create_first_user ⇒ Object



26
27
28
29
30
31
# File 'lib/thredded/seed_database.rb', line 26

def create_first_user
  @user ||= begin
    ::User.first ||
      ::User.create(name: 'joe', email: '[email protected]')
  end
end

#create_messageboard ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/thredded/seed_database.rb', line 37

def create_messageboard
  @messageboard = FactoryGirl.create(
    :messageboard,
    name:        'Main Board',
    slug:        'main-board',
    description: 'A board is not a board without some posts'
  )
end

#create_posts(count: (0..30)) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/thredded/seed_database.rb', line 60

def create_posts(count: (0..30))
  @posts = topics.flat_map do |topic|
    (count.min + rand(count.max + 1)).times do
      FactoryGirl.create(:post, postable: topic, messageboard: messageboard, user: users.sample)
    end
  end
end

#create_private_posts(count: (0..30)) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/thredded/seed_database.rb', line 68

def create_private_posts(count: (0..30))
  @private_posts = private_topics.flat_map do |topic|
    (count.min + rand(count.max + 1)).times do
      FactoryGirl.create(:private_post, postable: topic, user: users.sample)
    end
  end
end

#create_topics(count: 26) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/thredded/seed_database.rb', line 46

def create_topics(count: 26)
  @topics = FactoryGirl.create_list(
    :topic, count,
    messageboard: messageboard,
    user:         users.sample,
    last_user:    users.sample)

  @private_topics = FactoryGirl.create_list(
    :private_topic, count,
    user:         users.sample,
    last_user:    users.sample,
    users:        [user])
end

#create_users(count: 5) ⇒ Object



33
34
35
# File 'lib/thredded/seed_database.rb', line 33

def create_users(count: 5)
  @users = [user] + FactoryGirl.create_list(:user, count)
end