【问题标题】:Creating a draft version of an object using Audited Gem使用 Audited Gem 创建对象的草稿版本
【发布时间】:2018-11-13 22:25:22
【问题描述】:
Audited 适用于当前版本和以前的版本。我想要的是再有一个版本,一个未来的版本,又名草稿。
期望的场景
对象的当前版本在任何地方都使用。但是,在管理屏幕中,您可以访问和编辑对象的未来/草稿版本。这允许您进行其他人尚不可见的修改。当草稿准备好后,您将其发布,使其成为所有地方使用的当前版本。
我没有看到任何支持。
- 我错过了什么吗?是否支持?
- 是否有某种经过审核的 hack 可以支持这一点,即使是丑陋的方式?
- 如果以上都不是,这似乎可以通过 Audited gem 合理地完成,还是我最好使用其他方法?
【问题讨论】:
标签:
ruby
activerecord
acts-as-audited
【解决方案1】:
我没有使用 Audited gem 来提供“草稿”模式。相反,我在模型中添加了一个名为 active 的布尔值,并声明了
default_scope { where(active: true) }
scope :active, -> { where(active: true ) }
scope :draft, -> { where(active: false) }
然后,在控制器中,管理员查看草稿项的方法:
def in_draft
# Admins can see all items in draft status.
# Sellers can see only their own items in draft status.
# Buyers can't get here at all because of the authorizations
if current_tuser.role == "Themadmin"
@seller_listings = Listing.unscoped.draft
end
end
最后,控制器中发布项目的方法:
80 def publish
81 @listing = Listing.unscoped.find(params[:id])
82 @listing.active = true
83 respond_to do |format|
84 if @listing.save!
85 format.html {redirect_to @listing, notice: 'Listing changed from draft to published.'}
86 else
87 format.html {redirect_to @listing, notice: 'Something went wrong'}
88 end
89 end
90 end