在我的一个RoR项目中,我正在使用ActiveAdmin (1.4.3)和Discard宝石。 我创建了两个这样的自定义范围:
scope ('Kept') { |scope| scope.kept }
scope ('Discarded') { |scope| scope.discarded }
我想将其中之一(“保留”)设置为默认范围,该默认范围将在用户访问资源的索引页时应用(并且我不喜欢模型中的the idea of adding default_scope)
这是Active Admin documentation的摘录:
scope :all, default: true
# assumes the model has a scope called ':active'
scope :active
# renames model scope ':leaves' to ':subcategories'
scope "Subcategories", :leaves
# Dynamic scope name
scope ->{ Date.today.strftime '%A' }, :published_today
# custom scope not defined on the model
scope("Inactive") { |scope| scope.where(active: false) }
# conditionally show a custom controller scope
scope "Published", if: -> { current_admin_user.can? :manage, Posts } do |posts|
posts.published
end
它表明有一个可选的default
参数,实际上,该参数按预期用于带有符号的简单作用域。
我试图将其添加到我的自定义范围中,如下所示:
scope ('Kept', default: true) { |scope| scope.kept }
但是我遇到语法错误。
它似乎仅适用于带有符号的范围,不适用于字符串和可选块。有什么解决方法吗?