Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed `filter_options_for`

  • Loading branch information...
commit cad5ed4046ae01918ab49e4c35bab390094b4452 1 parent 70ef382
@matthewd matthewd authored
View
2  lib/auto_admin_configuration.rb
@@ -117,7 +117,7 @@ def filter_option_sql column_name, option_name
end
def filter_options_for column_name, custom_options, &block
- (@custom_filter_options ||= {})[column_name.to_sym] = AutoAdmin::CustomFilterSet.new( column_name, custom_options, &block )
+ (@custom_filter_options ||= {})[column_name.to_sym] = AutoAdmin::CustomFilterSet.new( self, reflect_on_association( column_name ) || find_column( column_name ), custom_options, &block )
end
def filters
columns_for_filter.map { |col| filter_instance( col ) }
View
5 lib/auto_admin_filter_sets.rb
@@ -41,11 +41,14 @@ def initialize klass, column, options, &block
super klass, column
@options, @block = options, block
end
+ def sql_from_string option_name
+ @block ? @block.call( option_name ) : super
+ end
def other_options
a = []
@options.each do |k,v|
o = build_option( k, v )
- o[:sql] = block.call( k ) unless block.nil?
+ o[:sql] = @block.call( k ) unless @block.nil?
a << o
end
a.sort_by {|i| i[:label] }
View
18 test/configuration_tests.rb
@@ -133,6 +133,24 @@ def test_sort_by_two
end
+ def test_filter_options_for
+ assert_equal [], Film.columns_for_filter
+ Film.filter_by :rating
+ Film.filter_options_for :rating, 'A' => 'X', 'B' => 'Y', 'C' => 'Z'
+ assert_equal [:rating], Film.columns_for_filter
+ assert_equal( { 'rating' => '*' }, Film.filter_defaults )
+ assert_equal ['rating = ?', 'B'], Film.filter_conditions( :rating => 'B' )
+ end
+ def test_filter_options_for_with_custom_sql_builder
+ assert_equal [], Film.columns_for_filter
+ Film.filter_by :length
+ Film.filter_options_for( :length, '0-60' => 'Very Short', '60-120' => 'Short', '120-999' => 'Long' ) do |val|
+ ["length BETWEEN ? AND ?", *( val.split('-').map {|n| n.to_i } )]
+ end
+ assert_equal [:length], Film.columns_for_filter
+ assert_equal( { 'length' => '*' }, Film.filter_defaults )
+ assert_equal ['length BETWEEN ? AND ?', 0, 60], Film.filter_conditions( :length => '0-60' )
+ end
def test_filter_by_string
assert_equal [], Film.columns_for_filter
Film.filter_by :rating
Please sign in to comment.
Something went wrong with that request. Please try again.