2014-09-02 12 views
0

我现在用的是"Index as Table"功能显示与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。

回答

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 

UPDATE: 这是一个猴补丁。在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

+0

这不会让我一个分段的行。这只是给我的应用增加了一个更大的存储需求,尽管CachedRecords实际上只是一个Rails.cache检查。 – 2014-09-02 19:27:32

+0

我想你缓存因为太多的sql语句... – nistvan 2014-09-02 19:32:57

+0

我更新了我的答案。 – nistvan 2014-09-04 18:43:53