tabulatr2 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7714cdb5e97ef698c0207c832c9778f107f01aed
4
- data.tar.gz: 6193fd43909b6a071223e54c78e628ccb7fcbec3
3
+ metadata.gz: f6cc8ca903e127f8312818a5a85faf7a01fb7b85
4
+ data.tar.gz: 90df54794f6c5699ac87809a3513ed5845fa34f5
5
5
  SHA512:
6
- metadata.gz: 88d9ba05b6785fe460e33266274b13acdb0231db38c29cbc4306492f18d6ed3ed8d5eef47094e9cfbb60bb39693ae1d5090e5556d9eb641a280b22ecab4b39de
7
- data.tar.gz: 0ee34fdbaa64d8693333ba27d4cd14ebcc1bfc88117f42635d055871a3e4b77d7d996c449c28f0319b592f7aa0b57f8256f50417111142060a1d834bfe1e24b0
6
+ metadata.gz: 6c4ce4dfcb2f5caf2e57a9c89b0a380989cc73112a074dceb6055837844817fb1271f0b10f4cab36c89d9f8e80d282a94d401b0d7b14785f061f3b352423b047
7
+ data.tar.gz: 66121f0ea6dc15b810fbaa31bc86385afd96f5a9632889a0a5ffe44f288a0a02981d48f4e6dccf45b1d524b6ec4eb483a78e83b717775a0c77646a5ebfce567b
data/.travis.yml CHANGED
@@ -1,5 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - '2.0.0'
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - jruby
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: jruby
4
9
  script: "bundle exec rspec spec"
5
10
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 0.9.3
2
+ * Tabulatr is now stopped from retrieving data in endless pagination mode when all data
3
+ is already on the client.
4
+
5
+ * Automatically determine appropriate filter types for the columns
6
+
7
+ * Boolean filter is now a `select` field with `yes`, `no`, `both` options
8
+
9
+ * User can pass in additional variables to the TabulatrData-Class via the
10
+ `locals` argument.
11
+
12
+ Example:
13
+ ```
14
+ tabulatr_for Product, locals: {current_user: current_user}
15
+ ```
16
+
1
17
  ## 0.9.2
2
18
 
3
19
  * Fixed bug which caused a reload of the website when clicking on the current
data/Gemfile CHANGED
@@ -2,14 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'tabulatr2', path: './'
6
-
7
5
  gem 'jquery-rails'
8
6
  gem 'slim'
9
7
 
10
8
  group :development do
11
9
  gem 'better_errors'
12
- gem 'binding_of_caller'
10
+ gem 'binding_of_caller', platforms: :ruby
13
11
  end
14
12
 
15
13
 
data/README.md CHANGED
@@ -226,8 +226,6 @@ the columns in the block of `table_for`.
226
226
  th_html: false,
227
227
  filter_html: false,
228
228
  filter: true, # whether this column should be filterable
229
- checkbox_value: '1',
230
- checkbox_label: '',
231
229
  sortable: true, # whethter this column should be sortable
232
230
  format: nil,
233
231
  map: true,
@@ -130,6 +130,10 @@ Tabulatr.prototype = {
130
130
  return $('tr[data-page] input[type=checkbox]:checked').length > 0;
131
131
  },
132
132
 
133
+ currentCount: function(){
134
+ return $('#'+ this.id +' tbody tr.tabulatr-row').length;
135
+ },
136
+
133
137
  handleResponse: function(response) {
134
138
  this.insertTabulatrData(response);
135
139
  this.updatePagination(response.meta.page, response.meta.pages, response.meta.table_id);
@@ -150,11 +154,9 @@ Tabulatr.prototype = {
150
154
  this.moreResults = false;
151
155
  $('.pagination_trigger[data-table='+ tableId +']').unbind('inview');
152
156
  }else{
153
- if(response.data.length < response.meta.pagesize){
157
+ if(this.currentCount() + response.data.length >= response.meta.count){
154
158
  this.moreResults = false;
155
159
  $('.pagination_trigger[data-table='+ tableId + ']').unbind('inview');
156
- }else{
157
- this.moreResults = true;
158
160
  }
159
161
 
160
162
  // insert the actual data
@@ -47,8 +47,12 @@ form.form.form-horizontal.tabulatr_filter_form data-table=table_id data-remote=
47
47
  .col-md-5
48
48
  input.tabulatr_filter.form-control type="text" id="#{name}_to" data-tabulatr-attribute="#{name}_to" name="#{iname}[to]"
49
49
  - elsif column.filter == :checkbox
50
- = check_box_tag(iname, column.checkbox_value, false, {})
51
- = column.checkbox_label
50
+ .col-md-11
51
+ = select_tag(iname, options_for_select([ \
52
+ [t('tabulatr.boolean_filter.both'), ""],
53
+ [t('tabulatr.boolean_filter.no'), "false"],
54
+ [t('tabulatr.boolean_filter.yes'), "true"]] \
55
+ ), class: 'form-control')
52
56
  - elsif column.filter == :exact
53
57
  .col-md-11
54
58
  input.tabulatr_filter.form-control type="text" id="#{name}_exact" data-tabulatr-attribute="#{name}" name="#{iname}"
@@ -19,5 +19,5 @@
19
19
  / OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  / WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- div.tabulatr_count data-table="#{klass.to_s.gsub(/::/, '--').downcase}_table" data-format-string=I18n.t('tabulatr.count')
22
+ div.tabulatr_count data-table=table_id data-format-string=I18n.t('tabulatr.count')
23
23
 
@@ -19,8 +19,6 @@
19
19
  / OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  / WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- - formatted_name = "#{klass.to_s.gsub(/::/, '--').downcase}"
23
- - table_id = "#{formatted_name}_table"
24
22
  - opts = { columns: columns, table_options: table_options, \
25
23
  klass: klass, classname: classname, table_id: table_id,
26
24
  formatted_name: formatted_name }
@@ -35,11 +35,12 @@ class Tabulatr::Data
35
35
  @batch_actions = nil
36
36
  @row = self.class.instance_variable_get('@row')
37
37
  table_columns.map do |col|
38
- col.klass = @base
38
+ col.klass = @base.reflect_on_association(col.table_name).try(:klass) || @base
39
+ col.determine_appropriate_filter! if col.filter === true
39
40
  end
40
41
  end
41
42
 
42
- def data_for_table(params, &block)
43
+ def data_for_table(params, locals: {}, &block)
43
44
 
44
45
  @batch_actions = block if block_given?
45
46
 
@@ -60,11 +61,10 @@ class Tabulatr::Data
60
61
  # TODO: batch actions and checked ids
61
62
 
62
63
  # get the records
63
- found = apply_formats()
64
+ found = apply_formats(locals: locals)
64
65
 
65
66
  append = params[:append].present? ? Tabulatr::Utility.string_to_boolean(params[:append]) : false
66
67
 
67
-
68
68
  # prepare result for rendering
69
69
  found.define_singleton_method(:__pagination) do
70
70
  { :page => pagination[:page],
@@ -62,7 +62,9 @@ module Tabulatr::Data::Filtering
62
62
  end
63
63
 
64
64
  def apply_condition(n,v)
65
- if v.is_a?(String)
65
+ if ['true', 'false'].include?(v)
66
+ @relation = @relation.where(:"#{n}" => Tabulatr::Utility.string_to_boolean(v))
67
+ elsif v.is_a?(String)
66
68
  apply_string_condition("#{n} = ?", v)
67
69
  elsif v.is_a?(Hash)
68
70
  apply_hash_condition(n, v)
@@ -23,8 +23,8 @@
23
23
 
24
24
  module Tabulatr::Data::Formatting
25
25
 
26
- def apply_formats()
27
- view = Data::Proxy.new
26
+ def apply_formats(locals: {})
27
+ view = Data::Proxy.new(locals: locals)
28
28
  return @relation.map do |record|
29
29
  view.record = record
30
30
  h = HashWithIndifferentAccess.new
@@ -33,7 +33,6 @@ module Tabulatr::Data::Pagination
33
33
  pagesize, page = pagesize.to_i, page.to_i
34
34
 
35
35
  pages = (count/pagesize.to_f).ceil
36
- page = [page, pages].min
37
36
 
38
37
  {
39
38
  offset: [0,((page-1)*pagesize).to_i].max,
@@ -25,9 +25,13 @@ class Data::Proxy < ActionView::Base
25
25
 
26
26
  attr_accessor :record
27
27
 
28
- def initialize(record=nil)
28
+ def initialize(record=nil, locals: {})
29
29
  self.class._init
30
30
  @record = record
31
+ locals.each do |nam, val|
32
+ raise "cowardly refusing to override `#{nam}'" if respond_to? nam
33
+ define_singleton_method nam do val end
34
+ end
31
35
  end
32
36
 
33
37
  def self._init
@@ -22,11 +22,11 @@
22
22
  #++
23
23
 
24
24
  class ActionController::Base
25
- def tabulatr_for(relation, tabulatr_data_class: nil, serializer: nil, render_action: nil, &block)
25
+ def tabulatr_for(relation, tabulatr_data_class: nil, serializer: nil, render_action: nil, locals: {}, &block)
26
26
  klass = relation.respond_to?(:klass) ? relation.klass : relation
27
27
  respond_to do |format|
28
28
  format.json {
29
- records = klass.tabulatr(relation, tabulatr_data_class).data_for_table(params, &block)
29
+ records = klass.tabulatr(relation, tabulatr_data_class).data_for_table(params, locals: locals, &block)
30
30
  render json: records.to_tabulatr_json(serializer)
31
31
  }
32
32
  format.html {
@@ -23,9 +23,7 @@
23
23
 
24
24
  class Tabulatr::Renderer::Association < Tabulatr::Renderer::Column
25
25
  def human_name
26
- header ||
27
- klass.reflect_on_association(table_name.to_sym).klass.model_name.human + ' ' +
28
- klass.reflect_on_association(table_name.to_sym).klass.human_attribute_name(name)
26
+ header || klass.model_name.human + ' ' + klass.human_attribute_name(name)
29
27
  end
30
28
 
31
29
  def coltype() 'association' end
@@ -25,7 +25,7 @@ class Tabulatr::Renderer::Column
25
25
  include ActiveModel::Model
26
26
 
27
27
  attr_accessor *%i{name header width align valign wrap type th_html filter_html
28
- filter checkbox_value checkbox_label filter_width range_filter_symbol
28
+ filter filter_width range_filter_symbol
29
29
  sortable table_name block klass format map classes cell_style header_style}
30
30
 
31
31
  def self.from(
@@ -40,8 +40,6 @@ class Tabulatr::Renderer::Column
40
40
  th_html: false,
41
41
  filter_html: false,
42
42
  filter: true,
43
- checkbox_value: '1',
44
- checkbox_label: '',
45
43
  sortable: true,
46
44
  format: nil,
47
45
  map: true,
@@ -62,8 +60,6 @@ class Tabulatr::Renderer::Column
62
60
  th_html: th_html,
63
61
  filter_html: filter_html,
64
62
  filter: filter,
65
- checkbox_value: checkbox_value,
66
- checkbox_label: checkbox_label,
67
63
  sortable: sortable,
68
64
  format: format,
69
65
  map: map,
@@ -136,4 +132,17 @@ class Tabulatr::Renderer::Column
136
132
  record.send name
137
133
  end
138
134
 
135
+ def determine_appropriate_filter!
136
+ case self.klass.columns_hash[self.name.to_s].try(:type)
137
+ when :integer, :float, :decimal
138
+ self.filter = :exact
139
+ when :string, :text
140
+ self.filter = :like
141
+ when :date, :time, :datetime, :timestamp
142
+ self.filter = :date
143
+ when :boolean
144
+ self.filter = :checkbox
145
+ end
146
+ end
147
+
139
148
  end
@@ -64,7 +64,9 @@ class Tabulatr::Renderer
64
64
  table_options: @table_options,
65
65
  klass: @klass,
66
66
  classname: @classname,
67
- tabulatr_data: tdc
67
+ tabulatr_data: tdc,
68
+ table_id: generate_id,
69
+ formatted_name: formatted_name
68
70
  })
69
71
  end
70
72
 
@@ -80,6 +82,14 @@ class Tabulatr::Renderer
80
82
  })
81
83
  end
82
84
 
85
+ def generate_id
86
+ "#{formatted_name}_table_#{SecureRandom.uuid}"
87
+ end
88
+
89
+ def formatted_name
90
+ "#{@klass.to_s.gsub(/::/, '--').downcase}"
91
+ end
92
+
83
93
  def self.build_static_table(records, view, toptions={}, &block)
84
94
  return '' unless records.present?
85
95
  klass = records.first.class
@@ -22,5 +22,5 @@
22
22
  #++
23
23
 
24
24
  module Tabulatr
25
- VERSION = "0.9.2"
25
+ VERSION = "0.9.3"
26
26
  end
@@ -15,6 +15,10 @@ en:
15
15
  this_month: This Month
16
16
  last_30_days: Last 30 Days
17
17
  from_to: From - To
18
+ boolean_filter:
19
+ both: All
20
+ yes: Yes
21
+ no: No
18
22
 
19
23
  de:
20
24
  tabulatr:
@@ -33,3 +37,7 @@ de:
33
37
  this_month: Dieser Monat
34
38
  last_30_days: Letzte 30 Tage
35
39
  from_to: Von - Bis
40
+ boolean_filter:
41
+ both: Alle
42
+ yes: Ja
43
+ no: Nein
@@ -123,8 +123,8 @@ describe "Tabulatr" do
123
123
  end
124
124
  end
125
125
 
126
- describe "Filters", js: true do
127
- it "filters with like" do
126
+ describe "Filters" do
127
+ it "filters with like", js: true do
128
128
  names.each do |n|
129
129
  Product.create!(:title => n, :active => true, :price => 10.0)
130
130
  end
@@ -137,22 +137,23 @@ describe "Tabulatr" do
137
137
  find(".tabulatr-filter-menu-wrapper a.btn").trigger('click')
138
138
  within(".tabulatr_filter_form") do
139
139
  fill_in("product_filter[title][like]", with: "ore")
140
- expect(find('#title_like').visible?)
141
- click_button("Apply")
140
+ expect{find('#title_like').visible?}.to be_true
141
+ find_button("Apply").trigger('click')
142
142
  end
143
- page.should have_content("lorem")
144
- page.should have_content("labore")
145
- page.should have_content("dolore")
143
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'lorem')
144
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'labore')
145
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'dolore')
146
+
146
147
  within(".tabulatr_filter_form") do
147
148
  fill_in("product_filter[title][like]", :with => "loreem")
148
- click_button("Apply")
149
+ find_button("Apply").trigger('click')
149
150
  end
150
- page.should_not have_content("lorem")
151
- page.should_not have_content("labore")
152
- page.should_not have_content("dolore")
151
+ expect(page).not_to have_selector('td[data-tabulatr-column-name="title"]', text: 'lorem')
152
+ expect(page).not_to have_selector('td[data-tabulatr-column-name="title"]', text: 'labore')
153
+ expect(page).not_to have_selector('td[data-tabulatr-column-name="title"]', text: 'dolore')
153
154
  end
154
155
 
155
- it "filters" do
156
+ it "filters", js: true do
156
157
  Product.create!([{title: 'foo', vendor: @vendor1},
157
158
  {title: 'bar', vendor: @vendor2}])
158
159
  visit simple_index_products_path
@@ -161,12 +162,10 @@ describe "Tabulatr" do
161
162
  find_link('Vendor Name').click
162
163
  end
163
164
  find(".tabulatr-filter-menu-wrapper a.btn").trigger('click')
164
- within(".tabulatr_filter_form") do
165
- fill_in("product_filter[vendor:name]", with: "producer")
166
- click_button("Apply")
167
- end
168
- page.should have_content(@vendor2.name)
169
- page.should have_no_content(@vendor1.name)
165
+ find('form.tabulatr_filter_form').fill_in("product_filter[vendor:name]", with: "producer")
166
+ find('form.tabulatr_filter_form').find_button('Apply').trigger('click')
167
+ expect(page).not_to have_selector('td[data-tabulatr-column-name="vendor:name"]', text: @vendor1.name)
168
+ expect(page).to have_selector('td[data-tabulatr-column-name="vendor:name"]', text: @vendor2.name)
170
169
  end
171
170
 
172
171
  it "filters with range", js: true do
@@ -181,14 +180,14 @@ describe "Tabulatr" do
181
180
  within('.tabulatr_filter_form') do
182
181
  fill_in("product_filter[price][from]", :with => 4)
183
182
  fill_in("product_filter[price][to]", :with => 10)
184
- click_button("Apply")
183
+ find_button("Apply").trigger('click')
185
184
  end
186
185
  page.find(".tabulatr_table tbody tr[data-id='#{Product.first.id}']").should have_content('foo')
187
186
  page.has_no_css?(".tabulatr_table tbody tr[data-id='#{Product.last.id}']")
188
187
  within('.tabulatr_filter_form') do
189
188
  fill_in("product_filter[price][from]", :with => 12)
190
189
  fill_in("product_filter[price][to]", :with => 19)
191
- click_button("Apply")
190
+ find_button("Apply").trigger('click')
192
191
  end
193
192
  page.should have_selector(".tabulatr_table tbody tr[data-id='#{Product.last.id}']")
194
193
  page.should have_no_selector(".tabulatr_table tbody tr[data-id='#{Product.first.id}']")
@@ -206,13 +205,17 @@ describe "Tabulatr" do
206
205
  within(".tabulatr_filter_form") do
207
206
  fill_in("product_filter[title][like]", with: "foo")
208
207
  expect(find('#title_like').visible?)
209
- click_button("Apply")
208
+ find_button("Apply").trigger('click')
210
209
  end
211
- expect(page).to have_content('foo')
212
- expect(page).to have_no_content('bar')
213
- find("a[data-hide-table-filter='title']").click
214
- expect(page).to have_content('foo')
215
- expect(page).to have_content('bar')
210
+ # expect(page).to have_content('foo')
211
+ # expect(page).to have_no_content('bar')
212
+ expect(page).not_to have_selector('td[data-tabulatr-column-name="title"]', text: 'bar')
213
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'foo')
214
+ find("a[data-hide-table-filter='title']").trigger('click')
215
+ # expect(page).to have_content('foo')
216
+ # expect(page).to have_content('bar')
217
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'bar')
218
+ expect(page).to have_selector('td[data-tabulatr-column-name="title"]', text: 'foo')
216
219
  end
217
220
  end
218
221
 
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tabulatr::Data::Pagination do
4
+ class DummySpecClass
5
+ include Tabulatr::Data::Pagination
6
+ end
7
+
8
+ before(:all) do
9
+ @dummy = DummySpecClass.new
10
+ end
11
+ describe '.compute_pagination' do
12
+ it "computes an offset" do
13
+ count = double(count: 20)
14
+ @dummy.instance_variable_set('@relation', count)
15
+ pagination = @dummy.compute_pagination(1, 10)
16
+ expect(pagination[:offset]).to be 0
17
+ pagination = @dummy.compute_pagination(2, 10)
18
+ expect(pagination[:offset]).to be 10
19
+ pagination = @dummy.compute_pagination(3, 10)
20
+ expect(pagination[:offset]).to be 20
21
+ pagination = @dummy.compute_pagination(4, 10)
22
+ expect(pagination[:offset]).to be 30
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tabulatr::Renderer do
4
+
5
+ describe "#generate_id"
6
+
7
+ it "generates a 'unique' id for a table" do
8
+ klass = Product
9
+ renderer = Tabulatr::Renderer.new(klass, nil)
10
+ first_id = renderer.generate_id
11
+ second_id = renderer.generate_id
12
+ expect(first_id).to_not eq second_id
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabulatr2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Horn
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-01-14 00:00:00.000000000 Z
13
+ date: 2014-01-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -180,7 +180,9 @@ files:
180
180
  - spec/dummy/public/favicon.ico
181
181
  - spec/features/tabulatrs_spec.rb
182
182
  - spec/lib/tabulatr/data/data_spec.rb
183
+ - spec/lib/tabulatr/data/pagination_spec.rb
183
184
  - spec/lib/tabulatr/json_builder_spec.rb
185
+ - spec/lib/tabulatr/renderer/renderer_spec.rb
184
186
  - spec/spec_helper.rb
185
187
  - tabulatr.gemspec
186
188
  homepage: http://github.com/provideal/tabulatr2
@@ -270,5 +272,7 @@ test_files:
270
272
  - spec/dummy/public/favicon.ico
271
273
  - spec/features/tabulatrs_spec.rb
272
274
  - spec/lib/tabulatr/data/data_spec.rb
275
+ - spec/lib/tabulatr/data/pagination_spec.rb
273
276
  - spec/lib/tabulatr/json_builder_spec.rb
277
+ - spec/lib/tabulatr/renderer/renderer_spec.rb
274
278
  - spec/spec_helper.rb