Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 8fa6bb1ebc659a5ecb8206428d83b2f6aaad7ee2 1 parent 0d8395b
@bogdan bogdan authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.