tableficate 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/README.markdown +53 -48
  2. data/app/views/tableficate/{_column_header.html.erb → _header.html.erb} +0 -0
  3. data/app/views/tableficate/_table.html.erb +4 -4
  4. data/app/views/tableficate/filters/_check_box.html.erb +4 -0
  5. data/app/views/tableficate/{_input_filter.html.erb → filters/_input.html.erb} +0 -0
  6. data/app/views/tableficate/{_input_range_filter.html.erb → filters/_input_range.html.erb} +0 -0
  7. data/app/views/tableficate/filters/_radio.html.erb +4 -0
  8. data/app/views/tableficate/{_select_filter.html.erb → filters/_select.html.erb} +0 -0
  9. data/app/views/tableficate/filters/_select_range.html.erb +6 -0
  10. data/changelog.markdown +8 -0
  11. data/lib/tableficate/action_column.rb +2 -2
  12. data/lib/tableficate/base.rb +2 -2
  13. data/lib/tableficate/column.rb +6 -2
  14. data/lib/tableficate/exceptions.rb +4 -0
  15. data/lib/tableficate/filters/base.rb +21 -0
  16. data/lib/tableficate/filters/check_box.rb +6 -0
  17. data/lib/tableficate/filters/choice.rb +23 -0
  18. data/lib/tableficate/filters/collection.rb +30 -0
  19. data/lib/tableficate/filters/input.rb +12 -0
  20. data/lib/tableficate/filters/input_range.rb +57 -0
  21. data/lib/tableficate/filters/radio.rb +6 -0
  22. data/lib/tableficate/filters/select.rb +6 -0
  23. data/lib/tableficate/filters/select_range.rb +54 -0
  24. data/lib/tableficate/helper.rb +69 -4
  25. data/lib/tableficate/table.rb +56 -10
  26. data/lib/tableficate/version.rb +1 -1
  27. data/lib/tableficate.rb +9 -3
  28. data/spec/action_column_spec.rb +1 -1
  29. data/spec/column_spec.rb +8 -0
  30. data/spec/filters/base_spec.rb +30 -0
  31. data/spec/filters/choice_spec.rb +13 -0
  32. data/spec/filters/collection_spec.rb +94 -0
  33. data/spec/filters/input_range_spec.rb +91 -0
  34. data/spec/filters/input_spec.rb +24 -0
  35. data/spec/generators/tableficate_table_spec.rb +20 -0
  36. data/spec/generators/tableficate_theme_spec.rb +11 -0
  37. data/spec/helper_spec.rb +164 -0
  38. data/spec/spec_helper.rb +3 -0
  39. data/spec/table_spec.rb +38 -13
  40. data/spec/test_app/Gemfile +1 -1
  41. data/spec/test_app/app/controllers/filters_controller.rb +7 -0
  42. data/spec/test_app/app/views/filters/check_box_tags.html.erb +14 -0
  43. data/spec/test_app/app/views/filters/multi_select_from_string.html.erb +11 -0
  44. data/spec/test_app/app/views/filters/multi_select_tag.html.erb +11 -0
  45. data/spec/test_app/app/views/filters/radio_tags.html.erb +14 -0
  46. data/spec/test_app/app/views/filters/select_from_string.html.erb +11 -0
  47. data/spec/test_app/app/views/filters/select_tag.html.erb +11 -0
  48. data/spec/test_app/app/views/tableficate/custom_check_box_block/_data.html.erb +1 -0
  49. data/spec/test_app/app/views/tableficate/custom_check_box_block/_header.html.erb +8 -0
  50. data/spec/test_app/app/views/tableficate/custom_check_box_block/_row.html.erb +5 -0
  51. data/spec/test_app/app/views/tableficate/custom_check_box_block/_table.html.erb +36 -0
  52. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_check_box.html.erb +7 -0
  53. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input.html.erb +4 -0
  54. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input_range.html.erb +6 -0
  55. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_radio.html.erb +4 -0
  56. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select.html.erb +4 -0
  57. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select_range_filter.html.erb +6 -0
  58. data/spec/test_app/app/views/tableficate/custom_radio_block/_data.html.erb +1 -0
  59. data/spec/test_app/app/views/tableficate/custom_radio_block/_header.html.erb +8 -0
  60. data/spec/test_app/app/views/tableficate/custom_radio_block/_row.html.erb +5 -0
  61. data/spec/test_app/app/views/tableficate/custom_radio_block/_table.html.erb +36 -0
  62. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_check_box.html.erb +4 -0
  63. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input.html.erb +4 -0
  64. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input_range.html.erb +6 -0
  65. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_radio.html.erb +7 -0
  66. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select.html.erb +4 -0
  67. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select_range_filter.html.erb +6 -0
  68. data/spec/test_app/config/routes.rb +1 -56
  69. data/tableficate.gemspec +4 -2
  70. metadata +122 -27
  71. data/lib/tableficate/filters/filter.rb +0 -19
  72. data/lib/tableficate/filters/input_filters.rb +0 -64
  73. data/lib/tableficate/filters/select_filter.rb +0 -11
  74. data/spec/filter_spec.rb +0 -30
  75. data/spec/input_filter_spec.rb +0 -90
  76. data/spec/select_filter_spec.rb +0 -10
data/README.markdown CHANGED
@@ -1,5 +1,7 @@
1
1
  # Tableficate
2
- A DSL for easy table creation with sorting and filtering. It tries to do to tables what simple_form and formtastic have done for forms. Below you'll find basic documentation with more compelete docs forthcoming.
2
+ A DSL for easy table creation with sorting and filtering. It tries to do to tables what simple_form and formtastic have done for forms. Below you'll find basic documentation with more compelete docs in the [wiki](https://github.com/sei-mi/tableficate/wiki).
3
+
4
+ This project follows [Semantic Versioning](http://semver.org/).
3
5
 
4
6
  ## Installation
5
7
 
@@ -7,7 +9,7 @@ A DSL for easy table creation with sorting and filtering. It tries to do to tabl
7
9
 
8
10
  If you're using Bundler, add this to your Gemfile:
9
11
 
10
- gem 'tableficate'
12
+ gem 'tableficate', '~0.2.0'
11
13
 
12
14
  ## Support
13
15
 
@@ -33,45 +35,43 @@ Our controller:
33
35
 
34
36
  @accounts = Accounts.active.tableficate(params[:accounts]).page(params.try(:[], :accounts_page) || 1)
35
37
 
36
- Here we're getting active accounts, calling tableficate and then paginating with Kaminari. Tableficate works on Arel objects and passes out an Arel so you can add things like pagination. We pass in the params for the table we'll create in the view. By default the params are stored under the primary table name of the query.
38
+ Here we're getting active accounts, calling tableficate and then paginating with Kaminari. Tableficate works on Arel objects and passes out an Arel so you can add things like pagination. We pass in the params for the table we'll create in the view. By default `:accounts` is derived from the database table name of the primary table in the query.
37
39
 
38
40
  Our View:
39
41
 
40
- <%= table_for @accounts, show_sorts: true do |t|
41
- t.column :id, header: 'Account Number'
42
- t.column :created_at do |account|
43
- account.created_at.strftime('%m/%d/%Y')
44
- end
45
- t.column :first_name
46
- t.column :last_name
47
- t.actions do |account|
48
- link_to('View', account_path(account))
49
- end
42
+ <%= table_for @accounts, show_sorts: true do |t| %>
43
+ <% t.column :id, header: 'Account Number' %>
44
+ <% t.column :created_at do |account| %>
45
+ <%= account.created_at.strftime('%m/%d/%Y') %>
46
+ <% end %>
47
+ <% t.column :first_name %>
48
+ <% t.column :last_name %>
49
+ <% t.actions do |account| %>
50
+ <%= link_to('View', account_path(account)) %>
51
+ <% end %>
50
52
 
51
- t.input_filter :first_name
52
- t.input_filter :last_name
53
- %>
53
+ <% t.filter :first_name %>
54
+ <% t.filter :last_name %>
55
+ <% end %>
54
56
 
55
- This creates a sortable table with two filters, 4 data columns and an action column. Column headers are automatically generated but can be overridden as seen on the "id" column. The column output can also be overridden by passing a block to the call. This setup provides easy table creation but only covers basic functionality. Some of the more advanced functionality requires you to wrap your scope in a special table model.
57
+ This creates a sortable table with 2 filters, 4 data columns and a column for actions. Column headers are automatically generated but can be overridden as seen on `:id`. The column output can also be overridden by passing a block to the call. This setup only covers basic functionality. Some of the more advanced functionality requires you to wrap your scope in a special table model.
56
58
 
57
- Having created the basic table we now want to default the sorting to show newest accounts first and we want the first and last name to be merged into one name column. First we'll need to create a table model.
59
+ Having created the basic table we now want to default the sorting to show newest accounts first and we want `:first_name` and `:last_name` to be merged into one column. First, create a table model.
58
60
 
59
- In "app/tables/" we'll create a new table model. This can be done using a generator.
61
+ In "app/tables/" we'll create a new table model using the generator. Provide the name of the new table model and then the model being wrapped.
60
62
 
61
- $ rails generate tableficate:table AccountReport
63
+ $ rails generate tableficate:table AccountReport Account
62
64
 
63
65
  Our table model:
64
66
 
65
67
  class AccountReport < Tableficate::Base
66
- scope do
67
- Accounts.active
68
- end
68
+ scope :accounts
69
69
 
70
- default_sort(:created_at, 'DESC')
70
+ default_sort :created_at, 'DESC'
71
71
 
72
- column(:full_name, sort: 'first_name ASC, last_name ASC')
72
+ column :full_name, sort: 'first_name ASC, last_name ASC'
73
73
 
74
- filter(:full_name) do |value, scope|
74
+ filter :full_name do |value, scope|
75
75
  first_name, last_name = value.split(/\s+/)
76
76
 
77
77
  if last_name.nil?
@@ -82,39 +82,44 @@ Our table model:
82
82
  end
83
83
  end
84
84
 
85
- We've defined a scope that we're wrapping. Then we provide a default sorting and explain how to sort and filter a new column called ":full_name".
85
+ We've defined a scope that we're wrapping. Then we provide a default sorting and explain how to sort and filter a new column called `:full_name`.
86
86
 
87
- Our new controller:
87
+ Our new controller using `AccountReport`:
88
88
 
89
- @accounts = AccountReport.tableficate(params[:accounts]).page(params.try(:[], :accounts_page) || 1)
89
+ @accounts = AccountReport.active.tableficate(params[:accounts]).page(params.try(:[], :accounts_page) || 1)
90
90
 
91
- The only change here is that "Accounts.active" has changed to "AccountReport".
91
+ Our new view using `:full_name`:
92
92
 
93
- Our new view:
93
+ <%= table_for @accounts, show_sorts: true do |t| %>
94
+ <% t.column :id, header: 'Account Number' %>
95
+ <% t.column :created_at do |account| %>
96
+ <%= account.created_at.strftime('%m/%d/%Y') %>
97
+ <% end %>
98
+ <% t.column :full_name do |account| %>
99
+ <%= "#{account.first_name} #{account.last_name}" %>
100
+ <% end %>
101
+ <% t.actions do |account| %>
102
+ <%= link_to('View', account_path(account)) %>
103
+ <% end %>
94
104
 
95
- <%= table_for @accounts, show_sorts: true do |t|
96
- t.column :id, header: 'Account Number'
97
- t.column :created_at do |account|
98
- account.created_at.strftime('%m/%d/%Y')
99
- end
100
- t.column :full_name
101
- t.actions do |account|
102
- link_to('View', account_path(account))
103
- end
104
-
105
- t.input_filter :full_name
106
- %>
105
+ <% t.filter :full_name %>
106
+ <% end %>
107
107
 
108
- In the view we've merged the first and last name into a new full name column. Now we have default sorting, sortable columns and a full name filter.
108
+ Now we have default sorting, sortable columns and a full name filter.
109
109
 
110
110
  ## Themes
111
111
 
112
112
  New themes can be created using the theme generator.
113
113
 
114
- $ rails generate tableficate:theme NAME
114
+ $ rails generate tableficate:theme foo
115
115
 
116
- The theme can then be applied to a table by passing "theme: 'NAME'" to the "table_for" call.
116
+ The theme can then be applied to a table.
117
117
 
118
- <%= table_for @accounts, theme: 'foo' do |t|
118
+ <%= table_for @records, theme: 'foo' do |t| %>
119
119
  ...
120
- %>
120
+ <% end %>
121
+
122
+ ## Changes Needed to Upgrade From 0.1
123
+
124
+ 1. The filter functions used in the `table_for` call have been completely changed. They will need to be rewritten in all of your calls.
125
+ 2. New filter partials have been added to accomodate the new filter types that are available. If you have custom themes they will need to be updated. This can be done by first moving "_column_header.html.erb" to "_header.html.erb", "filters/_input_field.html.erb" to "filters/_input.html.erb" and "filters/_input_field_range.html.erb" to "filters/_input_range.html.erb". Then rerun `rails generate tableficate:theme NAME`. This will not overwrite files you have altered for your new theme.
@@ -1,5 +1,5 @@
1
1
  <% if table.filters.any? %>
2
- <%= form_tag '', method: :get do %>
2
+ <%= form_tag('', method: :get, novalidate: 'novalidate') do %>
3
3
  <% if params[table.as] and params[table.as][:sort] %>
4
4
  <%= hidden_field_tag("#{table.as}[sort]", params[table.as][:sort]) %>
5
5
  <% end %>
@@ -7,7 +7,7 @@
7
7
  <%= hidden_field_tag("#{table.as}[dir]", params[table.as][:dir]) %>
8
8
  <% end %>
9
9
  <% table.filters.each do |filter| %>
10
- <%= tableficate_filter_tag filter %>
10
+ <%= tableficate_filter_tag(filter) %>
11
11
  <% end %>
12
12
 
13
13
  <%= submit_tag('Filter') %>
@@ -24,13 +24,13 @@
24
24
  <thead>
25
25
  <tr>
26
26
  <% table.columns.each do |column| %>
27
- <%= tableficate_header_tag column %>
27
+ <%= tableficate_header_tag(column) %>
28
28
  <% end %>
29
29
  </tr>
30
30
  </thead>
31
31
  <tbody>
32
32
  <% table.rows.each do |row| %>
33
- <%= tableficate_row_tag row, table.columns %>
33
+ <%= tableficate_row_tag(row, table.columns) %>
34
34
  <% end %>
35
35
  </tbody>
36
36
  <% end %>
@@ -0,0 +1,4 @@
1
+ <div>
2
+ <%= tableficate_label_tag(filter) %>
3
+ <%= tableficate_check_box_tags(filter) %>
4
+ </div>
@@ -0,0 +1,4 @@
1
+ <div>
2
+ <%= tableficate_label_tag(filter) %>
3
+ <%= tableficate_radio_tags(filter) %>
4
+ </div>
@@ -0,0 +1,6 @@
1
+ <div>
2
+ <%= tableficate_label_tag(filter.start) %>
3
+ <%= tableficate_select_tag(filter.start) %>
4
+ <%= tableficate_label_tag(filter.stop) %>
5
+ <%= tableficate_select_tag(filter.stop) %>
6
+ </div>
data/changelog.markdown CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.2.0
2
+ * complete filter rewrite
3
+ * replaced existing filter functions in table_for calls
4
+ * added select based range filter
5
+ * added radio button filter
6
+ * added check box filter
7
+ * `column` and `actions` calls in `table_for` now accept template blocks
8
+
1
9
  ## 0.1.3
2
10
  * fixing html_safe break in 0.1.2
3
11
 
@@ -1,7 +1,7 @@
1
1
  module Tableficate
2
2
  class ActionColumn < Column
3
- def initialize(table, options = {}, block)
4
- super(table, '', options, &block)
3
+ def initialize(table, &block)
4
+ super(table, '', {}, &block)
5
5
  end
6
6
 
7
7
  def show_sort?
@@ -2,9 +2,9 @@ module Tableficate
2
2
  class Base
3
3
  extend Tableficate::Finder
4
4
 
5
- def self.scope(model = nil, &block)
5
+ def self.scope(model = nil)
6
6
  if block_given?
7
- @scope = block.call
7
+ @scope = yield
8
8
  else
9
9
  @scope = model.to_s.camelize.constantize
10
10
  end
@@ -14,8 +14,12 @@ module Tableficate
14
14
  def value(row)
15
15
  if @block
16
16
  output = @block.call(row)
17
- output = output.html_safe if output.respond_to? :html_safe
18
- output
17
+ if output.is_a?(ActionView::OutputBuffer)
18
+ ''
19
+ else
20
+ output = output.html_safe if output.respond_to? :html_safe
21
+ output
22
+ end
19
23
  else
20
24
  row.send(@name)
21
25
  end
@@ -1,3 +1,7 @@
1
1
  module Tableficate
2
2
  class MissingScope < StandardError; end
3
+
4
+ module Filter
5
+ class UnknownInputType < StandardError; end
6
+ end
3
7
  end
@@ -0,0 +1,21 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Base
4
+ attr_reader :name, :label, :options, :template, :table, :field_name
5
+
6
+ def initialize(table, name, options = {})
7
+ @table = table
8
+ @name = name
9
+ @options = options
10
+
11
+ @template = 'filters/' + self.class.name.demodulize.underscore
12
+ @label = @options.delete(:label) || table.columns.detect{|column| column.name == @name}.try(:header) || name.to_s.titleize
13
+ @field_name = "#{table.as}[filter][#{@name}]"
14
+ end
15
+
16
+ def field_value(params)
17
+ params[:filter][@name] rescue ''
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ module Tableficate
2
+ module Filter
3
+ class CheckBox < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Choice
4
+ attr_reader :name, :value, :options
5
+
6
+ def initialize(name, value, options = {})
7
+ @name = name
8
+ @value = value
9
+
10
+ @selected = !!(options.has_key?(:selected) || options.has_key?(:checked))
11
+ options.delete(:selected)
12
+ options.delete(:checked)
13
+
14
+ @options = options
15
+ end
16
+
17
+ def selected?
18
+ @selected
19
+ end
20
+ alias_method :checked?, :selected?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Collection < Array
4
+ def initialize(container, options = {})
5
+ container = container.to_a if container.is_a?(Range)
6
+
7
+ selected = Array.wrap(options[:selected]).map{|item| item.to_s}
8
+ disabled = Array.wrap(options[:disabled]).map{|item| item.to_s}
9
+
10
+ container.map do |element|
11
+ text, value, choice_options = case
12
+ when Array === element
13
+ html_attrs = element.detect {|e| Hash === e} || {}
14
+ element = element.reject {|e| Hash === e}
15
+ [element.first, element.last, html_attrs]
16
+ when !element.is_a?(String) && element.respond_to?(:first) && element.respond_to?(:last)
17
+ [element.first, element.last, {}]
18
+ else
19
+ [element, element, {}]
20
+ end
21
+
22
+ choice_options[:selected] = 'selected' if selected.include?(value.to_s)
23
+ choice_options[:disabled] = 'disabled' if disabled.include?(value.to_s)
24
+
25
+ self.push(Choice.new(text, value, choice_options))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Input < Base
4
+ def initialize(table, name, options = {})
5
+ super(table, name, options)
6
+
7
+ new_template = "filters/input_#{options[:type]}"
8
+ @template = new_template if table.template.lookup_context.exists?(Tableficate::Utils::template_path(new_template, table.options[:theme]), [], true)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+ module Tableficate
2
+ module Filter
3
+ class InputStart < Input
4
+ def initialize(table, name, options = {})
5
+ super(table, name, options)
6
+
7
+ @field_name += '[start]'
8
+ end
9
+
10
+ def name
11
+ "#{@name}_start".to_sym
12
+ end
13
+
14
+ def field_value(params)
15
+ params[:filter][@name][:start] rescue ''
16
+ end
17
+ end
18
+
19
+ class InputStop < Input
20
+ def initialize(table, name, options = {})
21
+ super(table, name, options)
22
+
23
+ @field_name += '[stop]'
24
+ end
25
+
26
+ def name
27
+ "#{@name}_stop".to_sym
28
+ end
29
+
30
+ def field_value(params)
31
+ params[:filter][@name][:stop] rescue ''
32
+ end
33
+ end
34
+
35
+ class InputRange < Base
36
+ attr_reader :start, :stop
37
+
38
+ def initialize(table, name, options = {})
39
+ start_options = options.delete(:start) || {}
40
+ stop_options = options.delete(:stop) || {}
41
+
42
+ super(table, name, options)
43
+
44
+ start_options.reverse_merge!(@options)
45
+ start_options.reverse_merge!(label: self.label)
46
+ stop_options.reverse_merge!(@options)
47
+ stop_options.reverse_merge!(label: self.label)
48
+
49
+ @start = InputStart.new(table, name, start_options)
50
+ @stop = InputStop.new(table, name, stop_options)
51
+
52
+ new_template = "filters/input_range_#{options[:type]}"
53
+ @template = new_template if table.template.lookup_context.exists?(Tableficate::Utils::template_path(new_template, table.options[:theme]), [], true)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,6 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Radio < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Tableficate
2
+ module Filter
3
+ class Select < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,54 @@
1
+ module Tableficate
2
+ module Filter
3
+ class SelectStart < Select
4
+ def initialize(table, name, options = {})
5
+ super(table, name, options)
6
+
7
+ @field_name += '[start]'
8
+ end
9
+
10
+ def name
11
+ "#{@name}_start".to_sym
12
+ end
13
+
14
+ def field_value(params)
15
+ params[:filter][@name][:start] rescue ''
16
+ end
17
+ end
18
+
19
+ class SelectStop < Select
20
+ def initialize(table, name, options = {})
21
+ super(table, name, options)
22
+
23
+ @field_name += '[stop]'
24
+ end
25
+
26
+ def name
27
+ "#{@name}_stop".to_sym
28
+ end
29
+
30
+ def field_value(params)
31
+ params[:filter][@name][:stop] rescue ''
32
+ end
33
+ end
34
+
35
+ class SelectRange < Base
36
+ attr_reader :start, :stop
37
+
38
+ def initialize(table, name, options = {})
39
+ start_options = options.delete(:start) || {}
40
+ stop_options = options.delete(:stop) || {}
41
+
42
+ super(table, name, options)
43
+
44
+ start_options.reverse_merge!(@options)
45
+ start_options.reverse_merge!(label: self.label)
46
+ stop_options.reverse_merge!(@options)
47
+ stop_options.reverse_merge!(label: self.label)
48
+
49
+ @start = SelectStart.new(table, name, start_options)
50
+ @stop = SelectStop.new(table, name, stop_options)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -7,7 +7,7 @@ module Tableficate
7
7
  end
8
8
 
9
9
  def tableficate_header_tag(column)
10
- render partial: Tableficate::Utils::template_path('column_header', column.table.options[:theme]), locals: {column: column}
10
+ render partial: Tableficate::Utils::template_path('header', column.table.options[:theme]), locals: {column: column}
11
11
  end
12
12
 
13
13
  def tableficate_data_tag(row, column)
@@ -23,15 +23,80 @@ module Tableficate
23
23
  end
24
24
 
25
25
  def tableficate_label_tag(filter)
26
- label_tag(filter.field_name, filter.label, filter.options[:label_options] || {})
26
+ label_tag(filter.field_name, filter.label, filter.options[:label_options])
27
27
  end
28
28
 
29
29
  def tableficate_text_field_tag(filter)
30
- text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options[:field_options] || {})
30
+ text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options)
31
31
  end
32
32
 
33
33
  def tableficate_select_tag(filter)
34
- select_tag(filter.field_name, filter.choices, filter.options)
34
+ field_value = filter.field_value(params[filter.table.as])
35
+
36
+ if field_value.present? and filter.options[:collection].is_a?(String)
37
+ Array.wrap(field_value).each do |fv|
38
+ if filter.options[:collection].match(/<option[^>]*value\s*=/)
39
+ filter.options[:collection].gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"')
40
+ else
41
+ filter.options[:collection].gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<")
42
+ end
43
+ end
44
+ elsif not filter.options[:collection].is_a?(String)
45
+ filter.options[:collection] = Tableficate::Filter::Collection.new(filter.options[:collection], selected: field_value).map {|choice|
46
+ html_attributes = choice.options.length > 0 ? ' ' + choice.options.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : ''
47
+ selected_attribute = choice.selected? ? ' selected="selected"' : ''
48
+
49
+ %(<option value="#{ERB::Util.html_escape(choice.value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(choice.name)}</option>)
50
+ }.join("\n")
51
+ end
52
+
53
+ filter.options[:collection] = filter.options[:collection].html_safe
54
+
55
+ select_tag(filter.field_name, filter.options.delete(:collection), filter.options)
56
+ end
57
+
58
+ def tableficate_radio_tags(filter, &block)
59
+ field_value = filter.field_value(params[filter.table.as])
60
+
61
+ collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))
62
+
63
+ html = []
64
+ if block_given?
65
+ html = collection.map {|choice| capture(choice, &block)}
66
+ else
67
+ collection.each do |choice|
68
+ html.push(
69
+ radio_button_tag(filter.field_name, choice.value, choice.checked?, choice.options),
70
+ label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
71
+ '<br/>'
72
+ )
73
+ end
74
+ end
75
+
76
+ html.join("\n").html_safe
77
+ end
78
+
79
+ def tableficate_check_box_tags(filter, &block)
80
+ field_value = filter.field_value(params[filter.table.as])
81
+
82
+ collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))
83
+
84
+ html = []
85
+ if block_given?
86
+ html = collection.map {|choice| capture(choice, &block)}
87
+ else
88
+ collection.each do |choice|
89
+ html.push(
90
+ check_box_tag(
91
+ "#{filter.field_name}[#{choice.value}]", choice.value, choice.checked?, choice.options.reverse_merge(name: "#{filter.field_name}[]")
92
+ ),
93
+ label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
94
+ '<br/>'
95
+ )
96
+ end
97
+ end
98
+
99
+ html.join("\n").html_safe
35
100
  end
36
101
  end
37
102
  end