datagrid 1.8.1 → 1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -6
- data/Readme.markdown +4 -4
- data/app/assets/stylesheets/datagrid.sass +1 -1
- data/app/views/datagrid/_form.html.erb +0 -1
- data/datagrid.gemspec +3 -3
- data/lib/datagrid/column_names_attribute.rb +3 -1
- data/lib/datagrid/columns.rb +268 -264
- data/lib/datagrid/core.rb +132 -133
- data/lib/datagrid/drivers/abstract_driver.rb +1 -2
- data/lib/datagrid/drivers/array.rb +10 -9
- data/lib/datagrid/drivers/mongoid.rb +1 -1
- data/lib/datagrid/filters/base_filter.rb +3 -3
- data/lib/datagrid/filters/date_filter.rb +1 -1
- data/lib/datagrid/filters/extended_boolean_filter.rb +6 -3
- data/lib/datagrid/filters.rb +67 -72
- data/lib/datagrid/form_builder.rb +2 -2
- data/lib/datagrid/ordering.rb +71 -74
- data/lib/datagrid/rspec.rb +2 -2
- data/lib/datagrid/scaffold.rb +3 -3
- data/lib/datagrid/utils.rb +7 -10
- data/lib/datagrid/version.rb +1 -1
- data/templates/base.rb.erb +27 -3
- metadata +8 -8
data/lib/datagrid/ordering.rb
CHANGED
@@ -27,7 +27,6 @@ module Datagrid
|
|
27
27
|
alias descending? descending
|
28
28
|
|
29
29
|
end
|
30
|
-
base.include InstanceMethods
|
31
30
|
end
|
32
31
|
|
33
32
|
# @!visibility private
|
@@ -37,96 +36,94 @@ module Datagrid
|
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
40
|
-
module InstanceMethods
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
# @!visibility private
|
41
|
+
def assets
|
42
|
+
check_order_valid!
|
43
|
+
apply_order(super)
|
44
|
+
end
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
46
|
+
# @return [Datagrid::Columns::Column, nil] a column definition that is currently used to order assets
|
47
|
+
# @example
|
48
|
+
# class MyGrid
|
49
|
+
# scope { Model }
|
50
|
+
# column(:id)
|
51
|
+
# column(:name)
|
52
|
+
# end
|
53
|
+
# MyGrid.new(order: "name").order_column # => #<Column name: "name", ...>
|
54
|
+
def order_column
|
55
|
+
order ? column_by_name(order) : nil
|
56
|
+
end
|
59
57
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
58
|
+
# @param column [String, Datagrid::Columns::Column]
|
59
|
+
# @return [Boolean] true if given grid is ordered by given column.
|
60
|
+
def ordered_by?(column)
|
61
|
+
order_column == column_by_name(column)
|
62
|
+
end
|
65
63
|
|
66
|
-
|
64
|
+
private
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
else
|
80
|
-
apply_desc_order(assets, order_column.order)
|
81
|
-
end
|
66
|
+
def apply_order(assets)
|
67
|
+
return assets unless order
|
68
|
+
if order_column.order_by_value?
|
69
|
+
assets = assets.sort_by do |asset|
|
70
|
+
order_column.order_by_value(asset, self)
|
71
|
+
end
|
72
|
+
descending? ? assets.reverse : assets
|
73
|
+
else
|
74
|
+
if descending?
|
75
|
+
if order_column.order_desc
|
76
|
+
apply_asc_order(assets, order_column.order_desc)
|
82
77
|
else
|
83
|
-
|
78
|
+
apply_desc_order(assets, order_column.order)
|
84
79
|
end
|
80
|
+
else
|
81
|
+
apply_asc_order(assets, order_column.order)
|
85
82
|
end
|
86
83
|
end
|
84
|
+
end
|
87
85
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
end
|
94
|
-
unless column.supports_order?
|
95
|
-
self.class.order_unsupported(column.name, "column don't support order" )
|
96
|
-
end
|
86
|
+
def check_order_valid!
|
87
|
+
return unless order
|
88
|
+
column = column_by_name(order)
|
89
|
+
unless column
|
90
|
+
self.class.order_unsupported(order, "no column #{order} in #{self.class}")
|
97
91
|
end
|
98
|
-
|
99
|
-
|
100
|
-
if order.respond_to?(:call)
|
101
|
-
apply_block_order(assets, order)
|
102
|
-
else
|
103
|
-
driver.asc(assets, order)
|
104
|
-
end
|
92
|
+
unless column.supports_order?
|
93
|
+
self.class.order_unsupported(column.name, "column don't support order" )
|
105
94
|
end
|
95
|
+
end
|
106
96
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
end
|
97
|
+
def apply_asc_order(assets, order)
|
98
|
+
if order.respond_to?(:call)
|
99
|
+
apply_block_order(assets, order)
|
100
|
+
else
|
101
|
+
driver.asc(assets, order)
|
113
102
|
end
|
103
|
+
end
|
114
104
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
105
|
+
def apply_desc_order(assets, order)
|
106
|
+
if order.respond_to?(:call)
|
107
|
+
reverse_order(apply_asc_order(assets, order))
|
108
|
+
else
|
109
|
+
driver.desc(assets, order)
|
119
110
|
end
|
111
|
+
end
|
120
112
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
113
|
+
def reverse_order(assets)
|
114
|
+
driver.reverse_order(assets)
|
115
|
+
rescue NotImplementedError
|
116
|
+
self.class.order_unsupported(order_column.name, "Your ORM do not support reverse order: please specify :order_desc option manually")
|
117
|
+
end
|
118
|
+
|
119
|
+
def apply_block_order(assets, order)
|
120
|
+
case order.arity
|
121
|
+
when -1, 0
|
122
|
+
assets.instance_eval(&order)
|
123
|
+
when 1
|
124
|
+
order.call(assets)
|
125
|
+
else
|
126
|
+
self.class.order_unsupported(order_column.name, "Order option proc can not handle more than one argument")
|
130
127
|
end
|
131
128
|
end
|
132
129
|
end
|
data/lib/datagrid/rspec.rb
CHANGED
@@ -46,7 +46,7 @@ shared_examples_for "Datagrid" do
|
|
46
46
|
1
|
47
47
|
when :enum
|
48
48
|
select = subject.select_options(filter)
|
49
|
-
select.first
|
49
|
+
select.first&.last
|
50
50
|
else
|
51
51
|
raise "unknown filter type: #{filter.class}"
|
52
52
|
end.to_s
|
@@ -54,7 +54,7 @@ shared_examples_for "Datagrid" do
|
|
54
54
|
|
55
55
|
before(:each) do
|
56
56
|
subject.attributes = {filter.name => filter_value}
|
57
|
-
subject.
|
57
|
+
subject.public_send(filter.name).should_not be_nil
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should be supported" do
|
data/lib/datagrid/scaffold.rb
CHANGED
@@ -4,7 +4,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
|
|
4
4
|
|
5
5
|
include Rails::Generators::ResourceHelpers
|
6
6
|
|
7
|
-
check_class_collision :
|
7
|
+
check_class_collision suffix: "Grid"
|
8
8
|
source_root File.expand_path(__FILE__ + "/../../../templates")
|
9
9
|
|
10
10
|
def create_scaffold
|
@@ -13,7 +13,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
|
|
13
13
|
end
|
14
14
|
template "grid.rb.erb", "app/grids/#{grid_class_name.underscore}.rb"
|
15
15
|
if file_exists?(grid_controller_file)
|
16
|
-
inject_into_file grid_controller_file, index_action, :
|
16
|
+
inject_into_file grid_controller_file, index_action, after: %r{class .*#{grid_controller_class_name}.*\n}
|
17
17
|
else
|
18
18
|
template "controller.rb.erb", grid_controller_file
|
19
19
|
end
|
@@ -30,7 +30,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
|
|
30
30
|
}.each do |extension, string|
|
31
31
|
file = "app/assets/stylesheets/application.#{extension}"
|
32
32
|
if file_exists?(file)
|
33
|
-
inject_into_file file, string + "\n", {:
|
33
|
+
inject_into_file file, string + "\n", {before: %r{.*require_self}} # before all
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
data/lib/datagrid/utils.rb
CHANGED
@@ -38,15 +38,12 @@ module Datagrid
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def add_html_classes(options, *classes)
|
41
|
+
return options if classes.empty?
|
41
42
|
options = options.clone
|
42
|
-
options[:class] ||=
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
# suppose that it is a String
|
47
|
-
options[:class] += " " unless options[:class].blank?
|
48
|
-
options[:class] += classes.join(" ")
|
49
|
-
end
|
43
|
+
options[:class] ||= []
|
44
|
+
array = options[:class].is_a?(Array)
|
45
|
+
value = [*options[:class], *classes]
|
46
|
+
options[:class] = array ? value : value.join(" ")
|
50
47
|
options
|
51
48
|
end
|
52
49
|
|
@@ -117,9 +114,9 @@ module Datagrid
|
|
117
114
|
if !value
|
118
115
|
value
|
119
116
|
elsif value.is_a?(Array)
|
120
|
-
[value.first
|
117
|
+
[value.first&.beginning_of_day, value.last&.end_of_day]
|
121
118
|
elsif value.is_a?(Range)
|
122
|
-
(value.
|
119
|
+
(value.begin&.beginning_of_day..value.end&.end_of_day)
|
123
120
|
else
|
124
121
|
value.beginning_of_day..value.end_of_day
|
125
122
|
end
|
data/lib/datagrid/version.rb
CHANGED
data/templates/base.rb.erb
CHANGED
@@ -11,11 +11,35 @@ class BaseGrid
|
|
11
11
|
# Enable forbidden attributes protection
|
12
12
|
# self.forbidden_attributes_protection = true
|
13
13
|
|
14
|
-
|
14
|
+
# Makes a date column
|
15
|
+
# @param name [Symbol] Column name
|
16
|
+
# @param args [Array] Other column helper arguments
|
17
|
+
# @example
|
18
|
+
# date_column(:created_at)
|
19
|
+
# date_column(:owner_registered_at) do |model|
|
20
|
+
# model.owner.registered_at
|
21
|
+
# end
|
22
|
+
def self.date_column(name, *args, &block)
|
15
23
|
column(name, *args) do |model|
|
16
|
-
format(
|
17
|
-
date
|
24
|
+
format(block ? block.call(model) : model.public_send(name)) do |date|
|
25
|
+
date&.strftime("%m/%d/%Y") || "—".html_safe
|
18
26
|
end
|
19
27
|
end
|
20
28
|
end
|
29
|
+
|
30
|
+
# Makes a boolean YES/NO column
|
31
|
+
# @param name [Symbol] Column name
|
32
|
+
# @param args [Array] Other column helper arguments
|
33
|
+
# @example
|
34
|
+
# boolean_column(:approved)
|
35
|
+
# boolean_column(:has_tasks, preload: :tasks) do |model|
|
36
|
+
# model.tasks.unfinished.any?
|
37
|
+
# end
|
38
|
+
def self.boolean_column(name, *args, &block)
|
39
|
+
column(name, *args) do |model|
|
40
|
+
value = block ? block.call(model) : model.public_send(name)
|
41
|
+
value ? "Yes" : "No"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
21
45
|
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: 1.8.
|
4
|
+
version: 1.8.2
|
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
|
+
date: 2024-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -16,16 +16,16 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '6.
|
19
|
+
version: '6.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '6.
|
27
|
-
description:
|
28
|
-
columns and filters
|
26
|
+
version: '6.1'
|
27
|
+
description: The library allows you to easily build datagrid aka data tables with
|
28
|
+
sortable columns and filters
|
29
29
|
email: agresso@gmail.com
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
@@ -113,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
|
-
rubygems_version: 3.4.
|
116
|
+
rubygems_version: 3.4.10
|
117
117
|
signing_key:
|
118
118
|
specification_version: 4
|
119
|
-
summary:
|
119
|
+
summary: Library that provides DSL to present table like data
|
120
120
|
test_files: []
|