module MultiSolr::RailsFormRenderHelper
# Generieren der HML-Form-Felder (input/select/..) für den angegeben Filter
# Params:
# form: das Rails-Form-Object, dieses MUSS als Object eine MultiSolr::SearchRequest-Instance haben
# filter_name: Name des Filters als Symbol (die SearchRequest-Instance im Form muss diesen Filter kennen)
# options: optionale Formular-Input-Feld-Options. Beschreibung => siehe jeweilige Beschreibung je Filter-Methode
# Diese werden i.d.R. an die Rails-Form-Methode 'text_field' weitergegeben.
#
# returns: HTML-Code des Input-Feldes als String
def render_solr_filter_for_form form, filter_name, options={}
cur_options = options.clone
search_request = form.object
filter = search_request.class.possible_filters[filter_name]
raise "unknown filter: #{filter_name}" if filter.nil?
label = cur_options.delete(:label) || filter.label
render_method = "render_#{filter.class.to_s.underscore}"
render_method.gsub!("/", '_')
render_method.gsub!("multi_solr_", '')
"".html_safe + send(render_method, form, filter, cur_options)
end
# generiert für einen SolrFilterSimple einen input-tag vom typ text
# mit der css-class "simple"
def render_solr_filter_simple form, filter, options
add_class options, 'simple'
form.text_field "filter_#{filter.name}", options
end
# generiert für einen SolrFilterSimple einen input-tag vom typ text
# mit der css-class "datepicker"
# Diese css-Klasse kann dann als jQuery-Selector für datepicker genutzt werden
def render_solr_filter_date form, filter, options
add_class options, 'datepicker'
form.text_field "filter_#{filter.name}", options
end
def render_solr_filter_date_range form, filter, options
add_class options, 'datepicker'
value = form.object.send("filter_#{filter.name}")
return nil if value.blank? || !value.is_a?(Hash) || value.empty?
from = value["#{filter.name}_from"]
to = value["#{filter.name}_to"]
date_range = Struct.new("#{filter.name}_from".to_sym, "#{filter.name}_to".to_sym).new from,to
form.fields_for "filter_#{filter.name}", date_range do |fields_form|
fields_form.template.concat("".html_safe)
fields_form.template.concat(fields_form.text_field("#{filter.name}_from", options))
fields_form.template.concat("".html_safe)
fields_form.template.concat(fields_form.text_field("#{filter.name}_to", options))
end
end
def render_solr_filter_collection form, filter, options
add_class options, 'collection'
core_handler = options.delete(:core_handler)
context = options.delete(:context)
data = filter.collection_data(core_handler, context)
form.select "filter_#{filter.name}", data, options
end
def render_solr_filter_free_query form, filter, options
add_class options, 'free_query'
form.text_field "filter_#{filter.name}", options
end
private
def add_class options, css_class_name
css_class = options[:class]
css_class = css_class.nil? ? css_class_name : "#{css_class} #{css_class_name}"
options[:class] = css_class
end
end