trk_datatables 0.2.7 → 0.2.12

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
  SHA256:
3
- metadata.gz: 25c48f4088f3f708eaa1dc7d464e0daaad342e57cdeca53c2d1a6b53cf317e6e
4
- data.tar.gz: 3ab420703241cd84845e88890027840a48c70f203363e2799322837963d47e73
3
+ metadata.gz: 07f18203116549f3dedce931bea8ea8c01d61dd550396899b4cfcb50fc52f779
4
+ data.tar.gz: 4f85a893e6b8f281caf64472c61ae7bc28635a45c73dfb6fb9b5fa4f44310b2d
5
5
  SHA512:
6
- metadata.gz: 7244afc3f897a92c6ea63b5b21721398a70ba5882a19a5296e79396c87df287fc9d257aa5a6a1efe99162ae31f673c5bb1400d4aa457236e8068ad6aac2b6aa1
7
- data.tar.gz: 4a9dd2fc25a1ad1be8f2ccab02409a7f0c00f38ba4dffe8d25eb0ebce308c29190e6043e955e352f17d12f4b432061a8baa763b53b1c8e6b4f8b9082d1c613d8
6
+ metadata.gz: 3e1cce7b7e3f561ba68bba76b7a078f9d7d4980a2805976cff1100d4863f20d25d667c4469d08904cf70ee64ef9a49e199385564c94a7a2fd9df44d443014c82
7
+ data.tar.gz: d1c6ddeb4bc762b7f4af30848466b9478386bfcf9c45cede0f3008cba5772c9ca04d3cd7e2cd2c19fb006e9903078575d51897a174b9cba852045153277ebfc6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.2.7)
4
+ trk_datatables (0.2.12)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -30,6 +30,7 @@ GEM
30
30
  rake (10.5.0)
31
31
  sqlite3 (1.4.1)
32
32
  thread_safe (0.3.6)
33
+ timecop (0.9.4)
33
34
  tzinfo (1.2.5)
34
35
  thread_safe (~> 0.1)
35
36
  zeitwerk (2.1.10)
@@ -47,6 +48,7 @@ DEPENDENCIES
47
48
  pg
48
49
  rake (~> 10.0)
49
50
  sqlite3
51
+ timecop
50
52
  trk_datatables!
51
53
 
52
54
  BUNDLED WITH
data/README.md CHANGED
@@ -256,7 +256,7 @@ For specific columns you can use following keys
256
256
  * `order: false` disable ordering for this column
257
257
  * `select_options: Post.statuses` generate select box instead of text input
258
258
  * `predefined_ranges: {}` for datetime fiels add ranges to pick up from
259
- * `hide: true` hide column with display none
259
+ * `hide: true` hide column with display none, for example `{ hide: @view.params[:user_id].present? }`
260
260
  * `class_name: 'Admin::User'` use different class name than
261
261
  `table_name.classify` (in this case of `admin_users` will be `AdminUser`)
262
262
  * `column_type_in_db` one of the: `:string`, `:integer`, `:date`, `:datetime`,
@@ -332,7 +332,7 @@ def columns
332
332
  end
333
333
 
334
334
  # in view
335
- link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status':
335
+ link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status',
336
336
  Post.statues.values_at(:published, :promoted)))
337
337
  ```
338
338
 
@@ -552,12 +552,30 @@ class PostsDatatable
552
552
  end
553
553
  ```
554
554
 
555
+ ### Render html with additional css class and different DOM
556
+
557
+ To add additional styling to your datatable you can use `:class` option when you
558
+ are calling `@datatable.render_html url, options` for example
559
+
560
+ ```
561
+ <%= @datatable.render_html search_posts_path(format: :json), class: 'table-hover' %>
562
+ ```
563
+
564
+ Default [DOM](https://datatables.net/reference/option/dom) is
565
+ `<"trk-global-search-wrapper"f>rtp<"trk-move-up"il>` which includes global
566
+ **f**iltering, p**r**ocessing loader, **t**able search, **p**agination, table
567
+ **i**nformation and **l**ength changing control. To override you can use
568
+
569
+ ```
570
+ <%= @datatable.render_html search_posts_path(format: :json), 'data-datatable-dom': 'rt' %>
571
+ ```
572
+
555
573
  ### Params
556
574
 
557
575
  To set parameters that you can use for links to set column search value, use
558
576
  this `PostsDatatable.param_set 'users.email', 'my@email.com'`. For between
559
577
  search you can use range `Time.zone.today..(Time.zone.today + 1.year)` and for
560
- in multiple values use array `[Post.statuses[:draft]]`.
578
+ in multiple values use array `[Post.statuses[:draft]]`:
561
579
 
562
580
  ```
563
581
  <%= link_to 'Active posts for my@email.com', \
@@ -573,6 +591,15 @@ in multiple values use array `[Post.statuses[:draft]]`.
573
591
  This will fill proper column search values so you do not need to do it manually
574
592
  (`post_path(:columns=>{"3"=>{:search=>{:value=>"my@email.com"}},
575
593
  "2"=>{:search=>{:value=>"1|2"}}}, :user_id=>1)`)
594
+ Please note that user_id is not inside datatable params so it will not be used
595
+ for next search params (all other search params are used with Datatables and
596
+ will remain on next search) so you need to manually add that param
597
+ ```
598
+ <%= @datatable.render_html search_posts_path(user_id: params[:user_id], format: :json) %>
599
+ ```
600
+
601
+ You can use generic name `params[:non_table_filter]` and split with colon
602
+ `user_id:123` but that is not needed.
576
603
 
577
604
  For form fields you can use similar helper that will return name which points to
578
605
  specific column, for example:
@@ -596,10 +623,39 @@ For global search you can use `[search][value]` for example
596
623
  <% end %>
597
624
  ```
598
625
 
599
- If you need, you can fetch params with this helper
626
+ If you need, you can fetch params with this helper and for example, show the
627
+ link for that record
628
+
629
+ ```
630
+ if @datatable.param_get("locations.name").present? &&
631
+ (location = Location.find_by(name: @datatable.param_get("locations.name")))
632
+ page_description "For <a href='#{location_path(location)}'>#{location.name}</a>"
633
+ breadcrumb "Dashboard": dashboard_path, location.name => location_path(location), "Package Sales": nil
634
+ else
635
+ breadcrumb "Dashboard": dashboard_path, "Package Sales": nil
636
+ end
637
+ ```
600
638
 
639
+ For other filter params which are not in columns you can use non table params
601
640
  ```
602
- PostsDatatable.param_get('users.email', params)
641
+ # on dashboard
642
+ <%= link_to 'Locations', isp_locations_path(non_table_filter: "reseller_operator_id:#{@reseller_operator.id}") %>
643
+
644
+ # on index
645
+ if params[:non_table_filter].present? &&
646
+ (reseller_operator = ResellerOperator.find(params[:non_table_filter].split(":").second))
647
+ page_description "For <a href='#{reseller_operator_path(reseller_operator)}'>#{reseller_operator.company_name}</a>"
648
+ breadcrumb 'Dashboard' => isp_dashboard_path, reseller_operator.company_name => reseller_operator_path(reseller_operator), 'Locations' => nil
649
+ else
650
+ breadcrumb 'Dashboard' => isp_dashboard_path, 'Locations' => nil
651
+ end
652
+
653
+ # in datatables
654
+ case @view.params[:non_table_filter].to_s.split(':').first
655
+ when 'reseller_operator_id'
656
+ reseller_operator = ResellerOperator.find @view.params[:non_table_filter].split(':').second
657
+ all_isp_locations = all_isp_locations.where(reseller_operator: reseller_operator)
658
+ end
603
659
  ```
604
660
 
605
661
  You can set filters on datatable even params are blank, for example
@@ -661,7 +717,9 @@ end
661
717
 
662
718
  You can use condition to provide different data, for example let's assume
663
719
  `@view.api_user?` returns true for json requests from mobile app. Here is
664
- example that provides different columns for normal and api_user:
720
+ example that provides different columns for normal and api_user.
721
+ Note that when you are using different columns for some reason in `@view` you
722
+ need to provide view in `param_set` so it can check the same conditionals.
665
723
 
666
724
  ```
667
725
  # app/datatables/posts_datatable.rb
@@ -671,8 +729,14 @@ class PostsDatatable < TrkDatatables::ActiveRecord
671
729
  end
672
730
 
673
731
  def columns_for_html
732
+ balance = @view.current_location
733
+ {}
734
+ else
735
+ { 'integer_calculated_in_db.balance_amount_in_cents': { search: false, title: 'Balance' } }
736
+ end
674
737
  {
675
738
  'subscribers.subscriberid': {},
739
+ **balance,
676
740
  'subscribers.name': {},
677
741
  }
678
742
  end
@@ -691,8 +755,14 @@ class PostsDatatable < TrkDatatables::ActiveRecord
691
755
 
692
756
  def rows_for_html(filtered)
693
757
  filtered.map do |subscriber|
758
+ balance = if Constant.STILL_WITH_OLD_CODE
759
+ []
760
+ else
761
+ [@view.humanized_money_with_symbol(Money.new(location.balance_amount_in_cents)) : 'NA']
762
+ end
694
763
  [
695
764
  @view.link_to(subscriber.subscriberid, subscriber),
765
+ *balance,
696
766
  subscriber.name,
697
767
  ]
698
768
  end
@@ -712,6 +782,10 @@ class PostsDatatable < TrkDatatables::ActiveRecord
712
782
  @view.api_user? ? columns_for_api : nil
713
783
  end
714
784
  end
785
+
786
+ # On some dashboard page provide @view using `self` to param_set
787
+ link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status',
788
+ :active, self))
715
789
  ```
716
790
 
717
791
  ## Test your datatables
@@ -87,12 +87,15 @@ module TrkDatatables
87
87
  # we do not need to cast from string since range will do automatically
88
88
  parsed_from = from
89
89
  parsed_to = to
90
- when :date, :datetime
90
+ when :date
91
+ parsed_from = _parse_in_zone(from).to_date
92
+ parsed_to = _parse_in_zone(to).to_date
93
+ when :datetime
91
94
  parsed_from = _parse_in_zone(from)
92
95
  parsed_to = _parse_in_zone(to)
93
- if parsed_to.present? && !to.match(/AM|PM/)
94
- # we need to add one day since it looks at begining of a day 2010-10-10 00:00:00
95
- parsed_to += 60 * 60 * 24 - 1
96
+ if parsed_to.present? && !to.match(/:/)
97
+ # Use end of a day if time is not defined, for example 2020-02-02
98
+ parsed_to = parsed_to.at_end_of_day
96
99
  end
97
100
  end
98
101
  [parsed_from, parsed_to]
@@ -7,7 +7,7 @@ module TrkDatatables
7
7
  class Error < StandardError
8
8
  end
9
9
 
10
- class Base
10
+ class Base # rubocop:todo Metrics/ClassLength
11
11
  extend TrkDatatables::BaseHelpers
12
12
 
13
13
  attr_accessor :column_key_options
@@ -97,8 +97,6 @@ module TrkDatatables
97
97
  raise 'order_and_paginate_items_is_defined_in_specific_orm'
98
98
  end
99
99
 
100
- # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
101
-
102
100
  # Returns dt_orders or default as array of index and direction
103
101
  # https://datatables.net/reference/option/order
104
102
  # @return
@@ -114,12 +112,11 @@ module TrkDatatables
114
112
  @dt_orders_or_default = @dt_params.dt_orders
115
113
  @preferences.set :order, @dt_params.dt_orders
116
114
  else
117
- check_value = ->(r) { r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) }
115
+ check_value = ->(r) { r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) && r[0][0] < @column_key_options.size }
118
116
  @dt_orders_or_default = @preferences.get(:order, check_value) || default_order
119
117
  end
120
118
  @dt_orders_or_default
121
119
  end
122
- # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
123
120
 
124
121
  def default_order
125
122
  [[0, :desc]].freeze
@@ -226,7 +223,7 @@ module TrkDatatables
226
223
  }
227
224
  end
228
225
 
229
- def predefined_date_ranges
226
+ def predefined_date_ranges # rubocop:todo Metrics/AbcSize
230
227
  {
231
228
  'Today': Time.zone.today..Time.zone.today,
232
229
  'Yesterday': [Time.zone.today - 1.day, Time.zone.today - 1.day],
@@ -236,12 +233,13 @@ module TrkDatatables
236
233
  }
237
234
  end
238
235
 
239
- def predefined_datetime_ranges
236
+ def predefined_datetime_ranges # rubocop:todo Metrics/AbcSize
240
237
  {
241
238
  'Today': Time.zone.now.beginning_of_day..Time.zone.now.end_of_day,
242
239
  'Yesterday': [Time.zone.now.beginning_of_day - 1.day, Time.zone.now.end_of_day - 1.day],
243
240
  'This Month': Time.zone.today.beginning_of_month.beginning_of_day...Time.zone.now.end_of_day,
244
- 'Last Month': Time.zone.today.prev_month.beginning_of_month.beginning_of_day...Time.zone.today.prev_month.end_of_month.end_of_day,
241
+ 'Last Month':
242
+ Time.zone.today.prev_month.beginning_of_month.beginning_of_day...Time.zone.today.prev_month.end_of_month.end_of_day,
245
243
  'This Year': Time.zone.today.beginning_of_year.beginning_of_day...Time.zone.today.end_of_day,
246
244
  }.transform_values do |range|
247
245
  # datepicker expects format 2020-11-29 11:59:59
@@ -13,8 +13,8 @@ module TrkDatatables
13
13
  # @example
14
14
  # link_to 'Published posts for user1',
15
15
  # posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))
16
- def param_set(column_key, value)
17
- datatable = new OpenStruct.new(params: {})
16
+ def param_set(column_key, value, view = nil)
17
+ datatable = new view || OpenStruct.new(params: {})
18
18
  value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
19
19
  value = [value.first, value.last].join BETWEEN_SEPARATOR if value.is_a? Range
20
20
  column_index = datatable.index_by_column_key column_key
@@ -172,7 +172,12 @@ module TrkDatatables
172
172
  # This is helper
173
173
  def _determine_string_type_cast # :nodoc:
174
174
  if defined?(::ActiveRecord::Base)
175
- DB_ADAPTER_STRING_TYPE_CAST[::ActiveRecord::Base.connection_config[:adapter].to_sym]
175
+ current_adapter = if ::ActiveRecord::Base.respond_to?(:connection_db_config)
176
+ ::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
177
+ else
178
+ ::ActiveRecord::Base.connection_config[:adapter]
179
+ end
180
+ DB_ADAPTER_STRING_TYPE_CAST[current_adapter.to_sym]
176
181
  else
177
182
  'not_used'
178
183
  end
@@ -117,7 +117,7 @@ module TrkDatatables
117
117
  def param_get(column_index)
118
118
  @params.dig :columns, column_index.to_s, :search, :value
119
119
  rescue TypeError => e
120
- raise Error, e.message + '. Column search is in a format: { "columns": { "0": { "search": { "value": { "ABC" } } } } }'
120
+ raise Error, "#{e.message}. Column search is in a format: { \"columns\": { \"0\": { \"search\": { \"value\": { \"ABC\" } } } } }"
121
121
  end
122
122
 
123
123
  def self.sample_view_params(options = {})
@@ -4,7 +4,7 @@ module TrkDatatables
4
4
  def initialize(search_link, datatable, html_options = {})
5
5
  @search_link = search_link
6
6
  @datatable = datatable
7
- @html_options = html_options
7
+ @html_options = html_options.symbolize_keys
8
8
  self.class.indent = 0
9
9
  end
10
10
 
@@ -89,6 +89,7 @@ module TrkDatatables
89
89
  # for initial page load we do not have ability to show recordsTotal
90
90
  # https://github.com/trkin/trk_datatables_js/issues/1
91
91
  'data-datatable-total-length': @datatable.filtered_items_count,
92
+ 'data-datatable-dom': @html_options[:'data-datatable-dom'] || '<"trk-global-search-wrapper"f>rtp<"trk-move-up"il>',
92
93
  ) do
93
94
  thead << "\n".html_safe << tbody
94
95
  end +
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.2.7'.freeze
2
+ VERSION = '0.2.12'.freeze
3
3
  end
@@ -46,6 +46,7 @@ Gem::Specification.new do |spec|
46
46
  spec.add_development_dependency 'database_cleaner'
47
47
  spec.add_development_dependency 'minitest', '~> 5.0'
48
48
  spec.add_development_dependency 'minitest-color'
49
+ spec.add_development_dependency 'timecop'
49
50
  spec.add_development_dependency 'pg'
50
51
  spec.add_development_dependency 'rake', '~> 10.0'
51
52
  spec.add_development_dependency 'sqlite3'
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.2.7
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dusan Orlovic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-28 00:00:00.000000000 Z
11
+ date: 2021-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: pg
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
220
  - !ruby/object:Gem::Version
207
221
  version: '0'
208
222
  requirements: []
209
- rubygems_version: 3.0.8
223
+ rubygems_version: 3.0.3
210
224
  signing_key:
211
225
  specification_version: 4
212
226
  summary: Gem that simplify using datatables with Ruby on Rails and Sinatra.