Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History order #3095

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class << self
# Default association limit
attr_accessor :default_associated_collection_limit

# Default order for history versions model in dashboard
attr_accessor :default_versions_order

attr_reader :default_search_operator

# Configuration option to specify which method names will be searched for
Expand Down Expand Up @@ -292,6 +295,7 @@ def reset
@navigation_static_links = {}
@navigation_static_label = nil
@parent_controller = '::ActionController::Base'
@default_versions_order = {id: :desc}
@forgery_protection_settings = {with: :exception}
RailsAdmin::Config::Actions.reset
end
Expand Down
5 changes: 3 additions & 2 deletions lib/rails_admin/config/actions/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Dashboard < RailsAdmin::Config::Actions::Base

register_instance_option :controller do
proc do
@history = @auditing_adapter && @auditing_adapter.latest(@action.auditing_versions_limit) || []
@history = @auditing_adapter && @auditing_adapter.latest(@action.auditing_versions_limit, RailsAdmin::Config.default_versions_order) || []
if @action.statistics?
@abstract_models = RailsAdmin::Config.visible_models(controller: self).collect(&:abstract_model)

Expand All @@ -31,7 +31,8 @@ class Dashboard < RailsAdmin::Config::Actions::Base
@max = current_count > @max ? current_count : @max
@count[t.model.name] = current_count
next unless t.properties.detect { |c| c.name == :created_at }
@most_recent_created[t.model.name] = t.model.last.try(:created_at)
sort_field = t.properties.detect { |c| c.name == t.config.dashboard_sort_by} ? t.config.dashboard_sort_by : :id
@most_recent_created[t.model.name] = t.model.order(sort_field).last.try(:created_at)
end
end
render @action.template_name, status: @status_code || :ok
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def pluralize(count)
nil
end

register_instance_option :dashboard_sort_by do
nil
end

# Act as a proxy for the base section configuration that actually
# store the configurations.
def method_missing(m, *args, &block)
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/extensions/history/auditing_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize(controller, user_class = User)
require 'rails_admin/extensions/history/history'
end

def latest(count = 100)
::RailsAdmin::History.latest(count)
def latest(count = 100, order = {id: :desc})
::RailsAdmin::History.latest(count, order)
end

def delete_object(object, model, user)
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/extensions/history/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class History < ActiveRecord::Base
default_scope { order('id DESC') }

class << self
def latest(count = 100)
limit(count)
def latest(count = 100, order = {id: :desc})
order(order).limit(count)
end

def create_history_item(message, object, abstract_model, user)
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/extensions/paper_trail/auditing_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def initialize(controller, user_class = 'User', version_class = '::Version')
end
end

def latest(count = 100)
def latest(count = 100, order = {id: :desc})
@version_class.
order(id: :desc).includes(:item).limit(count).
order(order).includes(:item).limit(count).
collect { |version| VersionProxy.new(version, @user_class) }
end

Expand Down