Skip to content

Commit 39c8af3

Browse files
committed
fixed seeds skipping initial posts if UPDATE_POSTS is not set
1 parent 2e13957 commit 39c8af3

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

db/seeds.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
types = SeedsHelper.types(files)
77
sorted = SeedsHelper.prioritize(types, files)
88

9+
should_update_posts = ENV['UPDATE_POSTS'] == 'true'
10+
911
def expand_communities(type, seed)
1012
if type.column_names.include?('community_id') && !seed.include?('community_id')
1113
# if model includes a community_id, create the seed for every community
@@ -76,6 +78,17 @@ def ensure_system_user_abilities
7678
end
7779
end
7880

81+
def render_seed(seed)
82+
ApplicationController.helpers.render_markdown(seed['body_markdown'])
83+
end
84+
85+
# TODO: make it the Post model's predicate (and add association):
86+
def not_edited?(post)
87+
PostHistory.where(post: post)
88+
.where.not(post_history_type: PostHistoryType.find_by(name: 'initial_revision'))
89+
.count.zero?
90+
end
91+
7992
sorted.each do |f, type|
8093
begin
8194
processed = ERB.new(File.read(f)).result(binding)
@@ -91,19 +104,16 @@ def ensure_system_user_abilities
91104
end
92105
end
93106

94-
if type == Post && ENV['UPDATE_POSTS'] == 'true'
95-
seed['body'] = ApplicationController.helpers.render_markdown(seed['body_markdown'])
107+
if type == Post
108+
seed['body'] = render_seed(seed)
96109

97110
system_usr = User.find(-1)
98111

99112
Community.all.each do |c|
100113
RequestContext.community = c
101-
post = Post.find_by doc_slug: seed['doc_slug']
102-
if post.present? && PostHistory.where(post: post)
103-
.where.not(post_history_type:
104-
PostHistoryType.find_by(name: 'initial_revision'))
105-
.count.zero?
114+
post = Post.find_by(doc_slug: seed['doc_slug'])
106115

116+
if post.present? && should_update_posts && not_edited?(post)
107117
# post exists, still original version: update post
108118
post.update(seed.merge('community_id' => c.id))
109119

@@ -121,7 +131,7 @@ def ensure_system_user_abilities
121131
# post doesn't exist: create post
122132
status = Post.create seed.merge('community_id' => c.id, 'user' => system_usr)
123133

124-
if status.errors.size
134+
if status.errors.any?
125135
status.errors.full_messages.each do |msg|
126136
puts "[#{c.name}:#{seed['doc_slug']}] invalid: #{msg}"
127137
end

0 commit comments

Comments
 (0)