Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added table name to sort order to fix ambiguous queries with joins.

* Closes #38
* Also removed the auto-requiring of rake
  • Loading branch information...
commit b01cd5d1923e505a9e75fae0dd2d175630783630 1 parent 7f94bdd
@pcreux pcreux authored gregbell committed
View
2  Gemfile
@@ -34,7 +34,7 @@ gem 'haml', '>= 3.0.18'
group :development, :test do
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'jeweler', '1.5.2'
- gem 'rake', '0.8.7'
+ gem 'rake', '0.8.7', :require => false
end
group :test do
View
4 lib/active_admin/resource.rb
@@ -82,6 +82,10 @@ def plural_resource_name
@plural_resource_name ||= resource_name.pluralize
end
+ def resource_table_name
+ resource.table_name
+ end
+
# Returns a properly formatted controller name for this
# resource within its namespace
def controller_name
View
3  lib/active_admin/resource_controller/collection.rb
@@ -46,8 +46,9 @@ def active_admin_collection
def sort_order(chain)
params[:order] ||= active_admin_config.sort_order
+ table_name = active_admin_config.resource_table_name
if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
- chain.order("#{$1} #{$2}")
+ chain.order("#{table_name}.#{$1} #{$2}")
else
chain # just return the chain
end
View
11 spec/unit/resource_controller/collection_spec.rb
@@ -6,7 +6,7 @@
end
let(:controller) do
- rc = ActiveAdmin::ResourceController.new
+ rc = Admin::PostsController.new
rc.stub!(:params) do
params
end
@@ -22,4 +22,13 @@
end
end
+ describe ActiveAdmin::ResourceController::Collection::Sorting do
+ let(:params){ {:order => "id_asc" }}
+ it "should prepend the table name" do
+ chain = mock("ChainObj")
+ chain.should_receive(:order).with("posts.id asc").once.and_return(Post.search)
+ controller.send :sort_order, chain
+ end
+ end
+
end
View
13 spec/unit/resource_spec.rb
@@ -43,13 +43,24 @@ module ::Mock; class Resource; end; end
it "should return the plural version" do
config.plural_resource_name.should == "Categories"
end
- context "when the :as option is give" do
+ context "when the :as option is given" do
it "should return the custom name" do
config(:as => "My Category").resource_name.should == "My Category"
end
end
end
+ describe "#resource_table_name" do
+ it "should return the resource's table name" do
+ config.resource_table_name.should == 'categories'
+ end
+ context "when the :as option is given" do
+ it "should return the resource's table name" do
+ config(:as => "My Category").resource_table_name.should == 'categories'
+ end
+ end
+ end
+
describe "namespace" do
it "should return the namespace" do
config.namespace.should == namespace
Please sign in to comment.
Something went wrong with that request. Please try again.