Skip to content

Instantly share code, notes, and snippets.

@ccabot
Created January 26, 2011 19:03
Show Gist options
  • Save ccabot/797219 to your computer and use it in GitHub Desktop.
Save ccabot/797219 to your computer and use it in GitHub Desktop.
patch rails_admin to use paper_trail
require "rails_admin/application_controller"
require "rails_admin/abstract_history"
module RailsAdmin
# use declarative_authorization for rails_admin's authz
class ApplicationController < ::ApplicationController
filter_access_to :all
end
# patch RailsAdmin to use PaperTrail instead of its home-grown
# history tracking
class AbstractHistory
# disable rails_admin's history tracking
def self.create_history_item(message, object, abstract_model, user) ; end
# Fetch the history items for a model. Returns an array containing
# the page count and an AR query result containing the history
# items.
def self.history_for_model(model, query, sort, sort_reverse, all, page = 1, per_page = RailsAdmin::Config::Sections::List.default_items_per_page || 20)
page ||= "1"
versions = Version.where :item_type => model.pretty_name
if sort
versions = versions.order(sort_reverse == "true" ? "#{sort} DESC" : sort)
end
if all
[1, versions]
else
page_count = (versions.count.to_f / per_page).ceil
[page_count, versions.limit(per_page).offset((page.to_i - 1) * per_page)]
end
end
# Fetch the history items for a specific object instance.
def self.history_for_object(model, object, query, sort, sort_reverse)
versions = Version.where :item_type => model.pretty_name, :item_id => object.id
if sort
versions = versions.order(sort_reverse == "true" ? "#{sort} DESC" : sort)
end
versions
end
# Fetch the history item counts for one month.
def self.history_for_month(month, year)
start = Date.new(year, month)
return Version.find(:all, :conditions => ["created_at >= ? AND created_at < ?", start, start.advance(:months=>1)])
end
# Fetch the history item counts between the dates provided.
def self.history_summaries(from, to)
start = Date.new(from[:year].to_i, from[:month].to_i)
months = []
5.times do
stop = start.advance(:months=>1)
months += Version.find_by_sql(["select count(*) as number, ? as year, ? as month from versions where created_at >= ? AND created_at < ?", start.year, start.month, start, stop])
start = stop
end
months
end
# Fetch the most recent history item for a model.
def self.most_recent_history(name)
Version.where(:item_type => name).order(:id).limit(1)
end
end
end
@DougPuchalski
Copy link

I think model.pretty_name should be model.model.to_s here.

@DougPuchalski
Copy link

Looks like AbstractHistory should only return the collection, not [page_number, collection]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment