Class: TableMe::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/table_me/filter.rb

Overview

This class creates the filter forms for searching a table. Unlike the column class, the filter class is responsible for creating it’s own view. See the table_for_helper file for documentation on how to use filters

Constant Summary collapse

@@filters =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, column_name) ⇒ Filter

Returns a new instance of Filter.



16
17
18
19
20
21
22
23
24
25
# File 'lib/table_me/filter.rb', line 16

def initialize options, column_name
  self.options = options
  self.column_name = column_name

  @@filters[options[:name]] ||= []


  @@filters[options[:name]].delete_if {|item| item.column_name == column_name}
  @@filters[options[:name]] << self 
end

Instance Attribute Details

#column_nameObject

Returns the value of attribute column_name.



8
9
10
# File 'lib/table_me/filter.rb', line 8

def column_name
  @column_name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/table_me/filter.rb', line 8

def options
  @options
end

Class Method Details

.filters_for(table_name) ⇒ Object

getter for all the filters



57
58
59
# File 'lib/table_me/filter.rb', line 57

def self.filters_for table_name
  @@filters[table_name]
end

.initObject



12
13
14
# File 'lib/table_me/filter.rb', line 12

def self.init
  @@filters = {}
end

Instance Method Details

#displayObject

Display the filter form



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/table_me/filter.rb', line 28

def display
  initial_value = options[:search] && options[:search][:column] == column_name.to_s ? options[:search][:query] : ''
  <<-HTML.strip_heredoc
    <form method='get' action="?">
      <label for='search'>#{column_name.to_s.split('_').join(" ").titleize}</label>
      <input type='text' name="tm_#{options[:name]}[search][query]" value="#{initial_value}"/>
      <input type='hidden' name="tm_#{options[:name]}[search][column]" value="#{column_name}"/>
      <input type='hidden' name="tm_#{options[:name]}[new_search]" value="true"/>
      #{create_other_fields options}
      <input id='search' type='submit' value='Search' />
    </form>
  HTML

end

#display_clearObject

display a clear filters button, this clears the filters if any are active. This could/should be just a link styled like a button, there really isn’t a need for it to be a form TODO Change this into a link instead of a form



47
48
49
50
51
52
53
54
# File 'lib/table_me/filter.rb', line 47

def display_clear
  <<-HTML.strip_heredoc if options[:search]
    <form method='get' action="?">
      #{create_other_fields options}
      <input id='search' type='submit' value='Clear Filter' />
    </form>
  HTML
end