Class: AdminAssistant::Request::Autocomplete
- Inherits:
-
Base
- Object
- Base
- AdminAssistant::Request::Autocomplete
show all
- Defined in:
- lib/admin_assistant/request/autocomplete.rb
Instance Method Summary
collapse
Methods inherited from Base
#action, #after_template_file, #before_template_file, #initialize, #model_class, #model_class_symbol, #origin, #render_multi_form, #render_single_form, #render_template_file, #render_to_string, #render_to_string_if_exists
Instance Method Details
#associated_class ⇒ Object
4
5
6
7
8
|
# File 'lib/admin_assistant/request/autocomplete.rb', line 4
def associated_class
@associated_class ||= Module.const_get(
underscored_assoc_class_name.camelize
)
end
|
#call ⇒ Object
10
11
12
13
14
15
|
# File 'lib/admin_assistant/request/autocomplete.rb', line 10
def call
results = records.map { |record|
{:id => record.id.to_s, :name => record.send(record_name_field)}
}
@controller.send(:render, :json => results.to_json)
end
|
#record_name_field ⇒ Object
#records ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/admin_assistant/request/autocomplete.rb', line 21
def records
action =~ /autocomplete_(.*)/
associated_class.find(
:all,
:conditions => [
"LOWER(#{record_name_field}) like ?",
"%#{search_string.downcase unless search_string.nil?}%"
],
:limit => 10,
:order => "length(#{record_name_field}), lower(#{record_name_field})"
)
end
|
#search_string ⇒ Object
34
35
36
|
# File 'lib/admin_assistant/request/autocomplete.rb', line 34
def search_string
@controller.params['q']
end
|
#underscored_assoc_class_name ⇒ Object
38
39
40
41
|
# File 'lib/admin_assistant/request/autocomplete.rb', line 38
def underscored_assoc_class_name
action =~ /autocomplete_(.*)/
$1
end
|