我有一个简单的脚手架“Post”,现在我想为它添加日程安排,这样我就可以创建一个帖子并将日期添加到它,而Post只会在那时创建(实际发布)。
我可以看到我需要使用'when'宝石作为cron,并且可能想要使用published_at字段,但我有点困惑如何去做?
由于
答案 0 :(得分:3)
我认为添加一个published_at字段会更好,因为你实际上已经有了记录。
# Add columns. (DB migration)
add_column :posts, :draft, :boolean, :default => true
add_column :posts, :published_at, :datetime
# Define scopes. (model)
#
# These set up shortcuts that will allow you to do:
# Post.draft
# instead of this in many places:
# Post.where(:draft => true)
# Or:
# Post.published
# instead of:
# Post.where(:draft => false).where('published_at <= ?', Time.zone.now)
scope :draft, where(:draft => true)
scope :published, proc {
where(:draft => false).where('published_at <= ?', Time.zone.now)
}
# If the user does not set published_at but sets post to public, automatically
# set published_at to the current time before persisting the record to the
# DB. (model)
before_save :ensure_published_at, :unless => :draft?
protected
def ensure_published_at
# Set it to current time if none has been specified.
self.published_at ||= Time.zone.now
end
# Might be helpful if you have a "Publish" action. (model)
#
# This already sets draft to false and published at to the default (because of
# ensure_published_at above:
# post.publish!
def publish!
self.draft = false
self.save!
end
# Finally, fetch the published posts: (controller action)
# It's best to also add pagination and ordering.
@posts = Post.published
答案 1 :(得分:2)
我可以向你展示我的一个项目中实际工作的代码:
# config/schedule.rb:
# Once wheneverifyed - asks cron to call rake task `posts:publish` every minute.
# See whenever docs at https://github.com/javan/whenever.
every 1.minute do
rake 'posts:publish', environment: environment
end
# models/post.rb
class Post < ActiveRecord::Base
# Every post has
# status - either PUBLISH_WAITING or PUBLISHED
# publish_at - date/time post need to be published
# published_at - actual time post was published (in this example it'll always be equal to publish_at)
PUBLISH_WAITING, PUBLISHED = 'not_published', 'published'
scope :publish_waiting, where(status: Post::PUBLISH_WAITING)
scope :ready_for_publish, where('publish_at <= ?', Time.now)
# Method to make post published!
# Warn: it doesnt check if it is time to publish! Just do that.
def publish_now!
self.status = Post::PUBLISHED
self.published_at = self.publish_at
save!
end
end
# lib/tasks/posts.rake
# Define rake `posts:publish` task,
# which when called searches through all not published
# (but ready to be published) posts and publishes them.
# You can call that task manually from CLI `$ rake posts:publish` to check.
namespace :posts do
desc "Publish posts with cron on certain time"
task :publish => :environment do
Post.publish_waiting.ready_for_publish.find_each do |post|
post.publish_now!
end
end
end