/
abstract_filter.rb
87 lines (67 loc) · 1.86 KB
/
abstract_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class AbstractFilter
attr_reader :grid, :name, :input_options, :input_tag, :format
def initialize grid, name, opts ={}
@grid, @name = grid, name
@label = opts[:label]
@input_options = opts[:input]
@input_tag = opts[:input_tag]
@include_blank = opts[:include_blank]
@exact = opts.fetch(:exact){true}
@ignore_case = opts.fetch(:ignore_case){false}
@format = opts[:format]
@param_scoped = opts.fetch(:param_scoped){true}
# replace shortcut
if coll = opts[:input_collection]
(@input_options||={})[:collection] = coll
end
end
def label
@label ||= grid.translate(:filters, name)
end
def input?
@input_options || @input_tag
end
def include_blank?
@include_blank
end
def param_scoped?
@param_scoped
end
def counts driver = grid.filtered_driver
{ true => apply(driver.unordered).count }
end
def distinct_values driver = nil
[ true, false ]
end
def input_tag
return '' unless input?
@input_tag ||= if col = collection
col = [''] + col if include_blank? && col.size > 1
select_tag col
else
text_field_tag
end
end
def text_field_tag
@text_field_tag ||= h.text_field_tag scoped_name, grid.params[name.intern], :class => 'grid'
end
def select_tag collection
h.select_tag scoped_name, h.options_for_select(collection, grid.params[name.intern]), :class => 'grid'
end
private
def scoped_name
"#{grid.key}[#{name}]"
end
def collection
input_options.respond_to?(:[]) && input_options[:collection]
end
def exact opts = {}
opts.fetch(:exact){@exact}
end
def ignore_case opts = {}
opts.fetch(:ignore_case){@ignore_case}
end
def h
@h ||= grid.h
end
end