1

有沒有人想出瞭如何使用ActiveResource與RailsAdmin或ActiveAdmin或與其他類似的引擎。ActiveResource與RailsAdmin或ActiveAdmin

看起來兩個項目都沒有此功能(https://github.com/activeadmin/activeadmin/issues/26,https://github.com/sferik/rails_admin/issues/1355)。另一方面,當數據駐留在其他服務/應用程序(vs數據庫)中時,這是相當常見的情況,我想知道是否有人將它們混在一起。

我試圖圍繞ActiveResource資源創建ActiveRecord包裝。但是,這超出了我對ActiveRecord的認識(可能是Ruby)。

回答

3

例與AA(含測試提交802dc451f3b3bd84e736b512d554867c0f2ae011)

ActiveAdmin.register Payment do 

    actions :index 
    config.batch_actions = false 
    filter :created_at_gteq, as: :date_time_picker 

    controller do 

    def find_collection 
     @search = OpenStruct.new(params[:q] || {}) 
     result = Payment.find(:all, params: { 
       order: params.fetch(:order, 'created_at_desc'), 
       page: params.fetch(:page, 1), 
       per_page: params.fetch(:per_page, 50), 
       q: params[:q] || {} 
       }) 
     #this uses https://github.com/Fivell/activeresource-response, 
     # but can be replaced with other ActiveResource pagination implementation 
     limit = result.http_response['X-Limit-Value'].to_i 
     offset = result.http_response['X-Offset-Value'].to_i 
     total = result.http_response['X-Total-Count'].to_i 
     Kaminari.paginate_array(result, limit: limit, offset: offset, total_count: total) 
    end 
    end  
end 

型號

class Payment < MyActiveResource 

    schema do 
    attribute 'customer_id', :integer 
    attribute 'amount', :integer 
    attribute 'created_at', :string 
    attribute 'success', :boolean 
    end 

    def self.column_names 
    content_columns 
    end 

    def self.content_columns 
    if @content_columns.nil? 
     @content_columns = Array.new 
     known_attributes.each do |name| 
     @content_columns << ResourceColumn.new(name) 
     end 
    end 
    @content_columns 
    end 

    def self.columns 
    content_columns 
    end 

    class ResourceColumn 
    attr_reader :name 

    def initialize(name) 
     @name = name 
    end 

    def type 
     :string 
    end 
    end 

end 

補丁

module ActiveAdmin 
    module Filters 
    module FormtasticAddons 
     alias :old_seems_searchable? :seems_searchable? 
     def seems_searchable? 
     return false if ransacker? 
     old_seems_searchable? 
     end 

     def klass 
     @object.try(:object).try(:klass) 
     end 

     def ransacker? 
     klass.try(:_ransackers).try(:key?, method.to_s) 
     end 

     def scope? 
     context = Ransack::Context.for klass rescue nil 
     context.respond_to?(:ransackable_scope?) && context.ransackable_scope?(method.to_s, klass) 
     end 
    end 
    end 
end 

module ActiveAdmin 
    module Helpers 
    module Collection 

     def collection_size(c = collection) 
     if c.is_a? ActiveRecord::Relation 
      c = c.except :select, :order 
      c.group_values.present? ? c.count.count : c.count 
     else 
      c.respond_to?(:count) ? c.count : 0 
     end 
     end 

    end 
    end 
end 
+0

太糟糕了,我不能投兩次票:)這是偉大的答案。 – 2015-04-16 15:16:30

+0

當我嘗試訪問'/ admin'中資源的索引頁時,我得到'undefined method \'_ransackers \'for nil:NilClass' – elju 2015-12-03 19:56:52

+0

@eliju,backtrace? – Fivell 2015-12-03 20:00:50