trk_datatables 0.2.6 → 0.2.11

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: 40eeba5483cf896ad0df528f5b329a0bdfd7b2a7d78ab18df295f1b30737ba8f
4
- data.tar.gz: 7e9c1cd8f4c9fcf942dd5639b7f406e85ccb6c0c037e2dd6a3a7b42ece5d641b
3
+ metadata.gz: 3b31d7018f64f25ae41952d0540e64ef9686e28a2aec737c4d2b2fd7efd0fd3c
4
+ data.tar.gz: 2ba8567079e3d578705dde71ee0388679bb005b4466d534297615cd837490eb2
5
5
  SHA512:
6
- metadata.gz: b29ccb1148bb7b4782b4cf5d466330d241bbc90c5554129b4d243833d5585a2fad599fbf6e518e62a08db487e32adcdf0ebd65232592f9d01fe048a4898f0310
7
- data.tar.gz: 3b3f65377b9bc66f33874abcbe231fc6e5ace020b82526fb8edbab5b697085bcf1f3bd48c2bc8f7d42873770a6c61e2096716a3bf95343dad20aa9c2947ee0dd
6
+ metadata.gz: c6004c6612c6389eac26063c4bd8ae4988c368d06315914c5636eb98dbc620101cfc68483fb7cada302b79c83007aab49c6f357e0de45dc9d2cd10eafd1ff800
7
+ data.tar.gz: d9e416dba7a23a0c4f1b8d8880a5460fbee44892993718dcea325fe92f89b579c70df9d7ba46d2dd5e37b752ad963d3b173dfa8e48bc489ecd52e4484ed1d770
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.2.6)
4
+ trk_datatables (0.2.11)
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
@@ -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,8 +4,8 @@ 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
8
- self.class.indent = -1
7
+ @html_options = html_options.symbolize_keys
8
+ self.class.indent = 0
9
9
  end
10
10
 
11
11
  def result
@@ -53,7 +53,7 @@ module TrkDatatables
53
53
  else
54
54
  ">\n".html_safe << yield << "\n#{' ' * self.class.indent}</#{tag}>".html_safe
55
55
  end
56
- self.class.indent -= 1
56
+ self.class.indent -= 1 if self.class.indent > 1
57
57
  html
58
58
  end
59
59
  # rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity
@@ -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.6'.freeze
2
+ VERSION = '0.2.11'.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.6
4
+ version: 0.2.11
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-11-29 00:00:00.000000000 Z
11
+ date: 2021-04-05 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.