Module: MundaneSearch::ViewHelpers

Defined in:
lib/mundane-search/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#search_form_default_optionsObject



38
39
40
41
42
# File 'lib/mundane-search/view_helpers.rb', line 38

def search_form_default_options
  {
    method: "GET"
  }
end

#search_form_for(record, options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/mundane-search/view_helpers.rb', line 23

def search_form_for(record, options = {}, &block)
  # Later!
  # if block_given?
  #   form_for(record, search_form_default_options(record).merge(options), &block)
  # else
  #   # Auto-generate form
  # end
  form_for(record.to_model, search_form_default_options.merge(options), &block)
end


11
12
13
14
15
16
# File 'lib/mundane-search/view_helpers.rb', line 11

def search_link_for(content, search, change_params = {})
  search_model = search.to_model
  link_to content,
          search_url_for(search, change_params),
          class: will_merge_change_hash?(search_model.params, change_params) ? nil : "unchanged-search"
end

#search_url_for(search, change_params = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/mundane-search/view_helpers.rb', line 3

def search_url_for(search, change_params = {})
  search_model = search.to_model
  new_model_params = search_model.params.merge(change_params).reject {|k,v| v.nil? }
  new_params = Hash.new
  new_params[search_model.class.model_name.i18n_key] = new_model_params
  polymorphic_url search_model, new_params
end

#simple_search_form_for(record, options = {}, &block) ⇒ Object



33
34
35
36
# File 'lib/mundane-search/view_helpers.rb', line 33

def simple_search_form_for(record, options = {}, &block)
  # TODO: warn if simple_form not available?
  simple_form_for(record.to_model, search_form_default_options.merge(options), &block)
end

#will_merge_change_hash?(a, b) ⇒ Boolean

should this be private?



19
20
21
# File 'lib/mundane-search/view_helpers.rb', line 19

def will_merge_change_hash?(a,b)
  !(a.merge(b) == a)
end