trk_datatables 0.1.23 → 0.1.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be66386c1790e311f2569103b47de86bf98d5a7a97e55cfd2e74208476192fb5
4
- data.tar.gz: b5f32eb60f84a619bcbbb689c3caeaeb8ed359d7de024eb862ac1e3c4a892c4e
3
+ metadata.gz: 63b484a61536f738a27bc1011d30e4e51f784f07468f13b66ad18912137159a1
4
+ data.tar.gz: 85d5c270f493090bcb6c0f177d2b71c26fa950adb181a6268747adf05491bc91
5
5
  SHA512:
6
- metadata.gz: 52d265632b49ace4ff5ff878eb7efa108920f4f53436aab5249ab0632731a1a7b796eb5d37a11dcedfc30609350a2f74fbfdac42518a3ea3402c8a1ee99f7c1d
7
- data.tar.gz: 0a6826db86f23223a2c95722c39f558e5516cfa9549d1616f0b6ed909d45f4808ded85954cdf05ecef94533caf6e55c1598bb03eae05a10b320c7d26d94231f0
6
+ metadata.gz: 1f0e940deeb4eebc8e227080a51e95c84f5038b54f00186bd273ac771abac4672d5c7cf262ce12cd653d22328c38f2a8bedf53f8b5f6deb50143233b1dc65349
7
+ data.tar.gz: eb84cc60601fd30ec87bcaf5c6c3812688ca0d15b7a53ea19f73a06290865fdbe0d72dcb62f7d1e4cdd52cd20fc7c538bbfe037b36e385629b0ad2ab80b1ee3c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.1.23)
4
+ trk_datatables (0.1.24)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -5,10 +5,10 @@ used with [trk_datatables npm package](https://www.npmjs.com/package/trk_datatab
5
5
 
6
6
  Instead of using Rails scaffold generator you can use advanced [Datatables plug-in for jQuery library](https://datatables.net).
7
7
 
8
- After [few lines of code](https://github.com/trkin/trk_datatables#installation) you can use
9
- one line commands `@datatable.render_html` to generate index page that supports:
10
- global search, filtering and sorting, first page is prerendered (so non-js
11
- crawlers can see it), adding map and other interesting features.
8
+ After [few lines of code](https://github.com/trkin/trk_datatables#installation)
9
+ you can use one line commands `@datatable.render_html` to generate index page
10
+ that supports: global search, filtering and sorting, first page is prerendered
11
+ (so non-js crawlers can see it), adding map and other interesting features.
12
12
 
13
13
  So instead of basic Rails scaffold with a lot of html code
14
14
 
@@ -26,13 +26,19 @@ You can get something like
26
26
  * [Usage example in Ruby on Rails](#usage-example-in-ruby-on-rails)
27
27
  * [Configuration](#configuration)
28
28
  * [Global search](#global-search)
29
- * [Column 'ILIKE' and 'BETWEEN' search](#column-ilike-and-between-search)
30
- * [Column 'IN' search](#column-in-search)
31
- * [Action column](#action-column)
29
+ * [Column 'ILIKE' search with text input](#column-ilike-search-with-text-input)
30
+ * [Column 'BETWEEN' search with js daterangepicker](#column-between-search-with-js-daterangepicker)
31
+ * [Column 'IN' search with select tag](#column-in-search-with-select-tag)
32
+ * [Boolean column with checkbox](#boolean-column-with-checkbox)
33
+ * [Action and non database columns](#action-and-non-database-columns)
34
+ * [Values calculated in database](#values-calculated-in-database)
35
+ * [Default order and page length](#default-order-and-page-length)
32
36
  * [Params](#params)
33
37
  * [Saved Preferences (optional)](#saved-preferences-optional)
34
38
  * [Additional data to json response](#additional-data-to-json-response)
35
39
  * [Different response for mobile app](#different-response-for-mobile-app)
40
+ * [Test your datatables](#test-your-datatables)
41
+ * [Exceptions](#exceptions)
36
42
  * [Debug](#debug)
37
43
  * [Alternatives](#alternatives)
38
44
  * [Development](#development)
@@ -40,7 +46,7 @@ You can get something like
40
46
  * [License](#license)
41
47
  * [Code of Conduct](#code-of-conduct)
42
48
 
43
- <!-- Added by: orlovic, at: Mon Sep 9 09:38:44 CEST 2019 -->
49
+ <!-- Added by: orlovic, at: Sat Aug 29 05:26:19 CEST 2020 -->
44
50
 
45
51
  <!--te-->
46
52
 
@@ -207,9 +213,10 @@ class PostsDatatable < TrkDatatables::ActiveRecord
207
213
  end
208
214
  ```
209
215
 
210
- ### Column 'ILIKE' search
216
+ ### Column 'ILIKE' search with text input
211
217
 
212
- All columns are by default casted to string and `ILIKE` is perfomed.
218
+ All columns are by default casted to `:string` (you can manually cast with
219
+ `column_type_in_db: :string`) and `ILIKE` is perfomed.
213
220
 
214
221
  If you do not need any specific column configuration,
215
222
  for example custom `title`, than instead of defining columns as key/value
@@ -244,7 +251,7 @@ For specific columns you can use following keys
244
251
  * `class_name: 'Admin::User'` use different class name than
245
252
  `table_name.classify` (in this case of `admin_users` will be `AdminUser`)
246
253
 
247
- ### Column 'BETWEEN' search
254
+ ### Column 'BETWEEN' search with js daterangepicker
248
255
 
249
256
  For column search when search string contains BETWEEN_SEPARATOR (` - `) and
250
257
  column_type_in_db as one of the: `:date`, `:datetime`, `:integer` and
@@ -315,7 +322,7 @@ We use
315
322
  [ActiveSupport::TimeZone](https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html)
316
323
  so if you use different than UTC you need to set `Time.zone =` in your app [example values](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/values/time_zone.rb#L31). Whenever Time.parse is used (and we use `Time.zone.parse` for params) it needs correct zone (in Rails you can set timezone in `config.time_zone` or use [browser timezone rails gem](https://github.com/kbaum/browser-timezone-rails)).
317
324
 
318
- ### Column 'IN' search
325
+ ### Column 'IN' search with select tag
319
326
 
320
327
  You can use column_option `select_options: [['name1', 'value1']]` so select box
321
328
  will be loaded and match if `col IN (value1|value2)`.
@@ -333,9 +340,11 @@ link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status':
333
340
  Post.statues.values_at(:published, :promoted)))
334
341
  ```
335
342
 
336
- You can use column_option `search: :checkbox` so for column_type_in_db `:boolean`
337
- it will provide checkbox. For other column_type_in_db it will match if value is
338
- NULL or NOT NULL.
343
+ ### Boolean column with checkbox
344
+
345
+ For column_type_in_db `:boolean` it will generate checkbox and use
346
+ http://vanderlee.github.io/tristate/ so you can filter for `true`, `false` or
347
+ `any` (in this case we ignore this column in filtering)
339
348
 
340
349
  ### Action and non database columns
341
350
 
@@ -583,7 +592,7 @@ class PostsDatatableTest < ActiveSupport::TestCase
583
592
  end
584
593
  ```
585
594
 
586
- # Exceptions
595
+ ## Exceptions
587
596
 
588
597
  To catch errors from TrkDatables you can
589
598
 
@@ -627,7 +636,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
627
636
  rake
628
637
  # specific file
629
638
  ruby -I test test/trk_datatables/base_test.rb
630
- # only mathing
639
+ # specific test that matches additional
631
640
  ruby -I test test/trk_datatables/base_test.rb -n /additional/
632
641
  ```
633
642
 
@@ -33,6 +33,8 @@ module TrkDatatables
33
33
  select_options = column_key_option[:column_options][ColumnKeyOptions::SELECT_OPTIONS]
34
34
  if select_options.present?
35
35
  filter_column_as_in(column_key_option, search_value)
36
+ elsif %i[boolean].include?(column_key_option[:column_type_in_db])
37
+ filter_column_as_boolean(column_key_option, search_value)
36
38
  elsif %i[date datetime integer float].include?(column_key_option[:column_type_in_db]) && \
37
39
  search_value.include?(BETWEEN_SEPARATOR)
38
40
  from, to = search_value.split BETWEEN_SEPARATOR
@@ -52,6 +54,13 @@ module TrkDatatables
52
54
  end.reduce(:and)
53
55
  end
54
56
 
57
+ def filter_column_as_boolean(column_key_option, search_value)
58
+ # return true relation in case we ignore
59
+ return Arel::Nodes::SqlLiteral.new('1').eq(1) if search_value == 'any'
60
+
61
+ _arel_column(column_key_option).eq(search_value == 'true')
62
+ end
63
+
55
64
  def filter_column_as_between(column_key_option, from, to)
56
65
  from, to = _parse_from_to(from, to, column_key_option)
57
66
  if from.present? && to.present?
@@ -86,7 +95,6 @@ module TrkDatatables
86
95
  [parsed_from, parsed_to]
87
96
  end
88
97
 
89
- # rubocop:disable Rails/TimeZone
90
98
  def _parse_in_zone(time)
91
99
  return nil if time.blank?
92
100
 
@@ -95,7 +103,6 @@ module TrkDatatables
95
103
  rescue ArgumentError
96
104
  nil
97
105
  end
98
- # rubocop:enable Rails/TimeZone
99
106
 
100
107
  def order_and_paginate_items(filtered)
101
108
  filtered = order_items filtered
@@ -1,5 +1,5 @@
1
1
  module TrkDatatables
2
- # rubocop:disable ClassLength
2
+ # rubocop:disable Metrics/ClassLength
3
3
  class ColumnKeyOptions
4
4
  include Enumerable
5
5
 
@@ -16,6 +16,7 @@ module TrkDatatables
16
16
  ORDER_OPTION = :order
17
17
  TITLE_OPTION = :title
18
18
  SELECT_OPTIONS = :select_options
19
+ CHECKBOX_OPTION = :checkbox_option
19
20
  PREDEFINED_RANGES = :predefined_ranges
20
21
  HIDE_OPTION = :hide
21
22
  CLASS_NAME = :class_name
@@ -23,9 +24,11 @@ module TrkDatatables
23
24
  # this will load date picker
24
25
  # SEARCH_OPTION_DATE_VALUE = :date
25
26
  # SEARCH_OPTION_DATETIME_VALUE = :datetime
26
- COLUMN_OPTIONS = [SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS,
27
- PREDEFINED_RANGES, HIDE_OPTION, CLASS_NAME,
28
- COLUMN_TYPE_IN_DB].freeze
27
+ COLUMN_OPTIONS = [
28
+ SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS,
29
+ CHECKBOX_OPTION, PREDEFINED_RANGES, HIDE_OPTION, CLASS_NAME,
30
+ COLUMN_TYPE_IN_DB
31
+ ].freeze
29
32
 
30
33
  # for columns that as calculated in db query
31
34
  STRING_CALCULATED_IN_DB = :string_calculcated_in_db
@@ -96,7 +99,10 @@ module TrkDatatables
96
99
  else
97
100
  table_class = _determine_table_class table_name, column_options[CLASS_NAME]
98
101
 
99
- column_type_in_db = column_options[COLUMN_TYPE_IN_DB] || _determine_db_type_for_column(table_class, column_name) unless column_options[SEARCH_OPTION] == false && column_options[ORDER_OPTION] == false
102
+ unless column_options[SEARCH_OPTION] == false && column_options[ORDER_OPTION] == false
103
+ column_type_in_db = column_options[COLUMN_TYPE_IN_DB] || _determine_db_type_for_column(table_class, column_name)
104
+ column_options[CHECKBOX_OPTION] = true if column_type_in_db == :boolean && column_options[CHECKBOX_OPTION].nil?
105
+ end
100
106
  end
101
107
  arr << {
102
108
  column_key: column_key.to_sym,
@@ -208,10 +214,11 @@ module TrkDatatables
208
214
  res['data-searchable'] = false if column_options[SEARCH_OPTION] == false
209
215
  res['data-orderable'] = false if column_options[ORDER_OPTION] == false
210
216
  res['data-datatable-hidden-column'] = true if column_options[HIDE_OPTION] == true
217
+ res['data-datatable-checkbox'] = true if column_options[CHECKBOX_OPTION] == true
211
218
  if %i[date datetime].include? column_type_in_db
212
219
  res['data-datatable-range'] = column_type_in_db == :datetime ? :datetime : true
213
220
  if column_options[PREDEFINED_RANGES].present? ||
214
- (@predefined_ranges.present? && column_options[PREDEFINED_RANGES] != false)
221
+ (@predefined_ranges.present? && column_options[PREDEFINED_RANGES] != false)
215
222
  res['data-datatable-predefined-ranges'] = if column_options[PREDEFINED_RANGES].is_a? Hash
216
223
  column_options[PREDEFINED_RANGES]
217
224
  else
@@ -225,5 +232,5 @@ module TrkDatatables
225
232
  res
226
233
  end
227
234
  end
228
- # rubocop:enable ClassLength
235
+ # rubocop:enable Metrics/ClassLength
229
236
  end
@@ -107,7 +107,7 @@ module TrkDatatables
107
107
  end
108
108
 
109
109
  def self.param_set(column_index, value)
110
- { columns: { column_index.to_s => { search: { value: value } } } }
110
+ {columns: {column_index.to_s => {search: {value: value}}}}
111
111
  end
112
112
 
113
113
  def self.form_field_name(column_index)
@@ -135,7 +135,7 @@ module TrkDatatables
135
135
  value: '', regex: 'false'
136
136
  },
137
137
  order: {
138
- '0': { column: '0', dir: 'desc' }
138
+ '0': {column: '0', dir: 'desc'}
139
139
  },
140
140
  # [:columns] should have the same size as column_key_options since we
141
141
  # ignore keys, and use positions
@@ -21,7 +21,7 @@ module TrkDatatables
21
21
  def set(key, value)
22
22
  return unless @holder
23
23
 
24
- h = { KEY_IN_PREFERENCES => { @class_name => { key => value } } }
24
+ h = {KEY_IN_PREFERENCES => {@class_name => {key => value}}}
25
25
  @holder.send("#{@field}=", {}) if @holder.send(@field).nil?
26
26
  @holder.send(@field).deep_merge! h
27
27
  @holder.save!
@@ -1,5 +1,4 @@
1
1
  module TrkDatatables
2
- # rubocop:disable Rails/OutputSafety
3
2
  class RenderHtml
4
3
  @indent = 0
5
4
  def initialize(search_link, datatable, html_options = {})
@@ -28,7 +27,7 @@ module TrkDatatables
28
27
  array.map { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
29
28
  end
30
29
 
31
- # rubocop:disable Metrics/AbcSize
30
+ # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
32
31
  #
33
32
  # _content_tag :p, 'Hi'
34
33
  # _content_tag :p, class: 'button', 'Hi'
@@ -57,13 +56,13 @@ module TrkDatatables
57
56
  self.class.indent -= 1
58
57
  html
59
58
  end
60
- # rubocop:enable Metrics/AbcSize
59
+ # rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity
61
60
 
62
61
  def _select_tag(options, search_value)
63
62
  selected = search_value.to_s.split(MULTIPLE_OPTION_SEPARATOR)
64
63
  _content_tag :select, multiple: 'multiple' do
65
64
  safe_join(options.map do |key, value|
66
- _content_tag :option, { value: value }.merge(selected.include?(value.to_s) ? { selected: 'selected' } : {}), key
65
+ _content_tag :option, {value: value}.merge(selected.include?(value.to_s) ? {selected: 'selected'} : {}), key
67
66
  end)
68
67
  end
69
68
  end
@@ -93,7 +92,8 @@ module TrkDatatables
93
92
  'data-datatable-total-length': @datatable.filtered_items_count,
94
93
  ) do
95
94
  thead << "\n".html_safe << tbody
96
- end
95
+ end +
96
+ "\n" # add new line at the end so we can test with HERE_DOC </table>
97
97
  end
98
98
 
99
99
  def thead
@@ -132,5 +132,4 @@ module TrkDatatables
132
132
  ''
133
133
  end
134
134
  end
135
- # rubocop:enable Rails/OutputSafety
136
135
  end
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.1.23'.freeze
2
+ VERSION = '0.1.24'.freeze
3
3
  end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Dusan Orlovic']
10
10
  spec.email = ['duleorlovic@gmail.com']
11
11
 
12
- spec.summary = %q(Gem that simplify using datatables with Ruby on Rails and Sinatra.)
13
- spec.description = %q(Html render first page, sort and filter...)
12
+ spec.summary = 'Gem that simplify using datatables with Ruby on Rails and Sinatra.'
13
+ spec.description = 'Html render first page, sort and filter...'
14
14
  spec.homepage = 'https://github.com/trkin/trk_datatables'
15
15
  spec.license = 'MIT'
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trk_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.23
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dusan Orlovic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-28 00:00:00.000000000 Z
11
+ date: 2020-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport