trk_datatables 0.2.12 → 0.2.13

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: 07f18203116549f3dedce931bea8ea8c01d61dd550396899b4cfcb50fc52f779
4
- data.tar.gz: 4f85a893e6b8f281caf64472c61ae7bc28635a45c73dfb6fb9b5fa4f44310b2d
3
+ metadata.gz: dfebccd40ad802ae6e7e6beb1eebec9099f9ede5bf4ed4d2f6f9ed92a08a8f03
4
+ data.tar.gz: 0fcb0efd54b898d2d2aa68ff76c095293fa0b57abb48a37bd893aa59a676ff4e
5
5
  SHA512:
6
- metadata.gz: 3e1cce7b7e3f561ba68bba76b7a078f9d7d4980a2805976cff1100d4863f20d25d667c4469d08904cf70ee64ef9a49e199385564c94a7a2fd9df44d443014c82
7
- data.tar.gz: d1c6ddeb4bc762b7f4af30848466b9478386bfcf9c45cede0f3008cba5772c9ca04d3cd7e2cd2c19fb006e9903078575d51897a174b9cba852045153277ebfc6
6
+ metadata.gz: fa204f488a45f055b602e9baf8504cd64e8a91404a69800588065fcabbfa5a85ce2ae9220751fdf7633facdfe1627a98bae33354eabfaf6c64e9053f20f6472a
7
+ data.tar.gz: ba2fbe421642ed72bff9e023b541c4321c56baa653f54f1ac372d3e7d43233691a7316040340ff20c94e1d4fab6ceb5ce314fe0efeadfa65240e2dee2042593e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.2.12)
4
+ trk_datatables (0.2.13)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -52,4 +52,4 @@ DEPENDENCIES
52
52
  trk_datatables!
53
53
 
54
54
  BUNDLED WITH
55
- 2.1.4
55
+ 2.2.15
data/README.md CHANGED
@@ -531,6 +531,94 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
531
531
  end
532
532
  ```
533
533
 
534
+ ### Table less models
535
+
536
+ You can use raw sql to fetch the data and use it as a model.
537
+ Here is an example when there is no relations to other models
538
+ ```
539
+ # app/models/table_less.rb
540
+ class TableLess < ApplicationRecord
541
+ self.abstract_class = true
542
+
543
+ def self.load_schema!
544
+ @columns_hash ||= {}
545
+
546
+ # From active_record/attributes.rb
547
+ attributes_to_define_after_schema_loads.each do |name, (type, options)|
548
+ type = ActiveRecord::Type.lookup(type, **options.except(:default)) if type.is_a?(Symbol)
549
+
550
+ define_attribute(name, type, **options.slice(:default))
551
+
552
+ # Improve Model#inspect output
553
+ @columns_hash[name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default])
554
+ end
555
+ end
556
+ end
557
+ ```
558
+
559
+ ```
560
+ # app/models/translation.rb
561
+ class Translation < TableLess
562
+ self.table_name = :translations
563
+
564
+ attribute :translateable_type, :string, default: nil
565
+ attribute :translateable_id, :string, default: nil
566
+ attribute :column_name, :string, default: nil
567
+ attribute :column_value, :string, default: nil
568
+
569
+ belongs_to :translateable, polymorphic: true
570
+ end
571
+ ```
572
+
573
+ ```
574
+ # app/datatables/translations_datatable.rb
575
+ # rubocop:disable Layout/LineLength
576
+ class TranslationsDatatable < BaseDatatable
577
+ def columns
578
+ {
579
+ 'translations.translateable_id': {},
580
+ 'translations.translateable_type': {hide: true},
581
+ 'translations.column_name': {},
582
+ 'translations.column_value': {},
583
+ '': {},
584
+ }
585
+ end
586
+
587
+ def all_items
588
+ sql = <<~SQL.squish
589
+ (
590
+ (SELECT 'Activity' AS translateable_type, id AS translateable_id, 'name' AS column_name, name AS column_value FROM activities)
591
+ UNION
592
+ (SELECT 'Activity' AS translateable_type, id AS translateable_id, 'description' AS column_name, description AS column_value FROM activities)
593
+ ) as translations
594
+ SQL
595
+ Translation.from([Arel.sql(sql)])
596
+ end
597
+
598
+ def rows(filtered)
599
+ filtered.map do |translation|
600
+ edit_link = @view.button_tag_open_modal(
601
+ @view.edit_translation_path(translation.translateable_id, translateable_type: translation.translateable_type, column_name: translation.column_name), title: @view.t_crud('edit', Translation)
602
+ )
603
+ [
604
+ @view.link_to(translation.translateable, translation.translateable),
605
+ translation.translateable_type,
606
+ translation.column_name,
607
+ translation.column_value,
608
+ edit_link,
609
+ ]
610
+ end
611
+ end
612
+ end
613
+ # rubocop:enable Layout/LineLength
614
+ ```
615
+
616
+ For column title we use `table_class.human_attribute_name column_name`. When
617
+ calculated_ columns is used than it can not find translation so better is to:
618
+ ```
619
+ 'string_calculated_in_db.column_value_translated': {search: false, title: @view.t('activerecord.attributes.translation.column_value_translated')},
620
+ ```
621
+
534
622
  ### Default order and page length
535
623
 
536
624
  You can override default order (index and direction) and default page length so
@@ -575,7 +663,18 @@ Default [DOM](https://datatables.net/reference/option/dom) is
575
663
  To set parameters that you can use for links to set column search value, use
576
664
  this `PostsDatatable.param_set 'users.email', 'my@email.com'`. For between
577
665
  search you can use range `Time.zone.today..(Time.zone.today + 1.year)` and for
578
- in multiple values use array `[Post.statuses[:draft]]`:
666
+ in multiple values use array `[Post.statuses[:draft]]`. Note that in Rails
667
+ `Time.zone.now.to_s` usually returns seconds ('23:59:59') but if you change
668
+ default format like in `config/initializers/time_formats.rb` with
669
+ `Time::DATE_FORMATS[:default] = '%d-%b-%Y %I:%M %p'` than range
670
+ `Time.zone.now.at_beginning_of_day..Time.zone.now.at_end_of_day` will not
671
+ include those at end of day since param will not include seconds ('23:59') so in
672
+ this case you can use range for strings
673
+ `Time.zone.now.at_beginning_of_day.to_s..Time.zone.now.at_end_of_day.to_s(:with_seconds)`
674
+ and config/initializers/time_formats.rb `Time::DATE_FORMATS[:with_seconds] = '%d-%b-%Y %I:%M:%S %p'`
675
+
676
+ (or use date `Time.zone.today..Time.zone.today` but that will not populate
677
+ datetime fields in dateRangePicker correctly)
579
678
 
580
679
  ```
581
680
  <%= link_to 'Active posts for my@email.com', \
@@ -241,10 +241,7 @@ module TrkDatatables
241
241
  'Last Month':
242
242
  Time.zone.today.prev_month.beginning_of_month.beginning_of_day...Time.zone.today.prev_month.end_of_month.end_of_day,
243
243
  'This Year': Time.zone.today.beginning_of_year.beginning_of_day...Time.zone.today.end_of_day,
244
- }.transform_values do |range|
245
- # datepicker expects format 2020-11-29 11:59:59
246
- range.first.strftime('%F %T')..range.last.strftime('%F %T')
247
- end
244
+ }
248
245
  end
249
246
  end
250
247
  end
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.2.12'.freeze
2
+ VERSION = '0.2.13'.freeze
3
3
  end
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.12
4
+ version: 0.2.13
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-04-10 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
220
  - !ruby/object:Gem::Version
221
221
  version: '0'
222
222
  requirements: []
223
- rubygems_version: 3.0.3
223
+ rubygems_version: 3.2.15
224
224
  signing_key:
225
225
  specification_version: 4
226
226
  summary: Gem that simplify using datatables with Ruby on Rails and Sinatra.