Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/searchify/searchify_helper.rb

Instance Method Summary collapse

Instance Method Details

#searchify(field, *args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/searchify/searchify_helper.rb', line 66

def searchify(field, *args)
  options = args.extract_options!

  # searchify options
  model_name  = options.delete(:model_name) || extract_model_name(field)
  field_name  = options.delete(:field_name) || extract_field_name(field)
  collection  = options.delete(:collection) || extract_collection(model_name)
  search_url  = options.delete(:search_url) || extract_search_url(collection, options.delete(:scopes), options.delete(:search_strategy))

  # field options
  options[:class] = [:searchify].push(options[:class]).flatten.compact
  options[:data]  = {:'search-url' => search_url}.merge(options[:data] || {})

  # default id
  options[:id] ||= "#{object_name.gsub(/\[/, '_').gsub(/\]/, '')}_#{field_name}"

  # value
  label_method = options.delete(:label_method) || Searchify::Config.label_method
  
  # fetch the value only if object respond_to model_name
  html_value = object.respond_to?(model_name) ? object.send(model_name).try(label_method) : ""

  hidden_field(field_name, :id => "#{options[:id]}_hidden") + @template.text_field_tag(:searchify, html_value, options)
end