@@ -49,17 +49,23 @@ def autocomplete(object, method, options = {})
49
49
50
50
term = params [ :term ]
51
51
52
+ # allow specifying fully qualified class name for model object
53
+ class_name = options [ :class_name ] || object
54
+ parameters = {
55
+ :model => get_object ( class_name ) ,
56
+ :controller => self ,
57
+ :term => term ,
58
+ :method => method ,
59
+ :options => options
60
+ }
61
+
52
62
if term && !term . blank?
53
- #allow specifying fully qualified class name for model object
54
- class_name = options [ :class_name ] || object
55
- items = get_autocomplete_items ( :model => get_object ( class_name ) , \
56
- :controller => self , \
57
- :options => options , :term => term , :method => method )
63
+ items = get_autocomplete_items ( parameters )
58
64
else
59
65
items = { }
60
66
end
61
67
62
- render :json => json_for_autocomplete ( items , options [ :display_value ] ||= method , options [ :extra_data ] )
68
+ render :json => json_for_autocomplete ( items , parameters )
63
69
end
64
70
end
65
71
end
@@ -83,12 +89,14 @@ def get_object(model_sym)
83
89
# Can be overriden to show whatever you like
84
90
# Hash also includes a key/value pair for each method in extra_data
85
91
#
86
- def json_for_autocomplete ( items , method , extra_data = [ ] )
92
+ def json_for_autocomplete ( items , parameters )
93
+ method = parameters [ :options ] [ :display_value ] ||= parameters [ :method ]
94
+ extra_data = parameters [ :options ] [ :extra_data ]
87
95
items . collect do |item |
88
96
hash = { "id" => item . id . to_s , "label" => item . send ( method ) , "value" => item . send ( method ) }
89
97
extra_data . each do |k , v |
90
98
if v
91
- hash [ k ] = v . is_a? ( Proc ) ? v . call ( item ) : item . send ( v )
99
+ hash [ k ] = v . is_a? ( Proc ) ? v . call ( item , parameters ) : item . send ( v )
92
100
else
93
101
hash [ k ] = item . send ( k )
94
102
end
0 commit comments