Skip to content

Commit

Permalink
Merge fe3a7c8 into 85b9f81
Browse files Browse the repository at this point in the history
  • Loading branch information
mwean committed Sep 20, 2013
2 parents 85b9f81 + fe3a7c8 commit f61707f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/active_admin/resource_controller/data_access.rb
Expand Up @@ -217,7 +217,9 @@ def apply_authorization_scope(collection)

def apply_sorting(chain)
params[:order] ||= active_admin_config.sort_order
if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
if params[:order] && chain.respond_to?("sort_by_#{params[:order]}")
chain.send("sort_by_#{params[:order]}")
elsif params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
column = $1
order = $2
table = active_admin_config.resource_column_names.include?(column) ? active_admin_config.resource_table_name : nil
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/resource_controller/data_access_spec.rb
Expand Up @@ -43,6 +43,23 @@
end
end

context "for custom sorting methods" do
let(:params){ {:order => "custom_field_asc" }}
context "when the methods exist on the model" do
it "should call the model's sort methods" do
chain = mock("ChainObj", sort_by_custom_field_asc: :sorted)
controller.send(:apply_sorting, chain).should == :sorted
end
end

context "when the methods do not exist on the model" do
it "should call reorder" do
chain = mock("ChainObj")
chain.should_receive(:reorder).once
controller.send :apply_sorting, chain
end
end
end
end

describe "scoping" do
Expand Down

0 comments on commit f61707f

Please sign in to comment.