Class: TableMe::Filter
- Inherits:
-
Object
- Object
- TableMe::Filter
- 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
-
#column_name ⇒ Object
Returns the value of attribute column_name.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.filters_for(table_name) ⇒ Object
getter for all the filters.
- .init ⇒ Object
Instance Method Summary collapse
-
#display ⇒ Object
Display the filter form.
-
#display_clear ⇒ Object
display a clear filters button, this clears the filters if any are active.
-
#initialize(options, column_name) ⇒ Filter
constructor
A new instance of Filter.
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 , column_name self. = self.column_name = column_name @@filters[[:name]] ||= [] @@filters[[:name]].delete_if {|item| item.column_name == column_name} @@filters[[:name]] << self end |
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name.
8 9 10 |
# File 'lib/table_me/filter.rb', line 8 def column_name @column_name end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/table_me/filter.rb', line 8 def @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 |
.init ⇒ Object
12 13 14 |
# File 'lib/table_me/filter.rb', line 12 def self.init @@filters = {} end |
Instance Method Details
#display ⇒ Object
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 = [:search] && [:search][:column] == column_name.to_s ? [: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_#{[:name]}[search][query]" value="#{initial_value}"/> <input type='hidden' name="tm_#{[:name]}[search][column]" value="#{column_name}"/> <input type='hidden' name="tm_#{[:name]}[new_search]" value="true"/> #{create_other_fields } <input id='search' type='submit' value='Search' /> </form> HTML end |
#display_clear ⇒ Object
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 [:search] <form method='get' action="?"> #{create_other_fields } <input id='search' type='submit' value='Clear Filter' /> </form> HTML end |