trk_datatables 0.1.11 → 0.1.12

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: 6567d47eb0ced6f562a01e2c706bb7b56089f8e33ee6a3236e0becf23577cea1
4
- data.tar.gz: 81e71aea8bd52a67c6566fa226b11f52190bf3c09c31bb070245c6db34171c82
3
+ metadata.gz: 689f7693808d3e9a2401c9b6fdbb6481b8d2d642e31064eb2f38e704ed95c5a2
4
+ data.tar.gz: b5f6ba211d7fcec99e9025a51bd0f6fc83d69ee3e10eeb6e1df10f2c41ddf3e2
5
5
  SHA512:
6
- metadata.gz: bf8a9d267d2e4db6a41051cb219016fdc4adde2fe4c663f67cad7c8eb0657820df810ef9780e60fdeb514d459e19f18cf573505deb7f1ad61f1f30c37de9d51d
7
- data.tar.gz: e94e94b04cd08ef33882a4eff9ec8643c4adc2430a5080a9b36fcc6162809f19562d85e24c460cbfbe632ed28837099a69687b87ed7b2a4554657473fab5b31b
6
+ metadata.gz: 566e9b5010b7b75bb0a1cc4ee3adf937a9cdfde97826007665a4e4b5edf80d4a970a1705700e66bf30a3c629da5bc3da66a069858e3670343284f66075b06d90
7
+ data.tar.gz: '08e33268ee12bb569476fafb899c8938ef7b272d3ea7272e4c851113ffde0b6fba94630f6363dd61e874f465622ab94c9fff638b7ab48c186c8c21a96c127fcc'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.1.11)
4
+ trk_datatables (0.1.12)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -141,20 +141,39 @@ end
141
141
 
142
142
  ### Column 'ILIKE' search
143
143
 
144
- All columns are by default caster to string and `ILIKE` is perfomed.
145
- There is short notation if you do not need any specific column configuration,
146
- for example custom title (in which case you need to define columns as key/value
147
- pairs).
144
+ All columns are by default casted to string and `ILIKE` is perfomed.
145
+
146
+ If you do not need any specific column configuration,
147
+ for example custom `title`, than instead of defining columns as key/value
148
+ pairs, you can define them in one line as array of column_key.
148
149
 
149
150
  ```
150
151
  # app/datatables/posts_datatable.rb
151
152
  class PostsDatatable < TrkDatatables::ActiveRecord
152
153
  def columns
153
- # %w[posts.id posts.title posts.body] or even shorter using map
154
+ # instead of
155
+ # {
156
+ # id: {},
157
+ # title: {},
158
+ # body: {},
159
+ # }.each_with_object({}) { |(key, value), obj| obj["posts.#{key}"] = value }
160
+ # you can use one liner
161
+ # %w[posts.id posts.title posts.body]
162
+ # or even shorter using map
154
163
  %i[id title body].map { |col| "posts.#{col}" }
155
164
  end
156
165
  end
157
166
  ```
167
+
168
+ For specific columns you can use following keys
169
+
170
+ * `title: 'My Title'` set up column header
171
+ * `search: false` disable searching for this column
172
+ * `order: false` disable ordering for this column
173
+ * `predefined_ranges: {}` add ranges for datetime fields
174
+ * `select_options: Post.statuses` generate select box
175
+ * `hide: true` hide column
176
+
158
177
  ### Column 'BETWEEN' search
159
178
 
160
179
  For column search when search string contains BETWEEN_SEPARATOR (` - `) and
@@ -240,7 +259,7 @@ def columns
240
259
  end
241
260
 
242
261
  # in view
243
- link_to 'Active', search_posts_path(PostsDatatable.params_set('posts.status':
262
+ link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status':
244
263
  Post.statues.values_at(:published, :promoted)))
245
264
  ```
246
265
 
@@ -272,20 +291,43 @@ use empty column_key
272
291
  end
273
292
  ```
274
293
 
294
+ ### Default order and page length
295
+
296
+ You can override default order (index and direction) and default page length so
297
+ if user did not request some order or page length (and if it is not found in
298
+ save preferences) and this default values will be used
299
+
300
+ ```
301
+ # app/datatables/posts_datatable.rb
302
+ class PostsDatatable
303
+ # when we show some invoice_no on first column, and that is reset every year
304
+ # on first april, thatn it is better is to use date column ordering
305
+ def default_order
306
+ [[3, :desc]]
307
+ end
308
+
309
+ def default_page_length
310
+ 20
311
+ end
312
+ end
313
+ ```
314
+
275
315
  ### Params
276
316
 
277
317
  To set parameters that you can use for links to set column search value, use
278
318
  this `PostsDatatable.param_set` for example
279
319
 
280
320
  ```
281
- link_to 'Posts for my@email.com and my_title', \
321
+ link_to 'Active posts for my@email.com', \
282
322
  posts_path(
283
- PostsDatatable.params_set('users.email': 'my@email.com', 'posts.title': 'my_title')
284
- .merge(user_id: 1)
323
+ PostsDatatable.param_set('users.email', 'my@email.com')
324
+ .deep_merge(PostsDatatable.param_set('posts.status', Post.statuses.values_at(:published, :promoted))
325
+ .deep_merge(user_id: 1)
285
326
  )
286
- # will generate
287
327
  ```
288
328
 
329
+ This will fill proper column search values so you do not need to do it manually (`post_path(:columns=>{"3"=>{:search=>{:value=>"my@email.com"}}, "2"=>{:search=>{:value=>"1|2"}}}, :user_id=>1)`)
330
+
289
331
  If you need, you can fetch params with this helper
290
332
 
291
333
  ```
@@ -420,6 +462,15 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
420
462
 
421
463
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
422
464
 
465
+ Instead of installing you can point directly to your path
466
+ ```
467
+ # Gemfile
468
+ # for index, ordering, pagination and searching
469
+ # gem 'trk_datatables', '~>0.1'
470
+ # no need to `bundle update trk_datatables` when switch to local
471
+ gem 'trk_datatables', path: '~/gems/trk_datatables'
472
+ ```
473
+
423
474
  To generate docs you can run
424
475
 
425
476
  ```
@@ -1,8 +1,8 @@
1
1
  module TrkDatatables
2
+ # TODO: extract those to configuration options
2
3
  BETWEEN_SEPARATOR = ' - '.freeze
3
4
  MULTIPLE_OPTION_SEPARATOR = '|'.freeze
4
- DEFAULT_ORDER = [[0, :desc]].freeze
5
- DEFAULT_PAGE_LENGTH = 10
5
+ # maximum page length = 100 (we should not believe params)
6
6
 
7
7
  class Error < StandardError
8
8
  end
@@ -109,11 +109,19 @@ module TrkDatatables
109
109
  @preferences.set :order, @dt_params.dt_orders
110
110
  else
111
111
  check_value = ->(r) { r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) }
112
- @dt_orders_or_default = @preferences.get(:order, check_value) || DEFAULT_ORDER
112
+ @dt_orders_or_default = @preferences.get(:order, check_value) || default_order
113
113
  end
114
114
  @dt_orders_or_default
115
115
  end
116
116
 
117
+ def default_order
118
+ [[0, :desc]].freeze
119
+ end
120
+
121
+ def default_page_length
122
+ 10
123
+ end
124
+
117
125
  def dt_per_page_or_default
118
126
  return @dt_per_page_or_default if defined? @dt_per_page_or_default
119
127
 
@@ -122,7 +130,7 @@ module TrkDatatables
122
130
  @preferences.set :per_page, @dt_params.dt_per_page
123
131
  @dt_params.dt_per_page
124
132
  else
125
- @preferences.get(:per_page) || DEFAULT_PAGE_LENGTH
133
+ @preferences.get(:per_page) || default_page_length
126
134
  end
127
135
  end
128
136
 
@@ -134,23 +142,20 @@ module TrkDatatables
134
142
  # posts_path(PostsDatatable.params('posts.status': :published,
135
143
  # 'users.email: 'my@email.com')
136
144
  #
137
- # You can always use your params for filtering outside of datatable
145
+ # You can always use your params for filtering outside of datatable and
146
+ # merge to params
138
147
  # @example
139
148
  # link_to 'Published posts for user1',
140
- # posts_path(PostsDatatable.params_set('posts.status': :published).merge(user_id: user1.id))
141
- def self.params_set(attr)
149
+ # posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))
150
+ def self.param_set(column_key, value)
142
151
  datatable = new OpenStruct.new(params: {})
143
- result = {}
144
- attr.each do |column_key, value|
145
- value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
146
- column_index = datatable.index_by_column_key column_key
147
- result = result.deep_merge DtParams.param_set column_index, value
148
- end
149
- result
152
+ value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
153
+ column_index = datatable.index_by_column_key column_key
154
+ DtParams.param_set column_index, value
150
155
  end
151
156
 
152
157
  # We need this method publicly available since we use it for class method
153
- # params_set
158
+ # param_set
154
159
  def index_by_column_key(column_key)
155
160
  @column_key_options.index_by_column_key column_key
156
161
  end
@@ -17,10 +17,11 @@ module TrkDatatables
17
17
  TITLE_OPTION = :title
18
18
  SELECT_OPTIONS = :select_options
19
19
  PREDEFINED_RANGES = :predefined_ranges
20
+ HIDE_OPTION = :hide
20
21
  # this will load date picker
21
22
  # SEARCH_OPTION_DATE_VALUE = :date
22
23
  # SEARCH_OPTION_DATETIME_VALUE = :datetime
23
- COLUMN_OPTIONS = [SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS, PREDEFINED_RANGES].freeze
24
+ COLUMN_OPTIONS = [SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS, PREDEFINED_RANGES, HIDE_OPTION].freeze
24
25
 
25
26
  STRING_TYPE_CAST_POSTGRES = 'VARCHAR'.freeze
26
27
  STRING_TYPE_CAST_MYSQL = 'CHAR'.freeze
@@ -170,6 +171,7 @@ module TrkDatatables
170
171
  res = {}
171
172
  res['data-searchable'] = false if column_options[SEARCH_OPTION] == false
172
173
  res['data-orderable'] = false if column_options[ORDER_OPTION] == false
174
+ res['data-datatable-hide-column'] = true if column_options[HIDE_OPTION] == true
173
175
  if %i[date datetime].include? column_type_in_db
174
176
  res['data-datatable-range'] = column_type_in_db == :datetime ? :datetime : true
175
177
  if column_options[PREDEFINED_RANGES].present? ||
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.1.11'.freeze
2
+ VERSION = '0.1.12'.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.1.11
4
+ version: 0.1.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: 2019-09-10 00:00:00.000000000 Z
11
+ date: 2019-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -203,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  - !ruby/object:Gem::Version
204
204
  version: '0'
205
205
  requirements: []
206
- rubyforge_project:
207
- rubygems_version: 2.7.6
206
+ rubygems_version: 3.0.4
208
207
  signing_key:
209
208
  specification_version: 4
210
209
  summary: Gem that simplify using datatables with Ruby on Rails and Sinatra.