datagrid 2.0.0.pre.alpha → 2.0.0
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 +4 -4
- data/.yardopts +2 -0
- data/README.md +1 -3
- data/datagrid.gemspec +2 -1
- data/lib/datagrid/columns.rb +76 -75
- data/lib/datagrid/configuration.rb +19 -3
- data/lib/datagrid/filters/base_filter.rb +0 -1
- data/lib/datagrid/filters/extended_boolean_filter.rb +0 -1
- data/lib/datagrid/filters/float_filter.rb +0 -1
- data/lib/datagrid/filters.rb +22 -30
- data/lib/datagrid/generators/scaffold.rb +0 -1
- data/lib/datagrid/helper.rb +51 -59
- data/lib/datagrid/version.rb +1 -1
- data/lib/datagrid.rb +0 -10
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4f9903c41d2efb4c914ddf949db685228644e1ba071e8e2337491e0b91e0bbd
|
4
|
+
data.tar.gz: 937aea06689389c091b3be16aabaa36d8c828b8a8e97f220eaad403975371c6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3a5ef4923f03bd41bc54c2cc8ad6261f4a1e3302c4aad83a58868d539d69527a6cf838ee6efdf621fa0a47dabd023fa041773dde723f35f0feaf59e0b5effe6
|
7
|
+
data.tar.gz: 0a4c6aca85619ab3a71c125717d6f6f5209eafcb7a509df76afaac91e70790f199fea2ac796ebb9c05d088c8f4d40b42c16d6a49a28661ae1da7703c86c1ff92
|
data/.yardopts
ADDED
data/README.md
CHANGED
@@ -23,8 +23,6 @@ including admin panels, analytics and data browsers:
|
|
23
23
|
* Sequel
|
24
24
|
* Array (in-memory data of smaller scale)
|
25
25
|
|
26
|
-
[Create an issue](https://github.com/bogdan/datagrid/issues/new) if you want more.
|
27
|
-
|
28
26
|
## Datagrid Philosophy
|
29
27
|
|
30
28
|
1. Expressive DSL complements OOD instead of replacing it.
|
@@ -164,7 +162,7 @@ end
|
|
164
162
|
Some formatting options are also available.
|
165
163
|
Each column is sortable.
|
166
164
|
|
167
|
-
[More about columns](https://
|
165
|
+
[More about columns](https://rubydoc.info/gems/datagrid/Datagrid/Columns)
|
168
166
|
|
169
167
|
### Front end
|
170
168
|
|
data/datagrid.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
"CHANGELOG.md",
|
20
20
|
"README.md",
|
21
21
|
"datagrid.gemspec",
|
22
|
+
".yardopts",
|
22
23
|
]
|
23
24
|
s.files += `git ls-files | grep -E '^(app|lib|templates)'`.split("\n")
|
24
25
|
s.homepage = "https://github.com/bogdan/datagrid"
|
@@ -27,7 +28,7 @@ Gem::Specification.new do |s|
|
|
27
28
|
s.metadata = {
|
28
29
|
"homepage_uri" => s.homepage,
|
29
30
|
"bug_tracker_uri" => "#{s.homepage}/issues",
|
30
|
-
"documentation_uri" => "
|
31
|
+
"documentation_uri" => "https://rubydoc.info/gems/datagrid",
|
31
32
|
"changelog_uri" => "#{s.homepage}/blob/main/CHANGELOG.md",
|
32
33
|
"source_code_uri" => s.homepage,
|
33
34
|
"rubygems_mfa_required" => "true",
|
data/lib/datagrid/columns.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "datagrid/utils"
|
4
4
|
require "active_support/core_ext/class/attribute"
|
5
|
+
require "datagrid/columns/column"
|
5
6
|
|
6
7
|
module Datagrid
|
7
8
|
# Defines a column to be used for displaying data in a Datagrid.
|
@@ -199,33 +200,30 @@ module Datagrid
|
|
199
200
|
# presenter.user.created_at
|
200
201
|
# end
|
201
202
|
module Columns
|
202
|
-
require "datagrid/columns/column"
|
203
|
-
|
204
203
|
# @!method default_column_options=(value)
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
# self.default_column_options = { html: true }
|
204
|
+
# @param [Hash] value default options passed to {#column} method call
|
205
|
+
# @return [Hash] default options passed to {#column} method call
|
206
|
+
# @example Disable default order
|
207
|
+
# self.default_column_options = { order: false }
|
208
|
+
# @example Makes entire report HTML
|
209
|
+
# self.default_column_options = { html: true }
|
212
210
|
|
213
211
|
# @!method default_column_options
|
214
|
-
#
|
215
|
-
#
|
212
|
+
# @return [Hash] default options passed to {#column} method call
|
213
|
+
# @see #default_column_options=
|
216
214
|
|
217
215
|
# @!method batch_size=(value)
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
216
|
+
# Specify a default batch size when generating CSV or just data.
|
217
|
+
# @param [Integer] value a batch size when generating CSV or just data. Default: 1000
|
218
|
+
# @return [void]
|
219
|
+
# @example Set batch size to 500
|
220
|
+
# self.batch_size = 500
|
221
|
+
# @example Disable batches
|
222
|
+
# self.batch_size = nil
|
225
223
|
|
226
224
|
# @!method batch_size
|
227
|
-
#
|
228
|
-
#
|
225
|
+
# @return [Integer]
|
226
|
+
# @see #batch_size=
|
229
227
|
|
230
228
|
# @visibility private
|
231
229
|
# @param [Object] base
|
@@ -253,37 +251,31 @@ module Datagrid
|
|
253
251
|
filter_columns(columns_array, *column_names, data: data, html: html)
|
254
252
|
end
|
255
253
|
|
256
|
-
# Defines new datagrid column
|
257
|
-
#
|
254
|
+
# Defines a new datagrid column
|
258
255
|
# @param name [Symbol] column name
|
259
256
|
# @param query [String, nil] a string representing the query to select this column (supports only ActiveRecord)
|
260
|
-
# @param options [Hash{Symbol => Object}] hash of options
|
261
257
|
# @param block [Block] proc to calculate a column value
|
258
|
+
# @option options [Boolean, String] html Determines if the column should be present
|
259
|
+
# in the HTML table and how it is formatted.
|
260
|
+
# @option options [String, Array<Symbol>] order Determines if the column can be sortable and
|
261
|
+
# specifies the ORM ordering method.
|
262
|
+
# Example: `"created_at, id"` for ActiveRecord, `[:created_at, :id]` for Mongoid.
|
263
|
+
# @option options [String] order_desc Specifies a descending order for the column
|
264
|
+
# (used when `:order` cannot be easily reversed by the ORM).
|
265
|
+
# @option options [Boolean, Proc] order_by_value Enables Ruby-level ordering for the column.
|
266
|
+
# Warning: Sorting large datasets in Ruby is not recommended.
|
267
|
+
# If `true`, Datagrid orders by the column value.
|
268
|
+
# If a block is provided, Datagrid orders by the block's return value.
|
269
|
+
# @option options [Boolean] mandatory If `true`, the column will never be hidden by the `#column_names` selection.
|
270
|
+
# @option options [Symbol] before Places the column before the specified column when determining order.
|
271
|
+
# @option options [Symbol] after Places the column after the specified column when determining order.
|
272
|
+
# @option options [Boolean, Proc] if conditions when a column is available.
|
273
|
+
# @option options [Boolean, Proc] unless conditions when a column is not available.
|
274
|
+
# @option options [Symbol, Array<Symbol>] preload Specifies associations
|
275
|
+
# to preload for the column within the scope.
|
276
|
+
# @option options [Hash] tag_options Specifies HTML attributes for the `<td>` or `<th>` of the column.
|
277
|
+
# Example: `{ class: "content-align-right", "data-group": "statistics" }`.
|
262
278
|
# @return [Datagrid::Columns::Column]
|
263
|
-
#
|
264
|
-
# Available options:
|
265
|
-
#
|
266
|
-
# * <tt>html</tt> - determines if current column should be present in html table and how is it formatted
|
267
|
-
# * <tt>order</tt> - determines if this column could be sortable and how.
|
268
|
-
# The value of order is explicitly passed to ORM ordering method.
|
269
|
-
# Example: <tt>"created_at, id"</tt> for ActiveRecord, <tt>[:created_at, :id]</tt> for Mongoid
|
270
|
-
# * <tt>order_desc</tt> - determines a descending order for given column
|
271
|
-
# (only in case when <tt>:order</tt> can not be easily reversed by ORM)
|
272
|
-
# * <tt>order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level.
|
273
|
-
# Warning: using ruby to order large datasets is very unrecommended.
|
274
|
-
# If set to true - datagrid will use column value to order by this column
|
275
|
-
# If block is given - datagrid will use value returned from block
|
276
|
-
# * <tt>mandatory</tt> - if true, column will never be hidden with #column_names selection
|
277
|
-
# * <tt>url</tt> - a proc with one argument, pass this option to easily convert the value into an URL
|
278
|
-
# * <tt>before</tt> - determines the position of this column, by adding it before the column passed here
|
279
|
-
# * <tt>after</tt> - determines the position of this column, by adding it after the column passed here
|
280
|
-
# * <tt>if</tt> - the column is shown if the reult of calling this argument is true
|
281
|
-
# * <tt>unless</tt> - the column is shown unless the reult of calling this argument is true
|
282
|
-
# * <tt>preload</tt> - spefies which associations of the scope should be preloaded for this column
|
283
|
-
# * `tag_options` - specify HTML attributes to be set for `<td>` or `<th>` of a column
|
284
|
-
# Example: `{ class: "content-align-right", "data-group": "statistics" }`
|
285
|
-
#
|
286
|
-
# @see https://github.com/bogdan/datagrid/wiki/Columns
|
287
279
|
def column(name, query = nil, **options, &block)
|
288
280
|
define_column(columns_array, name, query, **options, &block)
|
289
281
|
end
|
@@ -333,10 +325,10 @@ module Datagrid
|
|
333
325
|
# Defines a model decorator that will be used to define a column value.
|
334
326
|
# All column blocks will be given a decorated version of the model.
|
335
327
|
# @return [void]
|
336
|
-
# @example
|
328
|
+
# @example Wrapping a model with presenter
|
337
329
|
# decorate { |user| UserPresenter.new(user) }
|
338
|
-
#
|
339
|
-
# decorate { UserPresenter }
|
330
|
+
# @example A shortcut for doing the same
|
331
|
+
# decorate { UserPresenter }
|
340
332
|
def decorate(model = nil, &block)
|
341
333
|
if !model && !block
|
342
334
|
raise ArgumentError, "decorate needs either a block to define decoration or a model to decorate"
|
@@ -403,14 +395,16 @@ module Datagrid
|
|
403
395
|
)
|
404
396
|
end
|
405
397
|
|
406
|
-
# @param column_names [Array<String>] list of column names
|
398
|
+
# @param column_names [Array<String, Symbol>] list of column names
|
399
|
+
# if you want to limit data only to specified columns
|
407
400
|
# @return [Array<String>] human readable column names. See also "Localization" section
|
408
401
|
def header(*column_names)
|
409
402
|
data_columns(*column_names).map(&:header)
|
410
403
|
end
|
411
404
|
|
412
405
|
# @param asset [Object] asset from datagrid scope
|
413
|
-
# @param column_names [Array<String>] list of column names
|
406
|
+
# @param column_names [Array<String, Symbol>] list of column names
|
407
|
+
# if you want to limit data only to specified columns
|
414
408
|
# @return [Array<Object>] column values for given asset
|
415
409
|
def row_for(asset, *column_names)
|
416
410
|
data_columns(*column_names).map do |column|
|
@@ -419,7 +413,8 @@ module Datagrid
|
|
419
413
|
end
|
420
414
|
|
421
415
|
# @param asset [Object] asset from datagrid scope
|
422
|
-
# @return [Hash] A mapping where keys are column names and
|
416
|
+
# @return [Hash] A mapping where keys are column names and
|
417
|
+
# values are column values for the given asset
|
423
418
|
def hash_for(asset)
|
424
419
|
result = {}
|
425
420
|
data_columns.each do |column|
|
@@ -428,7 +423,8 @@ module Datagrid
|
|
428
423
|
result
|
429
424
|
end
|
430
425
|
|
431
|
-
# @param column_names [Array<String>] list of column names
|
426
|
+
# @param column_names [Array<String,Symbol>] list of column names
|
427
|
+
# if you want to limit data only to specified columns
|
432
428
|
# @return [Array<Array<Object>>] with data for each row in datagrid assets without header
|
433
429
|
def rows(*column_names)
|
434
430
|
map_with_batches do |asset|
|
@@ -436,7 +432,8 @@ module Datagrid
|
|
436
432
|
end
|
437
433
|
end
|
438
434
|
|
439
|
-
# @param column_names [Array<String>] list of column names
|
435
|
+
# @param column_names [Array<String, Symbol>] list of column names
|
436
|
+
# if you want to limit data only to specified columns.
|
440
437
|
# @return [Array<Array<Object>>] data for each row in datagrid assets with header.
|
441
438
|
def data(*column_names)
|
442
439
|
rows(*column_names).unshift(header(*column_names))
|
@@ -461,7 +458,7 @@ module Datagrid
|
|
461
458
|
end
|
462
459
|
end
|
463
460
|
|
464
|
-
# @param column_names [Array<String>]
|
461
|
+
# @param column_names [Array<String,Symbol>]
|
465
462
|
# @param options [Hash] CSV generation options
|
466
463
|
# @return [String] a CSV representation of the data in the grid
|
467
464
|
#
|
@@ -514,7 +511,7 @@ module Datagrid
|
|
514
511
|
columns(*column_names, data: data, html: true)
|
515
512
|
end
|
516
513
|
|
517
|
-
# Finds a column
|
514
|
+
# Finds a column by name
|
518
515
|
# @param name [String, Symbol] column name to be found
|
519
516
|
# @return [Datagrid::Columns::Column, nil]
|
520
517
|
def column_by_name(name)
|
@@ -522,22 +519,21 @@ module Datagrid
|
|
522
519
|
end
|
523
520
|
|
524
521
|
# Gives ability to have a different formatting for CSV and HTML column value.
|
525
|
-
#
|
526
|
-
# @example
|
522
|
+
# @example Formating using Rails view helpers
|
527
523
|
# column(:name) do |model|
|
528
524
|
# format(model.name) do |value|
|
529
525
|
# tag.strong(value)
|
530
526
|
# end
|
531
527
|
# end
|
532
|
-
#
|
528
|
+
# @example Formatting using a separated view template
|
533
529
|
# column(:company) do |model|
|
534
530
|
# format(model.company.name) do
|
535
|
-
# render partial: "company_with_logo", locals: {company: model.company }
|
531
|
+
# render partial: "companies/company_with_logo", locals: {company: model.company }
|
536
532
|
# end
|
537
533
|
# end
|
538
534
|
# @return [Datagrid::Columns::Column::ResponseFormat] Format object
|
539
535
|
def format(value, &block)
|
540
|
-
if
|
536
|
+
if block
|
541
537
|
self.class.format(value, &block)
|
542
538
|
else
|
543
539
|
# don't override Object#format method
|
@@ -545,26 +541,26 @@ module Datagrid
|
|
545
541
|
end
|
546
542
|
end
|
547
543
|
|
544
|
+
# @param [Object] asset one of the assets from grid scope
|
548
545
|
# @return [Datagrid::Columns::DataRow] an object representing a grid row.
|
549
546
|
# @example
|
550
|
-
#
|
551
|
-
#
|
552
|
-
#
|
553
|
-
#
|
554
|
-
#
|
555
|
-
#
|
556
|
-
#
|
557
|
-
#
|
547
|
+
# class MyGrid
|
548
|
+
# scope { User }
|
549
|
+
# column(:id)
|
550
|
+
# column(:name)
|
551
|
+
# column(:number_of_purchases) do |user|
|
552
|
+
# user.purchases.count
|
553
|
+
# end
|
554
|
+
# end
|
558
555
|
#
|
559
|
-
#
|
560
|
-
#
|
561
|
-
#
|
556
|
+
# row = MyGrid.new.data_row(User.last)
|
557
|
+
# row.id # => user.id
|
558
|
+
# row.number_of_purchases # => user.purchases.count
|
562
559
|
def data_row(asset)
|
563
560
|
::Datagrid::Columns::DataRow.new(self, asset)
|
564
561
|
end
|
565
562
|
|
566
563
|
# Defines a column at instance level
|
567
|
-
#
|
568
564
|
# @see Datagrid::Columns::ClassMethods#column
|
569
565
|
def column(name, query = nil, **options, &block)
|
570
566
|
self.class.define_column(columns_array, name, query, **options, &block)
|
@@ -578,7 +574,6 @@ module Datagrid
|
|
578
574
|
|
579
575
|
# @return [Array<Datagrid::Columns::Column>] all columns
|
580
576
|
# that are possible to be displayed for the current grid object
|
581
|
-
#
|
582
577
|
# @example
|
583
578
|
# class MyGrid
|
584
579
|
# filter(:search) {|scope, value| scope.full_text_search(value)}
|
@@ -600,6 +595,8 @@ module Datagrid
|
|
600
595
|
end
|
601
596
|
end
|
602
597
|
|
598
|
+
# @param [String,Symbol] column_name column name
|
599
|
+
# @param [Object] asset one of the assets from grid scope
|
603
600
|
# @return [Object] a cell data value for given column name and asset
|
604
601
|
def data_value(column_name, asset)
|
605
602
|
column = column_by_name(column_name)
|
@@ -611,6 +608,9 @@ module Datagrid
|
|
611
608
|
end
|
612
609
|
end
|
613
610
|
|
611
|
+
# @param [String,Symbol] column_name column name
|
612
|
+
# @param [Object] asset one of the assets from grid scope
|
613
|
+
# @param [ActionView::Base] context view context object
|
614
614
|
# @return [Object] a cell HTML value for given column name and asset and view context
|
615
615
|
def html_value(column_name, context, asset)
|
616
616
|
column = column_by_name(column_name)
|
@@ -624,6 +624,7 @@ module Datagrid
|
|
624
624
|
end
|
625
625
|
end
|
626
626
|
|
627
|
+
# @param [Object] model one of the assets from grid scope
|
627
628
|
# @return [Object] a decorated version of given model if decorator is specified or the model otherwise.
|
628
629
|
def decorate(model)
|
629
630
|
self.class.decorate(model)
|
@@ -1,6 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Datagrid
|
4
|
+
# @return [Configuration] current Datagrid configuration
|
5
|
+
def self.configuration
|
6
|
+
@configuration ||= Configuration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
# @yield block to configure
|
10
|
+
# @yieldparam [Configuration] configuration
|
11
|
+
# @yieldreturn [void]
|
12
|
+
# @return [void] configure datagrid
|
13
|
+
# @see Datagrid::Configuration
|
14
|
+
def self.configure(&block)
|
15
|
+
block.call(configuration)
|
16
|
+
end
|
17
|
+
|
4
18
|
# ## Configuration
|
5
19
|
#
|
6
20
|
# Datagrid provides several configuration options.
|
@@ -11,12 +25,14 @@ module Datagrid
|
|
11
25
|
# Datagrid.configure do |config|
|
12
26
|
# # Defines date formats that can be used to parse dates.
|
13
27
|
# # Note: Multiple formats can be specified. The first format is used to format dates as strings,
|
14
|
-
# # while other formats are used only for parsing dates
|
15
|
-
#
|
28
|
+
# # while other formats are used only for parsing dates
|
29
|
+
# # from strings (e.g., if your app supports multiple formats).
|
30
|
+
# config.date_formats = "%m/%d/%Y", "%Y-%m-%d"
|
16
31
|
#
|
17
32
|
# # Defines timestamp formats that can be used to parse timestamps.
|
18
33
|
# # Note: Multiple formats can be specified. The first format is used to format timestamps as strings,
|
19
|
-
# # while other formats are used only for parsing timestamps
|
34
|
+
# # while other formats are used only for parsing timestamps
|
35
|
+
# # from strings (e.g., if your app supports multiple formats).
|
20
36
|
# config.datetime_formats = ["%m/%d/%Y %h:%M", "%Y-%m-%d %h:%M:%s"]
|
21
37
|
# end
|
22
38
|
# ```
|
data/lib/datagrid/filters.rb
CHANGED
@@ -161,7 +161,6 @@ module Datagrid
|
|
161
161
|
|
162
162
|
extend ActiveSupport::Concern
|
163
163
|
|
164
|
-
# @!visibility private
|
165
164
|
included do
|
166
165
|
include Datagrid::Core
|
167
166
|
class_attribute :filters_array, default: []
|
@@ -192,35 +191,28 @@ module Datagrid
|
|
192
191
|
# @param [Hash] options hash of options
|
193
192
|
# @param [Proc] block proc to apply the filter
|
194
193
|
# @return [Datagrid::Filters::BaseFilter] Filter definition object
|
195
|
-
# @see
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
# * <tt>:if</tt> - specify the condition when the filter can be dislayed and used.
|
218
|
-
# Accepts a block or a symbol with an instance method name
|
219
|
-
# * <tt>:unless</tt> - specify the reverse condition when the filter can be dislayed and used.
|
220
|
-
# Accepts a block or a symbol with an instance method name
|
221
|
-
# * <tt>:input_options</tt> - options passed when rendering html input tag attributes.
|
222
|
-
# Use <tt>input_options.type</tt> to control input type including <tt>textarea</tt>.
|
223
|
-
# * <tt>:label_options</tt> - options passed when rendering html label tag attributes
|
194
|
+
# @see Datagrid::Filters
|
195
|
+
# @option options [String] header Determines the header of the filter.
|
196
|
+
# @option options [Object, Proc] default The default filter value. Accepts a `Proc` to allow dynamic calculation.
|
197
|
+
# @option options [Boolean] range Whether the filter accepts two values to define a range.
|
198
|
+
# Supported by types: `:integer`, `:float`, `:date`, `:datetime`, and `:string`.
|
199
|
+
# @option options [Boolean, String] multiple If true, allows multiple values for the filter.
|
200
|
+
# Strings are parsed using a separator (default: `,`). Can accept a custom separator. Default: `false`.
|
201
|
+
# @option options [Boolean] allow_nil Whether the filter value can be `nil`. Default: `false`.
|
202
|
+
# @option options [Boolean] allow_blank Whether the filter value can be blank. Default: `false`.
|
203
|
+
# @option options [Symbol] before Specifies the position of this filter by placing it before another filter.
|
204
|
+
# Used with the `datagrid_form_for` helper.
|
205
|
+
# @option options [Symbol] after Specifies the position of this filter by placing it after another filter.
|
206
|
+
# Used with the `datagrid_form_for` helper.
|
207
|
+
# @option options [Boolean] dummy If true, the filter is not applied automatically and
|
208
|
+
# is only displayed in the form. Useful for manual application.
|
209
|
+
# @option options [Proc, Symbol] if Specifies a condition under which the filter is displayed and used.
|
210
|
+
# Accepts a block or the name of an instance method.
|
211
|
+
# @option options [Proc, Symbol] unless Specifies a condition under which the filter is NOT displayed or used.
|
212
|
+
# Accepts a block or the name of an instance method.
|
213
|
+
# @option options [Hash] input_options Options passed to the HTML input tag for rendering attributes.
|
214
|
+
# Use `input_options[:type]` to control the input type (e.g., `textarea`).
|
215
|
+
# @option options [Hash] label_options Options passed to the HTML label tag for rendering attributes.
|
224
216
|
def filter(name, type = :default, **options, &block)
|
225
217
|
klass = type.is_a?(Class) ? type : FILTER_TYPES[type]
|
226
218
|
raise ConfigurationError, "filter class #{type.inspect} not found" unless klass
|
data/lib/datagrid/helper.rb
CHANGED
@@ -17,7 +17,8 @@ module Datagrid
|
|
17
17
|
# [built-in CSS](https://github.com/bogdan/datagrid/blob/master/app/assets/stylesheets/datagrid.sass).
|
18
18
|
#
|
19
19
|
# Datagrid includes helpers and a form builder for easy frontend generation.
|
20
|
-
# If you need a fully-featured custom GUI, create your templates manually
|
20
|
+
# If you need a fully-featured custom GUI, create your templates manually
|
21
|
+
# with the help of the {Datagrid::Columns} API.
|
21
22
|
#
|
22
23
|
# ## Controller and Routing
|
23
24
|
#
|
@@ -63,7 +64,8 @@ module Datagrid
|
|
63
64
|
#
|
64
65
|
# ### Advanced Method
|
65
66
|
#
|
66
|
-
# You can use Rails built-in tools to create a form.
|
67
|
+
# You can use Rails built-in tools to create a form.
|
68
|
+
# Additionally, Datagrid provides helpers to generate input/select elements for filters:
|
67
69
|
#
|
68
70
|
# ``` haml
|
69
71
|
# - form_with model: UserGrid.new, method: :get, url: users_path do |f|
|
@@ -240,7 +242,7 @@ module Datagrid
|
|
240
242
|
#
|
241
243
|
# https://github.com/bogdan/datagrid/blob/master/lib/datagrid/locale/en.yml
|
242
244
|
module Helper
|
243
|
-
# @param grid [Datagrid] grid object
|
245
|
+
# @param grid [Datagrid::Base] grid object
|
244
246
|
# @param column [Datagrid::Columns::Column, String, Symbol] column name
|
245
247
|
# @param model [Object] an object from grid scope
|
246
248
|
# @return [Object] individual cell value from the given grid, column name and model
|
@@ -264,19 +266,16 @@ module Datagrid
|
|
264
266
|
# Renders html table with columns defined in grid class.
|
265
267
|
# In the most common used you need to pass paginated collection
|
266
268
|
# to datagrid table because datagrid do not have pagination compatibilities:
|
267
|
-
#
|
268
|
-
#
|
269
|
-
# * <tt>:html</tt> - hash of attributes for <table> tag
|
270
|
-
# * <tt>:order</tt> - If false do not generate ordering controlls.
|
271
|
-
# Default: true.
|
272
|
-
# * <tt>:columns</tt> - Array of column names to display.
|
273
|
-
# Used in case when same grid class is used in different places
|
274
|
-
# and needs different columns. Default: all defined columns.
|
275
|
-
# * <tt>:partials</tt> - Path for partials lookup.
|
276
|
-
# Default: 'datagrid'.
|
277
|
-
# @param grid [Datagrid] grid object
|
269
|
+
# @param grid [Datagrid::Base] grid object
|
278
270
|
# @param assets [Array] objects from grid scope
|
279
271
|
# @param [Hash{Symbol => Object}] options HTML attributes to be passed to `<table>` tag
|
272
|
+
# @option options [Hash] html A hash of attributes for the `<table>` tag.
|
273
|
+
# @option options [Boolean] order Whether to generate ordering controls.
|
274
|
+
# If set to `false`, ordering controls are not generated. Default: `true`.
|
275
|
+
# @option options [Array<Symbol>] columns An array of column names to display.
|
276
|
+
# Use this when the same grid class is used in different contexts and requires different columns.
|
277
|
+
# Default: all defined columns.
|
278
|
+
# @option options [String] partials The path for partials lookup. Default: `'datagrid'`.
|
280
279
|
# @return [String] table tag HTML markup
|
281
280
|
# @example
|
282
281
|
# assets = grid.assets.page(params[:page])
|
@@ -294,16 +293,14 @@ module Datagrid
|
|
294
293
|
|
295
294
|
# Renders HTML table header for given grid instance using columns defined in it
|
296
295
|
#
|
297
|
-
#
|
298
|
-
#
|
299
|
-
#
|
300
|
-
#
|
301
|
-
#
|
302
|
-
#
|
303
|
-
#
|
304
|
-
#
|
305
|
-
# Default: 'datagrid'.
|
306
|
-
# @param grid [Datagrid] grid object
|
296
|
+
# @option options [Boolean] order Whether to display ordering controls built into the header.
|
297
|
+
# Default: `true`.
|
298
|
+
# @option options [Array<Symbol,String>] columns An array of column names to display.
|
299
|
+
# Use this when the same grid class is used in different contexts and requires different columns.
|
300
|
+
# Default: all defined columns.
|
301
|
+
# @option options [String] partials The path for partials lookup.
|
302
|
+
# Default: `'datagrid'`.
|
303
|
+
# @param grid [Datagrid::Base] grid object
|
307
304
|
# @param [Object] opts (deprecated) pass keyword arguments instead
|
308
305
|
# @param [Hash] options
|
309
306
|
# @return [String] HTML table header tag markup
|
@@ -321,22 +318,21 @@ module Datagrid
|
|
321
318
|
# Renders HTML table rows using given grid definition using columns defined in it.
|
322
319
|
# Allows to provide a custom layout for each for in place with a block
|
323
320
|
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
329
|
-
# * <tt>:partials</tt> - Path for partials lookup.
|
330
|
-
# Default: 'datagrid'.
|
331
|
-
#
|
321
|
+
# @option options [Array<Symbol>] columns An array of column names to display.
|
322
|
+
# Use this when the same grid class is used in different contexts and requires different columns.
|
323
|
+
# Default: all defined columns.
|
324
|
+
# @option options [String] partials The path for partials lookup.
|
325
|
+
# Default: `'datagrid'`.
|
332
326
|
# @return [String]
|
333
|
-
# @example
|
334
|
-
# = datagrid_rows(grid)
|
335
|
-
#
|
336
|
-
# = datagrid_rows(grid) do |row|
|
327
|
+
# @example Generic table rows Layout
|
328
|
+
# = datagrid_rows(grid)
|
329
|
+
# @example Custom Layout
|
330
|
+
# = datagrid_rows(grid) do |row|
|
337
331
|
# %tr
|
338
332
|
# %td= row.project_name
|
339
333
|
# %td.project-status{class: row.status}= row.status
|
334
|
+
# @param [Datagrid::Base] grid datagrid object
|
335
|
+
# @param [Array<Object>] assets assets as per defined in grid scope
|
340
336
|
def datagrid_rows(grid, assets = grid.assets, **options, &block)
|
341
337
|
safe_join(
|
342
338
|
assets.map do |asset|
|
@@ -346,11 +342,12 @@ module Datagrid
|
|
346
342
|
end
|
347
343
|
|
348
344
|
# @return [String] renders ordering controls for the given column name
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
#
|
353
|
-
#
|
345
|
+
# @option options [String] partials The path for partials lookup.
|
346
|
+
# Default: `'datagrid'`.
|
347
|
+
# @param [Datagrid::Base] grid datagrid object
|
348
|
+
# @param [Datagrid::Columns::Column] column
|
349
|
+
# @deprecated Put necessary code inline inside datagrid/head partial.
|
350
|
+
# See built-in partial for example.
|
354
351
|
def datagrid_order_for(grid, column, options = {})
|
355
352
|
Datagrid::Utils.warn_once(<<~MSG)
|
356
353
|
datagrid_order_for is deprecated.
|
@@ -362,15 +359,11 @@ module Datagrid
|
|
362
359
|
end
|
363
360
|
|
364
361
|
# Renders HTML for grid with all filters inputs and labels defined in it
|
365
|
-
#
|
366
|
-
#
|
367
|
-
#
|
368
|
-
#
|
369
|
-
#
|
370
|
-
# Example: 'datagrid_admin' results in using `app/views/datagrid_admin` partials.
|
371
|
-
# * <tt>:model</tt> - Datagrid object to be rendedred.
|
372
|
-
# * All options supported by Rails <tt>form_with</tt> helper
|
373
|
-
# @param grid [Datagrid] grid object
|
362
|
+
# @option options [String] partials Path for form partial lookup.
|
363
|
+
# Default: `'datagrid'`, which uses `app/views/datagrid/` partials.
|
364
|
+
# Example: `'datagrid_admin'` uses `app/views/datagrid_admin` partials.
|
365
|
+
# @option options [Datagrid::Base] model a Datagrid object to be rendered.
|
366
|
+
# @option options [Hash] All options supported by Rails `form_with` helper.
|
374
367
|
# @param [Hash{Symbol => Object}] options
|
375
368
|
# @return [String] form HTML tag markup
|
376
369
|
def datagrid_form_with(**options)
|
@@ -390,7 +383,7 @@ module Datagrid
|
|
390
383
|
# Default: 'datagrid'.
|
391
384
|
# * All options supported by Rails <tt>form_with</tt> helper
|
392
385
|
# @deprecated Use {#datagrid_form_with} instead.
|
393
|
-
# @param grid [Datagrid] grid object
|
386
|
+
# @param grid [Datagrid::Base] grid object
|
394
387
|
# @param [Hash] options
|
395
388
|
# @return [String] form HTML tag markup
|
396
389
|
def datagrid_form_for(grid, options = {})
|
@@ -409,25 +402,24 @@ module Datagrid
|
|
409
402
|
|
410
403
|
# Provides access to datagrid columns data.
|
411
404
|
# Used in case you want to build html table completelly manually
|
412
|
-
# @param grid [Datagrid] grid object
|
405
|
+
# @param grid [Datagrid::Base] grid object
|
413
406
|
# @param asset [Object] object from grid scope
|
414
407
|
# @param block [Proc] block with Datagrid::Helper::HtmlRow as an argument returning a HTML markup as a String
|
415
408
|
# @param [Hash{Symbol => Object}] options
|
416
409
|
# @return [Datagrid::Helper::HtmlRow, String] captured HTML markup if block given otherwise row object
|
417
|
-
# @example
|
418
|
-
#
|
410
|
+
# @example Render default layout for row
|
411
|
+
# <%= datagrid_row(grid, user, columns: [:first_name, :last_name, :actions]) %>
|
412
|
+
# @example Rendering custom layout for `first_name` and `last_name` columns
|
419
413
|
# <%= datagrid_row(grid, user) do |row| %>
|
420
414
|
# <tr>
|
421
415
|
# <td><%= row.first_name %></td>
|
422
416
|
# <td><%= row.last_name %></td>
|
423
417
|
# </tr>
|
424
418
|
# <% end %>
|
425
|
-
# @example
|
419
|
+
# @example Rendering custom layout passing a block
|
426
420
|
# <% row = datagrid_row(grid, user) %>
|
427
421
|
# First Name: <%= row.first_name %>
|
428
422
|
# Last Name: <%= row.last_name %>
|
429
|
-
# @example
|
430
|
-
# <%= datagrid_row(grid, user, columns: [:first_name, :last_name, :actions]) %>
|
431
423
|
def datagrid_row(grid, asset, **options, &block)
|
432
424
|
Datagrid::Helper::HtmlRow.new(self, grid, asset, options).tap do |row|
|
433
425
|
return capture(row, &block) if block_given?
|
@@ -435,7 +427,7 @@ module Datagrid
|
|
435
427
|
end
|
436
428
|
|
437
429
|
# Generates an ascending or descending order url for the given column
|
438
|
-
# @param grid [Datagrid] grid object
|
430
|
+
# @param grid [Datagrid::Base] grid object
|
439
431
|
# @param column [Datagrid::Columns::Column, String, Symbol] column name
|
440
432
|
# @param descending [Boolean] order direction, descending if true, otherwise ascending.
|
441
433
|
# @return [String] order layout HTML markup
|
@@ -475,7 +467,7 @@ module Datagrid
|
|
475
467
|
# row = datagrid_row(grid, user)
|
476
468
|
# row.class # => Datagrid::Helper::HtmlRow
|
477
469
|
# row.first_name # => "<strong>Bogdan</strong>"
|
478
|
-
# row.grid # =>
|
470
|
+
# row.grid # => Datagrid::Base object
|
479
471
|
# row.asset # => User object
|
480
472
|
# row.each do |value|
|
481
473
|
# puts value
|
data/lib/datagrid/version.rb
CHANGED
data/lib/datagrid.rb
CHANGED
@@ -4,7 +4,6 @@ require "action_view"
|
|
4
4
|
require "datagrid/configuration"
|
5
5
|
require "datagrid/engine"
|
6
6
|
|
7
|
-
# @main README.md
|
8
7
|
module Datagrid
|
9
8
|
# @!visibility private
|
10
9
|
def self.included(base)
|
@@ -19,15 +18,6 @@ module Datagrid
|
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
|
-
def self.configuration
|
23
|
-
@configuration ||= Configuration.new
|
24
|
-
end
|
25
|
-
|
26
|
-
# Configure
|
27
|
-
def self.configure(&block)
|
28
|
-
block.call(configuration)
|
29
|
-
end
|
30
|
-
|
31
21
|
class ConfigurationError < StandardError; end
|
32
22
|
class ArgumentError < ::ArgumentError; end
|
33
23
|
class ColumnUnavailableError < StandardError; end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datagrid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -33,6 +33,7 @@ extra_rdoc_files:
|
|
33
33
|
- LICENSE.txt
|
34
34
|
- README.md
|
35
35
|
files:
|
36
|
+
- ".yardopts"
|
36
37
|
- CHANGELOG.md
|
37
38
|
- LICENSE.txt
|
38
39
|
- README.md
|
@@ -93,7 +94,7 @@ licenses:
|
|
93
94
|
metadata:
|
94
95
|
homepage_uri: https://github.com/bogdan/datagrid
|
95
96
|
bug_tracker_uri: https://github.com/bogdan/datagrid/issues
|
96
|
-
documentation_uri: https://
|
97
|
+
documentation_uri: https://rubydoc.info/gems/datagrid
|
97
98
|
changelog_uri: https://github.com/bogdan/datagrid/blob/main/CHANGELOG.md
|
98
99
|
source_code_uri: https://github.com/bogdan/datagrid
|
99
100
|
rubygems_mfa_required: 'true'
|