Rails 使用 Paranoia 和 Adminstrate,有没有办法通过用户管理获取已删除的记录?
Rails using Paranoia and Administrate, Is there a way to get deleted record with administrate for user?
我想在 paranoia on admin panel with administrate 之前更改所有已删除的记录。我的问题是我正在寻找一种方法来做这些事情,但直到现在都没有成功
实际上,我想做的是在使用 Administrate
生成的特定控制器上重写索引方法,以便将所有元素(已删除或未删除)作为当前控制器的资源。像这样:
controllers/admin/foo_controller.rb
module Admin
class FooController < Admin::ApplicationController
super
resources = Foo.where("at_delete IS NOT NULL").page(params[:page])
end
end
但是当我从管理面板创建一个 foo 对象,并在删除它之后。该记录不再显示,我想让它仍然可见,以便管理员可以更改它。
如果有人有意见使这成为可能,那就太好了。
预先感谢您的帮助。
首先非常感谢@Tom Lord,他帮助我解决了我的问题,这就是解决方案:
- 首先在您的
app/controllers/admin/application_controller.rb
中覆盖由 Administrate
生成的内容,如下所示:
module Admin
class ApplicationController < Administrate::ApplicationController
...
def index
search_term = params[:search].to_s.strip
resources = Administrate::Search.new(scoped_resource,
dashboard_class,
search_term).run
resources = resources.includes(*resource_includes) if resource_includes.any?
resources = order.apply(resources)
resources = resources.page(params[:page]).per(records_per_page)
resources = finder_chain_additions(resources)
page = Administrate::Page::Collection.new(dashboard, order: order)
render locals: {
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?
}
end
private
def scoped_resource
begin
# Provide resource with deleted_at field generate with Paranoia
resource_class.unscoped
rescue
# Used for models whose don't have Paranoia field
resource_class
end
end
def finder_chain_additions resources
begin
resources.with_deleted
rescue
resources
end
end
end
end
- 在你的
app/dashboards/foo_dashboard.rb
require "administrate/base_dashboard"
class ArticleDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
id: Field::Number,
...
created_at: Field::DateTime,
updated_at: Field::DateTime,
deleted_at: Field::DateTime,
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [
:id,
...
:deleted_at,
:hide,
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = [
:id,
...
:deleted_at,
:created_at,
:updated_at,
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [
...
:deleted_at,
].freeze
# Overwrite this method to customize how articles are displayed
# across all pages of the admin dashboard.
#
# def display_resource(article)
# "Article ##{article.id}"
# end
end
当然,更改 deleted_at
字段取决于您可能为添加偏执狂所做的迁移 gem。
我想在 paranoia on admin panel with administrate 之前更改所有已删除的记录。我的问题是我正在寻找一种方法来做这些事情,但直到现在都没有成功
实际上,我想做的是在使用 Administrate
生成的特定控制器上重写索引方法,以便将所有元素(已删除或未删除)作为当前控制器的资源。像这样:
controllers/admin/foo_controller.rb
module Admin
class FooController < Admin::ApplicationController
super
resources = Foo.where("at_delete IS NOT NULL").page(params[:page])
end
end
但是当我从管理面板创建一个 foo 对象,并在删除它之后。该记录不再显示,我想让它仍然可见,以便管理员可以更改它。
如果有人有意见使这成为可能,那就太好了。
预先感谢您的帮助。
首先非常感谢@Tom Lord,他帮助我解决了我的问题,这就是解决方案:
- 首先在您的
app/controllers/admin/application_controller.rb
中覆盖由Administrate
生成的内容,如下所示:
module Admin
class ApplicationController < Administrate::ApplicationController
...
def index
search_term = params[:search].to_s.strip
resources = Administrate::Search.new(scoped_resource,
dashboard_class,
search_term).run
resources = resources.includes(*resource_includes) if resource_includes.any?
resources = order.apply(resources)
resources = resources.page(params[:page]).per(records_per_page)
resources = finder_chain_additions(resources)
page = Administrate::Page::Collection.new(dashboard, order: order)
render locals: {
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?
}
end
private
def scoped_resource
begin
# Provide resource with deleted_at field generate with Paranoia
resource_class.unscoped
rescue
# Used for models whose don't have Paranoia field
resource_class
end
end
def finder_chain_additions resources
begin
resources.with_deleted
rescue
resources
end
end
end
end
- 在你的
app/dashboards/foo_dashboard.rb
require "administrate/base_dashboard"
class ArticleDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
id: Field::Number,
...
created_at: Field::DateTime,
updated_at: Field::DateTime,
deleted_at: Field::DateTime,
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [
:id,
...
:deleted_at,
:hide,
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = [
:id,
...
:deleted_at,
:created_at,
:updated_at,
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [
...
:deleted_at,
].freeze
# Overwrite this method to customize how articles are displayed
# across all pages of the admin dashboard.
#
# def display_resource(article)
# "Article ##{article.id}"
# end
end
当然,更改 deleted_at
字段取决于您可能为添加偏执狂所做的迁移 gem。