Permalink
Browse files

Default sort order now uses the resource's primary_key if available.

Deprecated the default_sort_order setting. Closes #235
  • Loading branch information...
gregbell authored and whatthewhat committed Jul 20, 2011
1 parent c8d2e13 commit 79da64b52a8ee7e521bbeca14c16a3c2a60f9ac7
Showing with 24 additions and 13 deletions.
  1. +5 −3 lib/active_admin/application.rb
  2. +1 −1 lib/active_admin/resource.rb
  3. +18 −9 spec/unit/resource_spec.rb
@@ -15,9 +15,6 @@ class Application
# The default number of resources to display on index pages
setting :default_per_page, 30
- # The default sort order for index pages
- setting :default_sort_order, 'id_desc'
-
# A hash of all the registered namespaces
setting :namespaces, {}
@@ -55,6 +52,11 @@ class Application
:email,
:to_s ]
+ # == Deprecated Settings
+
+ # @deprecated The default sort order for index pages
+ deprecated_setting :default_sort_order, 'id_desc'
+
include AssetRegistration
def initialize
@@ -140,7 +140,7 @@ def csv_builder
def default_options
{
:namespace => ActiveAdmin.application.default_namespace,
- :sort_order => ActiveAdmin.application.default_sort_order
+ :sort_order => "#{resource.respond_to?(:primary_key) ? resource.primary_key : 'id'}_desc"
}
end
View
@@ -202,20 +202,29 @@ def config(options = {})
describe "sort order" do
- subject { resource_config.sort_order }
- context "by default" do
- let(:resource_config) { config }
-
- it { should == application.default_sort_order }
+ context "when resource class responds to primary_key" do
+ it "should sort by primary key desc by default" do
+ mock_resource = mock
+ mock_resource.should_receive(:primary_key).and_return("pk")
+ config = Resource.new(namespace, mock_resource)
+ config.sort_order.should == "pk_desc"
+ end
end
- context "when default_sort_order is set" do
- let(:sort_order) { "name_desc" }
- let(:resource_config) { config :sort_order => sort_order }
+ context "when resource class does not respond to primary_key" do
+ it "should default to id" do
+ mock_resource = mock
+ config = Resource.new(namespace, mock_resource)
+ config.sort_order.should == "id_desc"
+ end
+ end
- it { should == sort_order }
+ it "should be set-able" do
+ config.sort_order = "task_id_desc"
+ config.sort_order.should == "task_id_desc"
end
+
end
describe "adding a scope" do

0 comments on commit 79da64b

Please sign in to comment.