Skip to content
Browse files

Create default filter if scope has method with same name as filter

  • Loading branch information...
1 parent 0d8395b commit 8fa6bb1ebc659a5ecb8206428d83b2f6aaad7ee2 @bogdan committed
Showing with 23 additions and 2 deletions.
  1. +2 −0 lib/datagrid/drivers/active_record.rb
  2. +8 −2 lib/datagrid/filters.rb
  3. +13 −0 spec/datagrid/filters_spec.rb
View
2 lib/datagrid/drivers/active_record.rb
@@ -43,6 +43,8 @@ def less_equal(scope, field, value)
def has_column?(scope, column_name)
scope.column_names.include?(column_name.to_s)
+ rescue ::ActiveRecord::StatementInvalid
+ false
end
end
end
View
10 lib/datagrid/filters.rb
@@ -67,8 +67,14 @@ def filter(attribute, *args, &block)
protected
def default_filter(attribute)
check_scope_defined!("Scope should be defined before filters")
- lambda do |value, scope, grid|
- grid.driver.where(scope, attribute => value)
+ if !driver.has_column?(scope, attribute) && driver.to_scope(scope).respond_to?(attribute)
+ lambda do |value, scope, grid|
+ grid.driver.to_scope(scope).send(attribute, value)
+ end
+ else
+ lambda do |value, scope, grid|
+ grid.driver.where(scope, attribute => value)
+ end
end
end
View
13 spec/datagrid/filters_spec.rb
@@ -100,4 +100,17 @@ def check_performed(value, result, options)
check_performed(nil, true, :allow_nil => true, :allow_blank => false)
end
end
+
+ describe "default filter as scope" do
+ it "should create default filter if scope respond to filter name method" do
+ Entry.create!
+ Entry.create!
+ grid = test_report(:limit => 1) do
+ scope {Entry}
+ filter(:limit)
+ end
+ grid.assets.to_a.size.should == 1
+ end
+
+ end
end

0 comments on commit 8fa6bb1

Please sign in to comment.
Something went wrong with that request. Please try again.