Skip to content

Instantly share code, notes, and snippets.

@zhaiyingchang
Created August 31, 2017 09:36
Show Gist options
  • Save zhaiyingchang/4118261855d390635535398f70c15da6 to your computer and use it in GitHub Desktop.
Save zhaiyingchang/4118261855d390635535398f70c15da6 to your computer and use it in GitHub Desktop.
rake task demo
namespace :article do
task :migrate_to_disable_article_function => [:environment, :set_group_has_article_plugin_false, :make_moments_for_articles]
task :set_group_has_article_plugin_false do
progress_bar = ProgressBar.create(:title => "set group.has_article_plugin false", :total => 1)
Group.update_all has_article_plugin: false
progress_bar.progress += 1
end
task :make_moments_for_articles do
progress_bar = ProgressBar.create(:title => "create moments for user's article", :total => Article.count)
Article.all.each do |article|
make_moments_for_article(article)
progress_bar.progress += 1
end
end
def make_moments_for_article(article)
group = article.group
user = article.author || article.uploader
accession = group && user && user.accession_for_group(group)
return unless accession
link = format("%s/groups/%s/articles/%s", ENV["HOST"], group.id.to_s, article.id.to_s)
begin
Group::Moment.skip_callback(:create, :after, :send_created_push_notification)
moment = Group::Moment.new(group: group, author: user, accession: accession, created_at: article.created_at, attachment: {link: link})
moment.save!
rescue => e
puts(e.message) && puts(link)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment