0

我正在使用“索引作为表”功能显示带有 ActiveAdmin 的表:

index :pagination_total => false do

    if Ability.new(current_user).can? :manage, :metric
        selectable_column
    end

    column '' do |metric|
        links = link_to(metric.icon, admin_metric_path(metric), :title => metric.comment)
        links += link_to(metric.data_icon, admin_metric_path(metric)) unless metric.datum_ids.empty?
        links
    end

    column 'Status', :success, sortable: :success do |metric|
        metric.success == 1 ? status_tag('Success', :ok) : status_tag('FAILED', :error)
    end
    column 'When (UTC)', :createddttm
    column 'What', :metric_name
    column 'Area',    :logarea
    column 'Subarea', :subarea
    column 'Value',   :value
    column 'Machine', :machine_name, sortable: 'machinename.machinename'
    column 'Domain',  :domain_name, sortable: 'domain.domainname'
    column 'Product', :product_name, sortable: 'product.productname'
    column 'Version', :product_version, sortable: 'product.productversion'
    column 'Install Type', :install_type, sortable: 'product.productinstalltype'
    column 'Lang', :language
    column 'Duration', :duration
end

鉴于行数据没有改变,我想为渲染的 html添加行级缓存,过期时间很长,但我不知道如何挂钩Arbre中的行渲染代码。

我目前正在将整个页面缓存 60 秒,但这不是最佳的。我的缓存存储是 Dalli / memcached。

4

1 回答 1

0

更新 2:

试试这个cashed_rows方法:

# lib\active_admin\views\index_as_table.rb
module ActiveAdmin
  module Views
    class IndexAsTable < ActiveAdmin::Component

      def build(page_presenter, collection)

        table_options = {
            id: "index_table_#{active_admin_config.resource_name.plural}",
            sortable: true,
            class: "index_table index",
            i18n: active_admin_config.resource_class,
            paginator: page_presenter[:paginator] != false,
            row_class: page_presenter[:row_class]
        }

        table = table_for collection, table_options do |t|
          table_config_block = page_presenter.block || default_table
          instance_exec(t, &table_config_block)
        end

        #new code
        rows = []
        table.children.each {|row| rows << row.to_s}
        resource_class.cached_rows = rows 

        table
      end
    end
  end
end

更新: 这是一个猴子补丁。在 lib 文件夹中创建一个新文件,然后您可以使用该rows数组。

module ActiveAdmin
  module Views
    class TableFor < Arbre::HTML::Table

      def build_table_body

        @tbody = tbody do
          # Build enough rows for our collection
          @collection.each do |elem|
            classes = [cycle('odd', 'even')]

            if @row_class
              classes << @row_class.call(elem)
            end

            tr(class: classes.flatten.join(' '), id: dom_id_for(elem))
          end
        end

        #new code: rows will contain the html string for each row
        rows = []
        @tbody.children.each {|row| rows << row.to_s} #you can also use: @tbody.to_s
        YourActiveRecordModel.cached_rows= rows

        @tbody 
      end
    end
  end
end

class YourActiveRecordModel < ActiveRecord::Base

  def self.cached_rows=(attr)
    @@rows = attr
  end

  def self.cached_rows
    @@rows
  end
end

现在您可以获取 html 字符串:YourActiveRecordModel.cached_rows,如果您至少构建一次索引表。

老答案:

我会创建一个新表,其中包含所包含对象的字符串表示形式。您可以确定要加载的记录(原始记录或缓存记录):

  controller do
    def find_resource
      if params[:action] == 'index' #or something similar
         CachedRecords.find(params[:id])
      elsif params[:action] == 'edit'
         OriginalRecords.find(params[:id])
    end
  end

https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#customizing-resource-retrieval

于 2014-09-02T18:51:08.877 回答